#include <iostream.h> #include <iomanip> #include <math.h> using namespace std; int main() { cout << 3 + 5 << endl; cout << 3 - 5 << endl; cout << 3 * 5 << endl; cout << pow(3, 5) << endl; cout << 5 / 3 << endl; cout << 5.0 / 3 << endl; cout << 5 / 3.0 << endl; cout << 5 % 3 << endl; cout << setw(3) << 3 * 5 << endl; cout << setw(23) << fixed << setprecision(20) << 5 / 3.0 << endl; return 0; }
#include <iostream.h> int main() { int i = 3 * 5; cout << "3 * 5 = " << i << endl;; return 0; }
#include <iostream.h> int main() { for (int i = 1; i < 10; i++) { cout << i << ", "; } cout << endl;; return 0; }
#include <iostream.h> int main() { for (int i = 1; i < 10; i++) { if (i % 3 == 0) { cout << i << ", "; } } cout << endl;; return 0; }
#include <iostream.h> int main() { int sum = 0; for (int i = 1; i < 100; i++) { if (i % 3 == 0) { sum += i; } } cout << sum << endl;; return 0; }
#include <iostream> using namespace std; int sn(int a, int lim); int main() { // 3 の倍数の合計 cout << sn(3, 999) << endl; return 0; } // 初項:a, 公差:a で, 上限:lim の数列の総和を返す関数 int sn(int a, int lim) { int n = lim / a; // 項数:n = 上限:lim / 公差:a int l = n * a; // 末項:l = 項数:n * 公差:a return (a + l) * n / 2; // 総和:sn = (初項:a + 末項:l) * 項数:n / 2 }
#include <iostream> using namespace std; int main() { // 10000 までの 自然数の和 // 項目数 n = 10000 int n = 10000; cout << ( n * (n + 1) / 2 ) << endl; return 0; }
#include <iostream> using namespace std; int main() { // 10000 までの 偶数の和 // 項目数 n = 5000 int n = 10000 / 2; cout << ( n * (n + 1) ) << endl; return 0; }
#include <iostream> #include <math.h> using namespace std; int main() { // 10000 までの 奇数の和 // 項目数 n = 5000 int n = 10000 / 2; cout << int(pow(n, 2)) << endl; return 0; }
#include <iostream> using namespace std; int main() { // 1000 までの 自然数の2乗の和 int n = 1000; cout << ( n * (n + 1) * (2 * n + 1) / 6 ) << endl; return 0; }
#include <iostream> #include <math.h> using namespace std; int main() { // 100 までの 自然数の3乗の和 int n = 100; cout << int(pow(n, 2) * pow((n + 1), 2) / 4 ) << endl; return 0; }
#include <iostream> #include <math.h> using namespace std; int main() { // 初項 2, 公比 3, 項数 10 の等比数列の和 int n = 10; int a = 2; int r = 3; cout << ( (a * (pow(r, n) - 1)) / (r - 1) ) << endl; return 0; }
#include <iostream> using namespace std; int main() { int a = 5; // 初項 5 int d = 3; // 公差 3 int n = 10; // 項数 10 __int64 p = 1; // 積 for (int i = 1; i <= n; i++) { int m = a + (d * (i - 1)); p *= m; } cout << p << endl; return 0; }
#include <iostream> using namespace std; __int64 product(int m, int d, int n); int main() { // 初項 5, 公差 3, 項数 10 の数列の積を表示する cout << product(5, 3, 10) << endl; return 0; } __int64 product(int m, int d, int n) { if (n == 0) return 1; else return m * product(m + d, d, n - 1); }
#include <iostream> using namespace std; // 階乗を求める関数 int Fact(int n) { if (n <= 1) return 1; else return n * Fact(n - 1); } int main() { // 10の階乗 cout << Fact(10) << endl; cout << (10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1) << endl; return 0; }
#include <iostream> using namespace std; // 下降階乗冪 int FallingFact(int x, int n) { if (n <= 1) return x; else return x * FallingFact(x - 1, n - 1); } int main() { // 10 から 6 までの 総乗 cout << FallingFact(10, 5) << endl; cout << (10 * 9 * 8 * 7 * 6) << endl; return 0; }
#include <iostream> using namespace std; // 上昇階乗冪 int RisingFact(int x, int n) { if (n <= 1) return x; else return x * RisingFact(x + 1, n - 1); } int main() { // 10 から 14 までの 総乗 cout << RisingFact(10, 5) << endl; cout << (10 * 11 * 12 * 13 * 14) << endl; return 0; }
#include <iostream> using namespace std; // 階乗 int Fact(int n) { if (n <= 1) return 1; else return n * Fact(n - 1); } // 下降階乗冪 int FallingFact(int x, int n) { if (n <= 1) return x; else return x * FallingFact(x - 1, n - 1); } int main() { // 順列 (異なる 10 個のものから 5 個取ってできる順列の総数) int n = 10; int r = 5; cout << (Fact(n) / Fact(n - r)) << endl; cout << FallingFact(n, r) << endl; return 0; }
#include <iostream> #include <math.h> using namespace std; int main() { // 重複順列 (異なる 10 個のものから重複を許して 5 個取ってできる順列の総数) int n = 10; int r = 5; cout << pow(n, r) << endl; return 0; }
#include <iostream> #include <math.h> using namespace std; // 組合せ int Comb(int n, int r) { if (r == 0 || r == n) return 1; else if (r == 1) return n; else return Comb(n - 1, r - 1) + Comb(n - 1, r); } int main() { // 組合せ (異なる 10 個のものから 5 個取ってできる組合せの総数) int n = 10; int r = 5; cout << Comb(n, r) << endl; return 0; }
#include <iostream> using namespace std; // 組合せ int Comb(int n, int r) { if (r == 0 || r == n) return 1; else if (r == 1) return n; else return Comb(n - 1, r - 1) + Comb(n - 1, r); } int main() { // 重複組合せ (異なる 10 個のものから重複を許して 5 個とる組合せの総数) int n = 10; int r = 5; cout << Comb(n + r - 1, r) << endl; return 0; }
#include <iostream> #include <iomanip> #include <math> using namespace std; double mySin(double x, int n, bool nega, double numerator, double denominator, double y); int main() { for (int degree = 0; degree <= 360; degree += 15) { if (degree % 30 == 0 || degree % 45 == 0) { double radian = degree * M_PI / 180.0; // 自作の正弦関数 double d1 = mySin(radian, 1, false, radian, 1.0, radian); // 標準の正弦関数 double d2 = sin(radian); // 標準関数との差異 cout << setw(3) << degree << ":"; cout << setw(14) << fixed << setprecision(10) << d1 << " - "; cout << setw(14) << fixed << setprecision(10) << d2 << " = "; cout << setw(14) << fixed << setprecision(10) << d1 - d2 << endl; } } return 0; } // 自作の正弦関数 double mySin(double x, int n, bool nega, double numerator, double denominator, double y) { int m = 2 * n; denominator = denominator * (m + 1) * m; numerator = numerator * x * x; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (a <= 0.00000000001) return y; else return y + mySin(x, ++n, !nega, numerator, denominator, nega ? a : -a); }
#include <iostream> #include <iomanip> #include <math> using namespace std; double myCos(double x, int n, bool nega, double numerator, double denominator, double y); int main() { for (int degree = 0; degree <= 360; degree += 15) { if (degree % 30 == 0 || degree % 45 == 0) { double radian = degree * M_PI / 180.0; // 自作の余弦関数 double d1 = myCos(radian, 1, false, 1.0, 1.0, 1.0); // 標準の余弦関数 double d2 = cos(radian); // 標準関数との差異 cout << setw(3) << degree << ":"; cout << setw(14) << fixed << setprecision(10) << d1 << " - "; cout << setw(14) << fixed << setprecision(10) << d2 << " = "; cout << setw(14) << fixed << setprecision(10) << d1 - d2 << endl; } } return 0; } // 自作の余弦関数 double myCos(double x, int n, bool nega, double numerator, double denominator, double y) { int m = 2 * n; denominator = denominator * m * (m - 1); numerator = numerator * x * x; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (a <= 0.00000000001) return y; else return y + myCos(x, ++n, !nega, numerator, denominator, nega ? a : -a); }
#include <iostream> #include <iomanip> #include <math> using namespace std; double myTan(double x, double x2, int n, double t); int main() { for (int degree = -90; degree <= 90; degree += 15) { if ((degree + 90) % 180 != 0) { double radian = degree * M_PI / 180.0; double x2 = radian * radian; // 自作の正接関数 double d1 = myTan(radian, x2, 15, 0.0); // 15:必要な精度が得られる十分大きな奇数 // 標準の正接関数 double d2 = tan(radian); // 標準関数との差異 cout << setw(3) << degree << ":"; cout << setw(14) << fixed << setprecision(10) << d1 << " - "; cout << setw(14) << fixed << setprecision(10) << d2 << " = "; cout << setw(14) << fixed << setprecision(10) << d1 - d2 << endl; } } return 0; } // 自作の正接関数 double myTan(double x, double x2, int n, double t) { t = x2 / (n - t); n -= 2; if (n <= 1) return x / (1 - t); else return myTan(x, x2, n, t); }
#include <iostream> #include <iomanip> #include <math> using namespace std; double myExp(double x, int n, double numerator, double denominator, double y); int main() { for (int i = -10; i <= 10; i++) { double x = i / 4.0; // 標準の指数関数 double d1 = exp(x); // 自作の指数関数 double d2 = myExp(x, 1, 1.0, 1.0, 1.0); // 標準関数との差異 cout << setw(5) << fixed << setprecision(2) << x << ":"; cout << setw(14) << fixed << setprecision(10) << d1 << " - "; cout << setw(14) << fixed << setprecision(10) << d2 << " = "; cout << setw(14) << fixed << setprecision(10) << d1 - d2 << endl; } return 0; } // 自作の指数関数 double myExp(double x, int n, double numerator, double denominator, double y) { denominator = denominator * n; numerator = numerator * x; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (fabs(a) <= 0.00000000001) return y; else return y + myExp(x, ++n, numerator, denominator, a); }
#include <iostream> #include <iomanip> #include <math> using namespace std; double myExp(double x, double x2, int n, double t); int main() { for (int i = -10; i <= 10; i++) { double x = i / 4.0; // 標準の指数関数 double d1 = exp(x); // 自作の指数関数 double x2 = x * x; double d2 = myExp(x, x2, 30, 0.0); // 30:必要な精度が得られるよう, 6から始めて4ずつ増加させる // 標準関数との差異 cout << setw(5) << fixed << setprecision(2) << x << ":"; cout << setw(14) << fixed << setprecision(10) << d1 << " - "; cout << setw(14) << fixed << setprecision(10) << d2 << " = "; cout << setw(14) << fixed << setprecision(10) << d1 - d2 << endl; } return 0; } // 自作の指数関数 double myExp(double x, double x2, int n, double t) { t = x2 / (n + t); n -= 4; if (n < 6) return 1 + ((2 * x) / (2 - x + t)); else return myExp(x, x2, n, t); }
#include <iostream> #include <iomanip> #include <math> using namespace std; double myLog(double x2, double numerator, double denominator, double y); int main() { for (int i = 1; i <= 20; i++) { double x = i / 5.0; // 標準の対数関数 double d1 = log(x); // 自作の対数関数 double x2 = (x - 1) / (x + 1); double d2 = 2 * myLog(x2, x2, 1.0, x2); // 標準関数との差異 cout << setw(5) << fixed << setprecision(2) << x << ":"; cout << setw(14) << fixed << setprecision(10) << d1 << " - "; cout << setw(14) << fixed << setprecision(10) << d2 << " = "; cout << setw(14) << fixed << setprecision(10) << d1 - d2 << endl; } return 0; } // 自作の対数関数 double myLog(double x2, double numerator, double denominator, double y) { denominator = denominator + 2; numerator = numerator * x2 * x2; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (fabs(a) <= 0.00000000001) return y; else return y + myLog(x2, numerator, denominator, a); }
#include <iostream> #include <iomanip> #include <math> using namespace std; double myLog(double x, int n, double t); int main() { for (int i = 1; i <= 20; i++) { double x = i / 5.0; // 標準の対数関数 double d1 = log(x); // 自作の対数関数 double d2 = myLog(x - 1, 27, 0.0); // 27:必要な精度が得られる十分大きな奇数 // 標準関数との差異 cout << setw(5) << fixed << setprecision(2) << x << ":"; cout << setw(14) << fixed << setprecision(10) << d1 << " - "; cout << setw(14) << fixed << setprecision(10) << d2 << " = "; cout << setw(14) << fixed << setprecision(10) << d1 - d2 << endl; } return 0; } // 自作の対数関数 double myLog(double x, int n, double t) { int n2 = n; double x2 = x; if (n > 3) { if (n % 2 == 0) n2 = 2; x2 = x * (n / 2); } t = x2 / (n2 + t); if (n <= 2) return x / (1 + t); else return myLog(x, --n, t); }
#include <iostream> #include <iomanip> #include <math> using namespace std; double mySinh(double x, int n, double numerator, double denominator, double y); int main() { for (int x = -10; x <= 10; x++) { // 自作の双曲線正弦関数 double d1 = mySinh(x, 1, x, 1.0, x); // 標準の双曲線正弦関数 double d2 = sinh(x); // 標準関数との差異 cout << setw(3) << x << ":"; cout << setw(17) << fixed << setprecision(10) << d1 << " - "; cout << setw(17) << fixed << setprecision(10) << d2 << " = "; cout << setw(17) << fixed << setprecision(10) << d1 - d2 << endl; } return 0; } // 自作の双曲線正弦関数 double mySinh(double x, int n, double numerator, double denominator, double y) { int m = 2 * n; denominator = denominator * (m + 1) * m; numerator = numerator * x * x; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (fabs(a) <= 0.00000000001) return y; else return y + mySinh(x, ++n, numerator, denominator, a); }
#include <iostream> #include <iomanip> #include <math> using namespace std; double myCosh(double x, int n, double numerator, double denominator, double y); int main() { for (int x = -10; x <= 10; x++) { // 自作の双曲線余弦関数 double d1 = myCosh(x, 1, 1.0, 1.0, 1.0); // 標準の双曲線余弦関数 double d2 = cosh(x); // 標準関数との差異 cout << setw(3) << x << ":"; cout << setw(17) << fixed << setprecision(10) << d1 << " - "; cout << setw(17) << fixed << setprecision(10) << d2 << " = "; cout << setw(17) << fixed << setprecision(10) << d1 - d2 << endl; } return 0; } // 自作の双曲線余弦関数 double myCosh(double x, int n, double numerator, double denominator, double y) { int m = 2 * n; denominator = denominator * m * (m - 1); numerator = numerator * x * x; double a = numerator / denominator; // 十分な精度になったら処理を抜ける if (fabs(a) <= 0.00000000001) return y; else return y + myCosh(x, ++n, numerator, denominator, a); }
#include <iostream> #include <iomanip> #include <math> using namespace std; double f(double x) { return 4 / (1 + x * x); } int main() { const double a = 0; const double b = 1; // 台形則で積分 int n = 2; for (int j = 1; j <= 10; j++) { double h = (b - a) / n; double s = 0; double x = a; for (int i = 1; i <= n - 1; i++) { x += h; s += f(x); } s = h * ((f(a) + f(b)) / 2 + s); n *= 2; // 結果を π と比較 cout << setw(2) << j << ":"; cout << setw(13) << fixed << setprecision(10) << s << ", "; cout << setw(13) << fixed << setprecision(10) << s - M_PI << endl; } return 0; }
#include <iostream> #include <iomanip> #include <math> using namespace std; double f(double x) { return 4 / (1 + x * x); } int main() { const double a = 0; const double b = 1; // 中点則で積分 int n = 2; for (int j = 1; j <= 10; j++) { double h = (b - a) / n; double s = 0; double x = a + (h / 2); for (int i = 1; i <= n; i++) { s += f(x); x += h; } s *= h; n *= 2; // 結果を π と比較 cout << setw(2) << j << ":"; cout << setw(13) << fixed << setprecision(10) << s << ", "; cout << setw(13) << fixed << setprecision(10) << s - M_PI << endl; } return 0; }
#include <iostream> #include <iomanip> #include <math> using namespace std; double f(double x) { return 4 / (1 + x * x); } int main() { const double a = 0; const double b = 1; // Simpson則で積分 int n = 2; for (int j = 1; j <= 5; j++) { double h = (b - a) / n; double s2 = 0; double s4 = 0; double x = a + h; for (int i = 1; i <= n / 2; i++) { s4 += f(x); x += h; s2 += f(x); x += h; } s2 = (s2 - f(b)) * 2 + f(a) + f(b); s4 *= 4; double s = (s2 + s4) * h / 3; n *= 2; // 結果を π と比較 cout << setw(2) << j << ":"; cout << setw(13) << fixed << setprecision(10) << s << ", "; cout << setw(13) << fixed << setprecision(10) << s - M_PI << endl; } return 0; }
#include <iostream> #include <iomanip> #include <math> using namespace std; double f(double x) { return 4 / (1 + x * x); } int main() { const double a = 0; const double b = 1; double t[7][7]; // 台形則で積分 int n = 2; for (int i = 1; i <= 6; i++) { double h = (b - a) / n; double s = 0; double x = a; for (int j = 1; j <= n - 1; j++) { x += h; s += f(x); } // 結果を保存 t[i][1] = h * ((f(a) + f(b)) / 2 + s); n *= 2; } // Richardsonの補外法 n = 4; for (int j = 2; j <= 6; j++) { for (int i = j; i <= 6; i++) { t[i][j] = t[i][j - 1] + (t[i][j - 1] - t[i - 1][j - 1]) / (n - 1); if (i == j) { // 結果を π と比較 cout << setw(2) << j << ":"; cout << setw(13) << fixed << setprecision(10) << t[i][j] << ", "; cout << setw(13) << fixed << setprecision(10) << t[i][j] - M_PI << endl; } } n *= 4; } return 0; }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; // データ点の数 const int N = 7; // 元の関数 double f(double x); // Lagrange (ラグランジュ) 補間 double lagrange(double d, double x[], double y[]); int main() { double x[N], y[N]; // 1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット for (int i = 0; i < N; i++) { double d = i * 1.5 - 4.5; x[i] = d; y[i] = f(d); } // 0.5刻みで 与えられていない値を補間 for (int i = 0; i <= 18; i++) { double d = i * 0.5 - 4.5; double d1 = f(d); double d2 = lagrange(d, x, y); // 元の関数と比較 cout << setw(5) << fixed << setprecision(2) << d << '\t'; cout << setw(8) << fixed << setprecision(5) << d1 << '\t'; cout << setw(8) << fixed << setprecision(5) << d2 << '\t'; cout << setw(8) << fixed << setprecision(5) << d1 - d2 << endl; } return 0; } // 元の関数 double f(double x) { return x - pow(x,3) / (3 * 2) + pow(x,5) / (5 * 4 * 3 * 2); } // Lagrange (ラグランジュ) 補間 double lagrange(double d, double x[], double y[]) { double sum = 0; for (int i = 0; i < N; i++) { double prod = y[i]; for (int j = 0; j < N; j++) { if (j != i) prod *= (d - x[j]) / (x[i] - x[j]); } sum += prod; } return sum; }
#include <iostream> #include <iomanip> #include <math.h> #include <stdio.h> using namespace std; // データ点の数 const int N = 7; // 元の関数 double f(double x); // Neville (ネヴィル) 補間 double neville(double d, double x[], double y[]); int main() { double x[N], y[N]; // 1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット for (int i = 0; i < N; i++) { double d = i * 1.5 - 4.5; x[i] = d; y[i] = f(d); } // 0.5刻みで 与えられていない値を補間 for (int i = 0; i <= 18; i++) { double d = i * 0.5 - 4.5; double d1 = f(d); double d2 = neville(d, x, y); // 元の関数と比較 cout << setw(5) << fixed << setprecision(2) << d << '\t'; cout << setw(8) << fixed << setprecision(5) << d1 << '\t'; cout << setw(8) << fixed << setprecision(5) << d2 << '\t'; cout << setw(8) << fixed << setprecision(5) << d1 - d2 << endl; } return 0; } // 元の関数 double f(double x) { return x - pow(x,3) / (3 * 2) + pow(x,5) / (5 * 4 * 3 * 2); } // Neville (ネヴィル) 補間 double neville(double d, double x[], double y[]) { double w[N][N]; for (int i = 0; i < N; i++) w[0][i] = y[i]; for (int j = 1; j < N; j++) { for (int i = 0; i < N - j; i++) w[j][i] = w[j-1][i+1] + (w[j-1][i+1] - w[j-1][i]) * (d - x[i+j]) / (x[i+j] - x[i]); } return w[N-1][0]; }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; // データ点の数 const int N = 7; // 元の関数 double f(double x); // Newton (ニュートン) 補間 double newton(double d, double x[], double a[]); int main() { double x[N], y[N]; // 1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット for (int i = 0; i < N; i++) { double d = i * 1.5 - 4.5; x[i] = d; y[i] = f(d); } // 差分商の表を作る double d[N][N]; for (int j = 0; j < N; j++) d[0][j] = y[j]; for (int i = 1; i < N; i++) { for (int j = 0; j < N - i; j++) d[i][j] = (d[i-1][j+1] - d[i-1][j]) / (x[j+i] - x[j]); } // n階差分商 double a[N]; for (int j = 0; j < N; j++) a[j] = d[j][0]; // 0.5刻みで 与えられていない値を補間 for (int i = 0; i <= 18; i++) { double d = i * 0.5 - 4.5; double d1 = f(d); double d2 = newton(d, x, a); // 元の関数と比較 cout << setw(5) << fixed << setprecision(2) << d << '\t'; cout << setw(8) << fixed << setprecision(5) << d1 << '\t'; cout << setw(8) << fixed << setprecision(5) << d2 << '\t'; cout << setw(8) << fixed << setprecision(5) << d1 - d2 << endl; } return 0; } // 元の関数 double f(double x) { return x - pow(x,3) / (3 * 2) + pow(x,5) / (5 * 4 * 3 * 2); } // Newton (ニュートン) 補間 double newton(double d, double x[], double a[]) { double sum = a[0]; for (int i = 1; i < N; i++) { double prod = a[i]; for (int j = 0; j < i; j++) prod *= (d - x[j]); sum += prod; } return sum; }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; // データ点の数 const int N = 7; const int Nx2 = 14; // 元の関数 double f(double x); // 導関数 double fd(double x); // Hermite (エルミート) 補間 double hermite(double d, double z[], double a[]); int main() { double x[N], y[N], yd[N]; // 1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット for (int i = 0; i < N; i++) { double d = i * 1.5 - 4.5; x[i] = d; y[i] = f(d); yd[i] = fd(d); } // 差分商の表を作る double z[Nx2]; double d[Nx2][Nx2]; for (int i = 0; i < Nx2; i++) { int j = i / 2; z[i] = x[j]; d[0][i] = y[j]; } for (int i = 1; i < Nx2; i++) { for (int j = 0; j < Nx2 - i; j++) { if (i == 1 && j % 2 == 0) d[i][j] = yd[j / 2]; else d[i][j] = (d[i-1][j+1] - d[i-1][j]) / (z[j+i] - z[j]); } } // n階差分商 double a[Nx2]; for (int j = 0; j < Nx2; j++) a[j] = d[j][0]; // 0.5刻みで 与えられていない値を補間 for (int i = 0; i <= 18; i++) { double d = i * 0.5 - 4.5; double d1 = f(d); double d2 = hermite(d, z, a); // 元の関数と比較 cout << setw(5) << fixed << setprecision(2) << d << '\t'; cout << setw(8) << fixed << setprecision(5) << d1 << '\t'; cout << setw(8) << fixed << setprecision(5) << d2 << '\t'; cout << setw(8) << fixed << setprecision(5) << d1 - d2 << endl; } return 0; } // 元の関数 double f(double x) { return x - pow(x,3) / (3 * 2) + pow(x,5) / (5 * 4 * 3 * 2); } // 導関数 double fd(double x) { return 1 - pow(x,2) / 2 + pow(x,4) / (4 * 3 * 2); } // Hermite (エルミート) 補間 double hermite(double d, double z[], double a[]) { double sum = a[0]; for (int i = 1; i < Nx2; i++) { double prod = a[i]; for (int j = 0; j < i; j++) prod *= (d - z[j]); sum += prod; } return sum; }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; // データ点の数 const int N = 7; // 元の関数 double f(double x); // Spline (スプライン) 補間 double spline(double d, double x[], double y[], double z[]); int main() { double x[N], y[N]; // 1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット for (int i = 0; i < N; i++) { double d = i * 1.5 - 4.5; x[i] = d; y[i] = f(d); } // 3項方程式の係数の表を作る double a[N], b[N], c[N], d[N]; for (int i = 1; i < N - 1; i++) { a[i] = x[i] - x[i-1]; b[i] = 2.0 * (x[i+1] - x[i-1]); c[i] = x[i+1] - x[i]; d[i] = 6.0 * ((y[i+1] - y[i]) / (x[i+1] - x[i]) - (y[i] - y[i-1]) / (x[i] - x[i-1])); } // 3項方程式を解く (ト−マス法) double g[N], s[N]; g[1] = b[1]; s[1] = d[1]; for (int i = 2; i < N - 1; i++) { g[i] = b[i] - a[i] * c[i-1] / g[i-1]; s[i] = d[i] - a[i] * s[i-1] / g[i-1]; } double z[N]; z[0] = 0; z[N-1] = 0; z[N-2] = s[N-2] / g[N-2]; for (int i = N - 3; i >= 1; i--) { z[i] = (s[i] - c[i] * z[i+1]) / g[i]; } // 0.5刻みで 与えられていない値を補間 for (int i = 0; i <= 18; i++) { double d = i * 0.5 - 4.5; double d1 = f(d); double d2 = spline(d, x, y, z); // 元の関数と比較 cout << setw(5) << fixed << setprecision(2) << d << '\t'; cout << setw(8) << fixed << setprecision(5) << d1 << '\t'; cout << setw(8) << fixed << setprecision(5) << d2 << '\t'; cout << setw(8) << fixed << setprecision(5) << d1 - d2 << endl; } return 0; } // 元の関数 double f(double x) { return x - pow(x,3) / (3 * 2) + pow(x,5) / (5 * 4 * 3 * 2); } // Spline (スプライン) 補間 double spline(double d, double x[], double y[], double z[]) { // 補間関数値がどの区間にあるか int k = -1; for (int i = 1; i < N; i++) { if (d <= x[i]) { k = i - 1; break; } } if (k < 0) k = N - 1; double d1 = x[k+1] - d; double d2 = d - x[k]; double d3 = x[k+1] - x[k]; return (z[k] * pow(d1,3) + z[k+1] * pow(d2,3)) / (6.0 * d3) + (y[k] / d3 - z[k] * d3 / 6.0) * d1 + (y[k+1] / d3 - z[k+1] * d3 / 6.0) * d2; }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; // 重力加速度 const double g = -9.8; // 空気抵抗係数 const double k = -0.01; // 時間間隔(秒) const double h = 0.01; // 空気抵抗による水平方向の減速分 double fx(double vx, double vy); // 重力と空気抵抗による鉛直方向の減速分 double fy(double vx, double vy); int main() { // 角度 double degree = 45; double radian = degree * M_PI / 180.0; // 初速 250 km/h -> 秒速に変換 double v = 250 * 1000 / 3600; // 水平方向の速度 double vx[2]; vx[0] = v * cos(radian); // 鉛直方向の速度 double vy[2]; vy[0] = v * sin(radian); // 経過秒数 double t = 0.0; // 位置 double x = 0.0; double y = 0.0; // Euler法 for (int i = 1; y >= 0.0; i++) { // 経過秒数 t = i * h; cout << setw(4) << fixed << setprecision(2) << t << "\t"; // 位置 x += h * vx[0]; y += h * vy[0]; cout << setw(8) << fixed << setprecision(5) << vx[0] << "\t"; cout << setw(9) << fixed << setprecision(5) << vy[0] << "\t"; cout << setw(9) << fixed << setprecision(5) << x << "\t"; cout << setw(8) << fixed << setprecision(5) << y << endl; // 速度 vx[1] = vx[0] + h * fx(vx[0], vy[0]); vy[1] = vy[0] + h * fy(vx[0], vy[0]); vx[0] = vx[1]; vy[0] = vy[1]; } return 0; } // 空気抵抗による水平方向の減速分 double fx(double vx, double vy) { return k * sqrt(vx * vx + vy * vy) * vx; } // 重力と空気抵抗による鉛直方向の減速分 double fy(double vx, double vy) { return g + (k * sqrt(vx * vx + vy * vy) * vy); }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; // 重力加速度 const double g = -9.8; // 空気抵抗係数 const double k = -0.01; // 時間間隔(秒) const double h = 0.01; // 空気抵抗による水平方向の減速分 double fx(double vx, double vy); // 重力と空気抵抗による鉛直方向の減速分 double fy(double vx, double vy); int main() { // 角度 double degree = 45; double radian = degree * M_PI / 180.0; // 初速 250 km/h -> 秒速に変換 double v = 250 * 1000 / 3600; // 水平方向の速度 double vx[3]; vx[0] = v * cos(radian); // 鉛直方向の速度 double vy[3]; vy[0] = v * sin(radian); // 経過秒数 double t = 0.0; // 位置 double x[3]; x[0] = 0.0; double y[3]; y[0] = 0.0; // Heun法 for (int i = 1; y[0] >= 0.0; i++) { // 経過秒数 t = i * h; cout << setw(4) << fixed << setprecision(2) << t << "\t"; // 位置・速度 x[1] = x[0] + h * vx[0]; y[1] = y[0] + h * vy[0]; vx[1] = vx[0] + h * fx(vx[0], vy[0]); vy[1] = vy[0] + h * fy(vx[0], vy[0]); x[2] = x[0] + h * ( vx[0] + vx[1] ) / 2; y[2] = y[0] + h * ( vy[0] + vy[1] ) / 2; vx[2] = vx[0] + h * (fx(vx[0], vy[0]) + fx(vx[1], vy[1])) / 2; vy[2] = vy[0] + h * (fy(vx[0], vy[0]) + fy(vx[1], vy[1])) / 2; x[0] = x[2]; y[0] = y[2]; vx[0] = vx[2]; vy[0] = vy[2]; cout << setw(8) << fixed << setprecision(5) << vx[0] << "\t"; cout << setw(9) << fixed << setprecision(5) << vy[0] << "\t"; cout << setw(9) << fixed << setprecision(5) << x[0] << "\t"; cout << setw(8) << fixed << setprecision(5) << y[0] << endl; } return 0; } // 空気抵抗による水平方向の減速分 double fx(double vx, double vy) { return k * sqrt(vx * vx + vy * vy) * vx; } // 重力と空気抵抗による鉛直方向の減速分 double fy(double vx, double vy) { return g + (k * sqrt(vx * vx + vy * vy) * vy); }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; // 重力加速度 const double g = -9.8; // 空気抵抗係数 const double k = -0.01; // 時間間隔(秒) const double h = 0.01; // 空気抵抗による水平方向の減速分 double fx(double vx, double vy); // 重力と空気抵抗による鉛直方向の減速分 double fy(double vx, double vy); int main() { // 角度 double degree = 45; double radian = degree * M_PI / 180.0; // 初速 250 km/h -> 秒速に変換 double v = 250 * 1000 / 3600; // 水平方向の速度 double vx[2]; vx[0] = v * cos(radian); // 鉛直方向の速度 double vy[2]; vy[0] = v * sin(radian); // 経過秒数 double t = 0.0; // 位置 double x[2]; x[0] = 0.0; double y[2]; y[0] = 0.0; // 中点法 for (int i = 1; y[0] >= 0.0; i++) { // 経過秒数 t = i * h; cout << setw(4) << fixed << setprecision(2) << t << "\t"; // 位置・速度 vx[1] = h * fx(vx[0], vy[0]); vy[1] = h * fy(vx[0], vy[0]); double wx = vx[0] + vx[1] / 2; double wy = vy[0] + vy[1] / 2; vx[0] = vx[0] + h * fx(wx, wy); vy[0] = vy[0] + h * fy(wx, wy); x[0] = x[0] + h * wx; y[0] = y[0] + h * wy; cout << setw(8) << fixed << setprecision(5) << vx[0] << "\t"; cout << setw(9) << fixed << setprecision(5) << vy[0] << "\t"; cout << setw(9) << fixed << setprecision(5) << x[0] << "\t"; cout << setw(8) << fixed << setprecision(5) << y[0] << endl; } return 0; } // 空気抵抗による水平方向の減速分 double fx(double vx, double vy) { return k * sqrt(vx * vx + vy * vy) * vx; } // 重力と空気抵抗による鉛直方向の減速分 double fy(double vx, double vy) { return g + (k * sqrt(vx * vx + vy * vy) * vy); }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; // 重力加速度 const double g = -9.8; // 空気抵抗係数 const double k = -0.01; // 時間間隔(秒) const double h = 0.01; // 空気抵抗による水平方向の減速分 double fx(double vx, double vy); // 重力と空気抵抗による鉛直方向の減速分 double fy(double vx, double vy); int main() { // 角度 double degree = 45; double radian = degree * M_PI / 180.0; // 初速 250 km/h -> 秒速に変換 double v = 250 * 1000 / 3600; // 水平方向の速度 double vx[5]; vx[0] = v * cos(radian); // 鉛直方向の速度 double vy[5]; vy[0] = v * sin(radian); // 経過秒数 double t = 0.0; // 位置 double x[5]; x[0] = 0.0; double y[5]; y[0] = 0.0; // Runge-Kutta法 for (int i = 1; y[0] >= 0.0; i++) { // 経過秒数 t = i * h; cout << setw(4) << fixed << setprecision(2) << t << "\t"; // 位置・速度 x[1] = h * vx[0]; y[1] = h * vy[0]; vx[1] = h * fx(vx[0], vy[0]); vy[1] = h * fy(vx[0], vy[0]); double wx = vx[0] + vx[1] / 2; double wy = vy[0] + vy[1] / 2; x[2] = h * wx; y[2] = h * wy; vx[2] = h * fx(wx, wy); vy[2] = h * fy(wx, wy); wx = vx[0] + vx[2] / 2; wy = vy[0] + vy[2] / 2; x[3] = h * wx; y[3] = h * wy; vx[3] = h * fx(wx, wy); vy[3] = h * fy(wx, wy); wx = vx[0] + vx[3]; wy = vy[0] + vy[3]; x[4] = h * wx; y[4] = h * wy; vx[4] = h * fx(wx, wy); vy[4] = h * fy(wx, wy); x[0] += ( x[1] + x[2] * 2 + x[3] * 2 + x[4]) / 6; y[0] += ( y[1] + y[2] * 2 + y[3] * 2 + y[4]) / 6; vx[0] += (vx[1] + vx[2] * 2 + vx[3] * 2 + vx[4]) / 6; vy[0] += (vy[1] + vy[2] * 2 + vy[3] * 2 + vy[4]) / 6; cout << setw(8) << fixed << setprecision(5) << vx[0] << "\t"; cout << setw(9) << fixed << setprecision(5) << vy[0] << "\t"; cout << setw(9) << fixed << setprecision(5) << x[0] << "\t"; cout << setw(8) << fixed << setprecision(5) << y[0] << endl; } return 0; } // 空気抵抗による水平方向の減速分 double fx(double vx, double vy) { return k * sqrt(vx * vx + vy * vy) * vx; } // 重力と空気抵抗による鉛直方向の減速分 double fy(double vx, double vy) { return g + (k * sqrt(vx * vx + vy * vy) * vy); }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; // 重力加速度 const double g = -9.8; // 空気抵抗係数 const double k = -0.01; // 時間間隔(秒) const double h = 0.01; // 空気抵抗による水平方向の減速分 double fx(double vx, double vy); // 重力と空気抵抗による鉛直方向の減速分 double fy(double vx, double vy); int main() { // 角度 double degree = 45; double radian = degree * M_PI / 180.0; // 初速 250 km/h -> 秒速に変換 double v = 250 * 1000 / 3600; // 水平方向の速度 double vx[5]; vx[0] = v * cos(radian); // 鉛直方向の速度 double vy[5]; vy[0] = v * sin(radian); // 経過秒数 double t = 0.0; // 位置 double x[5]; x[0] = 0.0; double y[5]; y[0] = 0.0; // Runge-Kutta-Gill法 for (int i = 1; y[0] >= 0.0; i++) { // 経過秒数 t = i * h; cout << setw(4) << fixed << setprecision(2) << t << "\t"; // 速度 x[1] = h * vx[0]; y[1] = h * vy[0]; vx[1] = h * fx(vx[0], vy[0]); vy[1] = h * fy(vx[0], vy[0]); double wx = vx[0] + vx[1] / 2; double wy = vy[0] + vy[1] / 2; x[2] = h * wx; y[2] = h * wy; vx[2] = h * fx(wx, wy); vy[2] = h * fy(wx, wy); wx = vx[0] + vx[1] * ((sqrt(2.0) - 1) / 2) + vx[2] * (1 - 1 / sqrt(2.0)); wy = vy[0] + vy[1] * ((sqrt(2.0) - 1) / 2) + vy[2] * (1 - 1 / sqrt(2.0)); x[3] = h * wx; y[3] = h * wy; vx[3] = h * fx(wx, wy); vy[3] = h * fy(wx, wy); wx = vx[0] - vx[2] / sqrt(2.0) + vx[3] * (1 + 1 / sqrt(2.0)); wy = vy[0] - vy[2] / sqrt(2.0) + vy[3] * (1 + 1 / sqrt(2.0)); x[4] = h * wx; y[4] = h * wy; vx[4] = h * fx(wx, wy); vy[4] = h * fy(wx, wy); x[0] += ( x[1] + x[2] * (2 - sqrt(2.0)) + x[3] * (2 + sqrt(2.0)) + x[4]) / 6; y[0] += ( y[1] + y[2] * (2 - sqrt(2.0)) + y[3] * (2 + sqrt(2.0)) + y[4]) / 6; vx[0] += (vx[1] + vx[2] * (2 - sqrt(2.0)) + vx[3] * (2 + sqrt(2.0)) + vx[4]) / 6; vy[0] += (vy[1] + vy[2] * (2 - sqrt(2.0)) + vy[3] * (2 + sqrt(2.0)) + vy[4]) / 6; cout << setw(8) << fixed << setprecision(5) << vx[0] << "\t"; cout << setw(9) << fixed << setprecision(5) << vy[0] << "\t"; cout << setw(9) << fixed << setprecision(5) << x[0] << "\t"; cout << setw(8) << fixed << setprecision(5) << y[0] << endl; } return 0; } // 空気抵抗による水平方向の減速分 double fx(double vx, double vy) { return k * sqrt(vx * vx + vy * vy) * vx; } // 重力と空気抵抗による鉛直方向の減速分 double fy(double vx, double vy) { return g + (k * sqrt(vx * vx + vy * vy) * vy); }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; // 重力加速度 const double g = -9.8; // 空気抵抗係数 const double k = -0.01; // 時間間隔(秒) const double h = 0.01; // 空気抵抗による水平方向の減速分 double fx(double vx, double vy); // 重力と空気抵抗による鉛直方向の減速分 double fy(double vx, double vy); int main() { // 角度 double degree = 45; double radian = degree * M_PI / 180.0; // 初速 250 km/h -> 秒速に変換 double v = 250 * 1000 / 3600; // 水平方向の速度 double vx[3]; vx[0] = v * cos(radian); // 鉛直方向の速度 double vy[3]; vy[0] = v * sin(radian); // 経過秒数 double t = 0.0; // 位置 double x = 0.0; double y = 0.0; // 後退 Euler法 for (int i = 1; y >= 0.0; i++) { // 経過秒数 t = i * h; cout << setw(4) << fixed << setprecision(2) << t << "\t"; // 位置・速度 vx[1] = vx[0] + h * fx(vx[0], vy[0]); vy[1] = vy[0] + h * fy(vx[0], vy[0]); for (int j = 0; j <= 10; j++) { vx[2] = vx[0] + h * fx(vx[1], vy[1]); vy[2] = vy[0] + h * fy(vx[1], vy[1]); if ((fabs(vx[1] - vx[2]) < 0.00001) && (fabs(vy[1] - vy[2]) < 0.00001)) break; vx[1] = vx[2]; vy[1] = vy[2]; } vx[0] = vx[1]; vy[0] = vy[1]; x += h * vx[0]; y += h * vy[0]; cout << setw(8) << fixed << setprecision(5) << vx[0] << "\t"; cout << setw(9) << fixed << setprecision(5) << vy[0] << "\t"; cout << setw(9) << fixed << setprecision(5) << x << "\t"; cout << setw(8) << fixed << setprecision(5) << y << endl; } return 0; } // 空気抵抗による水平方向の減速分 double fx(double vx, double vy) { return k * sqrt(vx * vx + vy * vy) * vx; } // 重力と空気抵抗による鉛直方向の減速分 double fy(double vx, double vy) { return g + (k * sqrt(vx * vx + vy * vy) * vy); }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; double f(double x) { return x * x - 2; } double bisection(double a, double b) { double c; while (true) { // 区間 (a, b) の中点 c = (a + b) / 2 c = (a + b) / 2; cout << setw(12) << fixed << setprecision(10) << c << "\t"; cout << setw(13) << fixed << setprecision(10) << c - sqrt(2) << endl; double fc = f(c); if (fabs(fc) < 0.0000000001) break; if (fc < 0) { // f(c) < 0 であれば, 解は区間 (c, b) の中に存在 a = c; } else { // f(c) > 0 であれば, 解は区間 (a, c) の中に存在 b = c; } } return c; } int main() { double a = 1; double b = 2; cout << setw(12) << fixed << setprecision(10) << bisection(a, b) << endl; return 0; }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; double f(double x) { return x * x - 2; } double falseposition(double a, double b) { double c; while (true) { // 点 (a,f(a)) と 点 (b,f(b)) を結ぶ直線と x軸の交点 c = (a * f(b) - b * f(a)) / (f(b) - f(a)); cout << setw(12) << fixed << setprecision(10) << c << "\t"; cout << setw(13) << fixed << setprecision(10) << c - sqrt(2) << endl; double fc = f(c); if (fabs(fc) < 0.0000000001) break; if (fc < 0) { // f(c) < 0 であれば, 解は区間 (c, b) の中に存在 a = c; } else { // f(c) > 0 であれば, 解は区間 (a, c) の中に存在 b = c; } } return c; } int main() { double a = 1; double b = 2; cout << setw(12) << fixed << setprecision(10) << falseposition(a, b) << endl; return 0; }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; double g(double x) { return (x / 2) + (1 / x); } double iterative(double x0) { double x1; while (true) { x1 = g(x0); cout << setw(12) << fixed << setprecision(10) << x1 << "\t"; cout << setw(13) << fixed << setprecision(10) << x1 - sqrt(2) << endl; if (fabs(x1 - x0) < 0.0000000001) break; x0 = x1; } return x1; } int main() { double x = 1.0; cout << setw(12) << fixed << setprecision(10) << iterative(x) << endl; return 0; }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; double f0(double x) { return x * x - 2; } double f1(double x) { return 2 * x; } double newton(double x0) { double x1; while (true) { x1 = x0 - (f0(x0) / f1(x0)); cout << setw(12) << fixed << setprecision(10) << x1 << "\t"; cout << setw(13) << fixed << setprecision(10) << x1 - sqrt(2) << endl; if (fabs(x1 - x0) < 0.0000000001) break; x0 = x1; } return x1; } int main() { double x = 2.0; cout << setw(12) << fixed << setprecision(10) << newton(x) << endl; return 0; }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; double f0(double x) { return x * x - 2; } double f1(double x) { return 2 * x; } double f2(double x) { return 2; } double bailey(double x0) { double x1; while (true) { x1 = x0 - (f0(x0) / (f1(x0) - (f0(x0) * f2(x0) / (2 * f1(x0))))); cout << setw(12) << fixed << setprecision(10) << x1 << "\t"; cout << setw(13) << fixed << setprecision(10) << x1 - sqrt(2) << endl; if (fabs(x1 - x0) < 0.0000000001) break; x0 = x1; } return x1; } int main() { double x = 2.0; cout << setw(12) << fixed << setprecision(10) << bailey(x) << endl; return 0; }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; double f(double x) { return x * x - 2; } double secant(double x0, double x1) { double x2; while (true) { x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0)); cout << setw(12) << fixed << setprecision(10) << x2 << "\t"; cout << setw(13) << fixed << setprecision(10) << x2 - sqrt(2) << endl; if (fabs(x2 - x1) < 0.0000000001) break; x0 = x1; x1 = x2; } return x2; } int main() { double x0 = 1; double x1 = 2; cout << setw(12) << fixed << setprecision(10) << secant(x0, x1) << endl; return 0; }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; const int N = 4; // ヤコビの反復法 void jacobi(double a[N][N], double b[N], double c[N]); // 1次元配列を表示 void disp_vector(double row[N]); int main() { double a[N][N] = {{9,2,1,1},{2,8,-2,1},{-1,-2,7,-2},{1,-1,-2,6}}; double b[N] = {20,16,8,17}; double c[N] = {0,0,0,0}; // ヤコビの反復法 jacobi(a,b,c); cout << "X" << endl; disp_vector(c); return 0; } // ヤコビの反復法 void jacobi(double a[N][N], double b[N], double x0[N]) { while (true) { double x1[N]; bool finish = true; for (int i = 0; i < N; i++) { x1[i] = 0; for (int j = 0; j < N; j++) if (j != i) x1[i] += a[i][j] * x0[j]; x1[i] = (b[i] - x1[i]) / a[i][i]; if (fabs(x1[i] - x0[i]) > 0.0000000001) finish = false; } for (int i = 0; i < N; i++) x0[i] = x1[i]; if (finish) return; disp_vector(x0); } } // 1次元配列を表示 void disp_vector(double row[N]) { for (int i = 0; i < N; i++) cout << setw(14) << fixed << setprecision(10) << row[i] << "\t"; cout << endl; }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; const int N = 4; // ガウス・ザイデル法 void gauss(double a[N][N], double b[N], double c[N]); // 1次元配列を表示 void disp_vector(double row[N]); int main() { double a[N][N] = {{9,2,1,1},{2,8,-2,1},{-1,-2,7,-2},{1,-1,-2,6}}; double b[N] = {20,16,8,17}; double c[N] = {0,0,0,0}; // ガウス・ザイデル法 gauss(a,b,c); cout << "X" << endl; disp_vector(c); return 0; } // ガウス・ザイデル法 void gauss(double a[N][N], double b[N], double x0[N]) { while (true) { bool finish = true; for (int i = 0; i < N; i++) { double x1 = 0; for (int j = 0; j < N; j++) if (j != i) x1 += a[i][j] * x0[j]; x1 = (b[i] - x1) / a[i][i]; if (fabs(x1 - x0[i]) > 0.0000000001) finish = false; x0[i] = x1; } if (finish) return; disp_vector(x0); } } // 1次元配列を表示 void disp_vector(double row[N]) { for (int i = 0; i < N; i++) cout << setw(14) << fixed << setprecision(10) << row[i] << "\t"; cout << endl; }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; const int N = 4; // ピボット選択 void pivoting(double a[N][N], double b[N]); // 前進消去 void forward_elimination(double a[N][N], double b[N]); // 後退代入 void backward_substitution(double a[N][N], double b[N]); // 1次元配列を表示 void disp_vector(double row[N]); // 2次元配列を表示 void disp_matrix(double matrix[N][N]); // ガウスの消去法 int main() { double a[N][N] = {{-1,-2,7,-2},{1,-1,-2,6},{9,2,1,1},{2,8,-2,1}}; double b[N] = {8,17,20,16}; // ピボット選択 pivoting(a,b); cout << "pivoting" << endl; cout << "A" << endl; disp_matrix(a); cout << "B" << endl; disp_vector(b); cout << endl; // 前進消去 forward_elimination(a,b); cout << "forward elimination" << endl; cout << "A" << endl; disp_matrix(a); cout << "B" << endl; disp_vector(b); cout << endl; // 後退代入 backward_substitution(a,b); cout << "X" << endl; disp_vector(b); return 0; } // 前進消去 void forward_elimination(double a[N][N], double b[N]) { for (int pivot = 0; pivot < N - 1; pivot++) { for (int row = pivot + 1; row < N; row++) { double s = a[row][pivot] / a[pivot][pivot]; for (int col = pivot; col < N; col++) a[row][col] -= a[pivot][col] * s; b[row] -= b[pivot] * s; } } } // 後退代入 void backward_substitution(double a[N][N], double b[N]) { for (int row = N - 1; row >= 0; row--) { for (int col = N - 1; col > row; col--) b[row] -= a[row][col] * b[col]; b[row] /= a[row][row]; } } // ピボット選択 void pivoting(double a[N][N], double b[N]) { for(int pivot = 0; pivot < N; pivot++) { // 各列で 一番値が大きい行を 探す int max_row = pivot; double max_val = 0; for (int row = pivot; row < N; row++) { if (fabs(a[row][pivot]) > max_val) { // 一番値が大きい行 max_val = fabs(a[row][pivot]); max_row = row; } } // 一番値が大きい行と入れ替え if (max_row != pivot) { double tmp; for (int col = 0; col < N; col++) { tmp = a[max_row][col]; a[max_row][col] = a[pivot][col]; a[pivot][col] = tmp; } tmp = b[max_row]; b[max_row] = b[pivot]; b[pivot] = tmp; } } } // 1次元配列を表示 void disp_vector(double row[N]) { for (int i = 0; i < N; i++) cout << setw(14) << fixed << setprecision(10) << row[i] << "\t"; cout << endl; } // 2次元配列を表示 void disp_matrix(double matrix[N][N]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) cout << setw(14) << fixed << setprecision(10) << matrix[i][j] << "\t"; cout << endl; } }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; const int N = 4; // ピボット選択 void pivoting(double a[N][N], double b[N]); // 前進消去 void forward_elimination(double a[N][N], double b[N]); // 後退代入 void backward_substitution(double a[N][N], double b[N]); // 1次元配列を表示 void disp_vector(double row[N]); // 2次元配列を表示 void disp_matrix(double matrix[N][N]); // ガウス・ジョルダン法 int main() { double a[N][N] = {{-1,-2,7,-2},{1,-1,-2,6},{9,2,1,1},{2,8,-2,1}}; double b[N] = {8,17,20,16}; // ピボット選択 pivoting(a,b); cout << "pivoting" << endl; cout << "A" << endl; disp_matrix(a); cout << "B" << endl; disp_vector(b); cout << endl; // 前進消去 forward_elimination(a,b); cout << "forward elimination" << endl; cout << "A" << endl; disp_matrix(a); cout << "B" << endl; disp_vector(b); cout << endl; // 後退代入 backward_substitution(a,b); cout << "X" << endl; disp_vector(b); return 0; } // 前進消去 void forward_elimination(double a[N][N], double b[N]) { for (int pivot = 0; pivot < N; pivot++) { for (int row = 0; row < N; row++) { if (row == pivot) continue; double s = a[row][pivot] / a[pivot][pivot]; for (int col = pivot; col < N; col++) a[row][col] -= a[pivot][col] * s; b[row] -= b[pivot] * s; } } } // 後退代入 void backward_substitution(double a[N][N], double b[N]) { for (int pivot = 0; pivot < N; pivot++) b[pivot] /= a[pivot][pivot]; } // ピボット選択 void pivoting(double a[N][N], double b[N]) { for(int pivot = 0; pivot < N; pivot++) { // 各列で 一番値が大きい行を 探す int max_row = pivot; double max_val = 0; for (int row = pivot; row < N; row++) { if (fabs(a[row][pivot]) > max_val) { // 一番値が大きい行 max_val = fabs(a[row][pivot]); max_row = row; } } // 一番値が大きい行と入れ替え if (max_row != pivot) { double tmp; for (int col = 0; col < N; col++) { tmp = a[max_row][col]; a[max_row][col] = a[pivot][col]; a[pivot][col] = tmp; } tmp = b[max_row]; b[max_row] = b[pivot]; b[pivot] = tmp; } } } // 1次元配列を表示 void disp_vector(double row[N]) { for (int i = 0; i < N; i++) cout << setw(14) << fixed << setprecision(10) << row[i] << "\t"; cout << endl; } // 2次元配列を表示 void disp_matrix(double matrix[N][N]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) cout << setw(14) << fixed << setprecision(10) << matrix[i][j] << "\t"; cout << endl; } }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; const int N = 4; // ピボット選択 void pivoting(double a[N][N], double b[N]); // 前進消去 void forward_elimination(double a[N][N]); // 前進代入 void forward_substitution(double a[N][N], double b[N], double y[N]); // 後退代入 void backward_substitution(double a[N][N], double y[N], double x[N]); // 1次元配列を表示 void disp_vector(double row[N]); // 2次元配列を表示 void disp_matrix(double matrix[N][N]); // LU分解 int main() { double a[N][N] = {{-1,-2,7,-2},{1,-1,-2,6},{9,2,1,1},{2,8,-2,1}}; double b[N] = {8,17,20,16}; // ピボット選択 pivoting(a,b); cout << "pivoting" << endl; cout << "A" << endl; disp_matrix(a); cout << "B" << endl; disp_vector(b); cout << endl; // 前進消去 forward_elimination(a); cout << "LU" << endl; disp_matrix(a); // Ly=b から y を求める (前進代入) double y[N] = {0,0,0,0}; forward_substitution(a, b, y); cout << "Y" << endl; disp_vector(y); // Ux=y から x を求める (後退代入) double x[N] = {0,0,0,0}; backward_substitution(a, y, x); cout << "X" << endl; disp_vector(x); return 0; } // 前進消去 void forward_elimination(double a[N][N]) { for (int pivot = 0; pivot < N - 1; pivot++) { for (int row = pivot + 1; row < N; row++) { double s = a[row][pivot] / a[pivot][pivot]; for (int col = pivot; col < N; col++) a[row][col] -= a[pivot][col] * s; // これが 上三角行列 a[row][pivot] = s; // これが 下三角行列 // b[row] -= b[pivot] * s; // この値は変更しない } } } // 前進代入 void forward_substitution(double a[N][N], double b[N], double y[N]) { for (int row = 0; row < N; row++) { for (int col = 0; col < row; col++) b[row] -= a[row][col] * y[col]; y[row] = b[row]; } } // 後退代入 void backward_substitution(double a[N][N], double y[N], double x[N]) { for (int row = N - 1; row >= 0; row--) { for (int col = N - 1; col > row; col--) y[row] -= a[row][col] * x[col]; x[row] = y[row] / a[row][row]; } } // ピボット選択 void pivoting(double a[N][N], double b[N]) { for(int pivot = 0; pivot < N; pivot++) { // 各列で 一番値が大きい行を 探す int max_row = pivot; double max_val = 0; for (int row = pivot; row < N; row++) { if (fabs(a[row][pivot]) > max_val) { // 一番値が大きい行 max_val = fabs(a[row][pivot]); max_row = row; } } // 一番値が大きい行と入れ替え if (max_row != pivot) { double tmp; for (int col = 0; col < N; col++) { tmp = a[max_row][col]; a[max_row][col] = a[pivot][col]; a[pivot][col] = tmp; } tmp = b[max_row]; b[max_row] = b[pivot]; b[pivot] = tmp; } } } // 1次元配列を表示 void disp_vector(double row[N]) { for (int i = 0; i < N; i++) cout << setw(14) << fixed << setprecision(10) << row[i] << "\t"; cout << endl; } // 2次元配列を表示 void disp_matrix(double matrix[N][N]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) cout << setw(14) << fixed << setprecision(10) << matrix[i][j] << "\t"; cout << endl; } }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; const int N = 4; // 前進消去 void forward_elimination(double a[N][N]); // 前進代入 void forward_substitution(double a[N][N], double b[N], double y[N]); // 後退代入 void backward_substitution(double a[N][N], double y[N], double x[N]); // 1次元配列を表示 void disp_vector(double row[N]); // 2次元配列を表示 void disp_matrix(double matrix[N][N]); // コレスキー法 int main() { double a[N][N] = {{5,2,3,4},{2,10,6,7},{3,6,15,9},{4,7,9,20}}; double b[N] = {34,68,96,125}; cout << "A" << endl; disp_matrix(a); cout << "B" << endl; disp_vector(b); cout << endl; // 前進消去 forward_elimination(a); cout << "LL^T" << endl; disp_matrix(a); // Ly=b から y を求める (前進代入) double y[N] = {0,0,0,0}; forward_substitution(a, b, y); cout << "Y" << endl; disp_vector(y); // L^Tx=y から x を求める (後退代入) double x[N] = {0,0,0,0}; backward_substitution(a, y, x); cout << "X" << endl; disp_vector(x); return 0; } // 前進消去 void forward_elimination(double a[N][N]) { for (int pivot = 0; pivot < N; pivot++) { double s = 0; for (int col = 0; col < pivot; col++) s += a[pivot][col] * a[pivot][col]; // ここで根号の中が負の値になると計算できない! a[pivot][pivot] = sqrt(a[pivot][pivot] - s); for (int row = pivot + 1; row < N; row++) { s = 0; for (int col = 0; col < pivot; col++) s += a[row][col] * a[pivot][col]; a[row][pivot] = (a[row][pivot] - s) / a[pivot][pivot]; a[pivot][row] = a[row][pivot]; } } } // 前進代入 void forward_substitution(double a[N][N], double b[N], double y[N]) { for (int row = 0; row < N; row++) { for (int col = 0; col < row; col++) b[row] -= a[row][col] * y[col]; y[row] = b[row] / a[row][row]; } } // 後退代入 void backward_substitution(double a[N][N], double y[N], double x[N]) { for (int row = N - 1; row >= 0; row--) { for (int col = N - 1; col > row; col--) y[row] -= a[row][col] * x[col]; x[row] = y[row] / a[row][row]; } } // 1次元配列を表示 void disp_vector(double row[N]) { for (int i = 0; i < N; i++) cout << setw(14) << fixed << setprecision(10) << row[i] << "\t"; cout << endl; } // 2次元配列を表示 void disp_matrix(double matrix[N][N]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) cout << setw(14) << fixed << setprecision(10) << matrix[i][j] << "\t"; cout << endl; } }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; const int N = 4; // 前進消去 void forward_elimination(double a[N][N]); // 前進代入 void forward_substitution(double a[N][N], double b[N], double y[N]); // 後退代入 void backward_substitution(double a[N][N], double y[N], double x[N]); // 1次元配列を表示 void disp_vector(double row[N]); // 2次元配列を表示 void disp_matrix(double matrix[N][N]); // 修正コレスキー法 int main() { double a[N][N] = {{5,2,3,4},{2,10,6,7},{3,6,15,9},{4,7,9,20}}; double b[N] = {34,68,96,125}; cout << "A" << endl; disp_matrix(a); cout << "B" << endl; disp_vector(b); cout << endl; // 前進消去 forward_elimination(a); cout << "LDL^T" << endl; disp_matrix(a); // Ly=b から y を求める (前進代入) double y[N] = {0,0,0,0}; forward_substitution(a, b, y); cout << "Y" << endl; disp_vector(y); // DL^Tx=y から x を求める (後退代入) double x[N] = {0,0,0,0}; backward_substitution(a, y, x); cout << "X" << endl; disp_vector(x); return 0; } // 前進消去 void forward_elimination(double a[N][N]) { for (int pivot = 0; pivot < N; pivot++) { double s; // pivot < k の場合 for (int col = 0; col < pivot; col++) { s = a[pivot][col]; for (int k = 0; k < col; k++) s -= a[pivot][k] * a[col][k] * a[k][k]; a[pivot][col] = s / a[col][col]; a[col][pivot] = a[pivot][col]; } // pivot == k の場合 s = a[pivot][pivot]; for (int k = 0; k < pivot; k++) s -= a[pivot][k] * a[pivot][k] * a[k][k]; a[pivot][pivot] = s; } } // 前進代入 void forward_substitution(double a[N][N], double b[N], double y[N]) { for (int row = 0; row < N; row++) { for (int col = 0; col < row; col++) b[row] -= a[row][col] * y[col]; y[row] = b[row]; } } // 後退代入 void backward_substitution(double a[N][N], double y[N], double x[N]) { for (int row = N - 1; row >= 0; row--) { for (int col = N - 1; col > row; col--) y[row] -= a[row][col] * a[row][row] * x[col]; x[row] = y[row] / a[row][row]; } } // 1次元配列を表示 void disp_vector(double row[N]) { for (int i = 0; i < N; i++) cout << setw(14) << fixed << setprecision(10) << row[i] << "\t"; cout << endl; } // 2次元配列を表示 void disp_matrix(double matrix[N][N]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) cout << setw(14) << fixed << setprecision(10) << matrix[i][j] << "\t"; cout << endl; } }
#include <iostream> #include <iomanip> #include <math.h> #include <vector> using namespace std; const int N = 4; // ベキ乗法 double power(double a[N][N], double x0[N]); // 1次元配列を表示 void disp_vector(double row[N]); // 正規化 (ベクトルの長さを1にする) void normarize(double x[N]); // ベキ乗法で最大固有値を求める int main() { double a[N][N] = {{5.0, 4.0, 1.0, 1.0}, {4.0, 5.0, 1.0, 1.0}, {1.0, 1.0, 4.0, 2.0}, {1.0, 1.0, 2.0, 4.0}}; double x[N] = {1.0 ,0.0 ,0.0 ,0.0}; // ベキ乗法 double lambda = power(a, x); cout << endl << "eigenvalue" << endl; cout << setw(14) << fixed << setprecision(10) << lambda; cout << endl << "eigenvector" << endl; disp_vector(x); return 0; } // ベキ乗法 double power(double a[N][N], double x0[N]) { double lambda = 0.0; // 正規化 (ベクトル x0 の長さを1にする) normarize(x0); double e0 = 0.0; for (int i = 0; i < N; i++) e0 += x0[i]; for (int k = 1; k <= 200; k++) { // 1次元配列を表示 cout << setw(3) << k << "\t"; disp_vector(x0); // 行列の積 x1 = A × x0 double x1[N] = {0.0 ,0.0 ,0.0 ,0.0}; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) x1[i] += a[i][j] * x0[j]; // 内積 double p0 = 0.0; double p1 = 0.0; for (int i = 0; i < N; i++) { p0 += x1[i] * x1[i]; p1 += x1[i] * x0[i]; } // 固有値 lambda = p0 / p1; // 正規化 (ベクトル x1 の長さを1にする) normarize(x1); // 収束判定 double e1 = 0.0; for (int i = 0; i < N; i++) e1 += x1[i]; if (fabs(e0 - e1) < 0.00000000001) break; for (int i = 0; i < N; i++) x0[i] = x1[i]; e0 = e1; } return lambda; } // 1次元配列を表示 void disp_vector(double row[N]) { for (int i = 0; i < N; i++) cout << setw(14) << fixed << setprecision(10) << row[i] << "\t"; cout << endl; } // 正規化 (ベクトルの長さを1にする) void normarize(double x[N]) { double s = 0.0; for (int i = 0; i < N; i++) s += x[i] * x[i]; s = sqrt(s); for (int i = 0; i < N; i++) x[i] /= s; }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; const int N = 4; // 逆ベキ乗法 double inverse(double a[N][N], double x0[N]); // LU分解 void forward_elimination(double a[N][N]); // 前進代入 void forward_substitution(double a[N][N], double y[N], double b[N]); // 後退代入 void backward_substitution(double a[N][N], double x[N], double y[N]); // 1次元配列を表示 void disp_vector(double row[N]); // 正規化 (ベクトルの長さを1にする) void normarize(double x[N]); // 逆ベキ乗法で最小固有値を求める int main() { double a[N][N] = {{5.0, 4.0, 1.0, 1.0}, {4.0, 5.0, 1.0, 1.0}, {1.0, 1.0, 4.0, 2.0}, {1.0, 1.0, 2.0, 4.0}}; double x[N] = {1.0 ,0.0 ,0.0 ,0.0}; // LU分解 forward_elimination(a); // 逆ベキ乗法 double lambda = inverse(a, x); cout << endl << "eigenvalue" << endl; cout << setw(14) << fixed << setprecision(10) << lambda; cout << endl << "eigenvector" << endl; disp_vector(x); return 0; } // 逆ベキ乗法 double inverse(double a[N][N], double x0[N]) { double lambda = 0.0; // 正規化 (ベクトル x0 の長さを1にする) normarize(x0); double e0 = 0.0; for (int i = 0; i < N; i++) e0 += x0[i]; for (int k = 1; k <= 100; k++) { // 1次元配列を表示 cout << setw(3) << k << "\t"; disp_vector(x0); // Ly = b から y を求める (前進代入) double b[N] = {0.0 ,0.0 ,0.0 ,0.0}; double y[N] = {0.0 ,0.0 ,0.0 ,0.0}; for (int i = 0; i < N; i++) b[i] = x0[i]; forward_substitution(a,y,b); // Ux = y から x を求める (後退代入) double x1[N] = {0.0 ,0.0 ,0.0 ,0.0}; backward_substitution(a,x1,y); // 内積 double p0 = 0.0; double p1 = 0.0; for (int i = 0; i < N; i++) { p0 += x1[i] * x1[i]; p1 += x1[i] * x0[i]; } // 固有値 lambda = p1 / p0; // 正規化 (ベクトル x1 の長さを1にする) normarize(x1); // 収束判定 double e1 = 0.0; for (int i = 0; i < N; i++) e1 += x1[i]; if (fabs(e0 - e1) < 0.00000000001) break; for (int i = 0; i < N; i++) x0[i] = x1[i]; e0 = e1; } return lambda; } // LU分解 void forward_elimination(double a[N][N]) { for (int pivot = 0; pivot < N - 1; pivot++) { for (int row = pivot + 1; row < N; row++) { double s = a[row][pivot] / a[pivot][pivot]; for (int col = pivot; col < N; col++) a[row][col] -= a[pivot][col] * s; // これが 上三角行列 a[row][pivot] = s; // これが 下三角行列 } } } // 前進代入 void forward_substitution(double a[N][N], double y[N], double b[N]) { for (int row = 0; row < N; row++) { for (int col = 0; col < row; col++) b[row] -= a[row][col] * y[col]; y[row] = b[row]; } } // 後退代入 void backward_substitution(double a[N][N], double x[N], double y[N]) { for (int row = N - 1; row >= 0; row--) { for (int col = N - 1; col > row; col--) y[row] -= a[row][col] * x[col]; x[row] = y[row] / a[row][row]; } } // 1次元配列を表示 void disp_vector(double row[N]) { for (int i = 0; i < N; i++) cout << setw(14) << fixed << setprecision(10) << row[i] << "\t"; cout << endl; } // 正規化 (ベクトルの長さを1にする) void normarize(double x[N]) { double s = 0.0; for (int i = 0; i < N; i++) s += x[i] * x[i]; s = sqrt(s); for (int i = 0; i < N; i++) x[i] /= s; }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; const int N = 4; // LU分解 void decomp(double a[N][N], double l[N][N], double u[N][N]); // 行列の積 void multiply(double a[N][N], double b[N][N], double c[N][N]); // 対角要素を表示 void disp_eigenvalue(double a[N][N]); // LR分解で固有値を求める int main() { double a[N][N] = {{5.0, 4.0, 1.0, 1.0}, {4.0, 5.0, 1.0, 1.0}, {1.0, 1.0, 4.0, 2.0}, {1.0, 1.0, 2.0, 4.0}}; double l[N][N] = {{0.0 ,0.0 ,0.0 ,0.0}, {0.0 ,0.0 ,0.0 ,0.0}, {0.0 ,0.0 ,0.0 ,0.0}, {0.0 ,0.0 ,0.0 ,0.0}}; double u[N][N] = {{0.0 ,0.0 ,0.0 ,0.0}, {0.0 ,0.0 ,0.0 ,0.0}, {0.0 ,0.0 ,0.0 ,0.0}, {0.0 ,0.0 ,0.0 ,0.0}}; for (int k = 1; k <= 200; k++) { // LU分解 decomp(a, l, u); // 行列の積 multiply(u, l, a); // 対角要素を表示 cout << setw(3) << k << "\t"; disp_eigenvalue(a); // 収束判定 double e = 0.0; for (int i = 1; i < N; i++) for (int j = 0; j < i; j++) e += fabs(a[i][j]); if (e < 0.00000000001) break; } cout << endl << "eigenvalue" << endl; disp_eigenvalue(a); return 0; } // LU分解 void decomp(double a[N][N], double l[N][N], double u[N][N]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { l[i][j] = 0.0; u[i][j] = 0.0; } } l[0][0] = 1.0; for (int j = 0; j < N; j++) u[0][j] = a[0][j]; for (int i = 1; i < N; i++) { u[i][0] = 0.0; l[0][i] = 0.0; l[i][0] = a[i][0] / u[0][0]; } for (int i = 1; i < N; i++) { l[i][i] = 1.0; double t = a[i][i]; for (int k = 0; k <= i; k++) t -= l[i][k] * u[k][i]; u[i][i] = t; for (int j = i + 1; j < N; j++) { u[j][i] = 0.0; l[i][j] = 0.0; t = a[j][i]; for (int k = 0; k <= i; k++) t -= l[j][k] * u[k][i]; l[j][i] = t / u[i][i]; t = a[i][j]; for (int k = 0; k <= i; k++) t -= l[i][k] * u[k][j]; u[i][j] = t; } } } // 行列の積 void multiply(double a[N][N], double b[N][N], double c[N][N]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { double s = 0.0; for (int k = 0; k < N; k++) s += a[i][k] * b[k][j]; c[i][j] = s; } } } // 対角要素を表示 void disp_eigenvalue(double a[N][N]) { for (int i = 0; i < N; i++) cout << setw(14) << fixed << setprecision(10) << a[i][i] << "\t"; cout << endl; }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; const int N = 4; // QR分解 void decomp(double a[N][N], double q[N][N], double r[N][N]); // 行列の積 void multiply(double a[N][N], double b[N][N], double c[N][N]); // 対角要素を表示 void disp_eigenvalue(double a[N][N]); // QR分解で固有値を求める int main() { double a[N][N] = {{5.0, 4.0, 1.0, 1.0}, {4.0, 5.0, 1.0, 1.0}, {1.0, 1.0, 4.0, 2.0}, {1.0, 1.0, 2.0, 4.0}}; double q[N][N] = {{0.0 ,0.0 ,0.0 ,0.0}, {0.0 ,0.0 ,0.0 ,0.0}, {0.0 ,0.0 ,0.0 ,0.0}, {0.0 ,0.0 ,0.0 ,0.0}}; double r[N][N] = {{0.0 ,0.0 ,0.0 ,0.0}, {0.0 ,0.0 ,0.0 ,0.0}, {0.0 ,0.0 ,0.0 ,0.0}, {0.0 ,0.0 ,0.0 ,0.0}}; for (int k = 1; k <= 200; k++) { // QR分解 decomp(a, q, r); // 行列の積 multiply(r, q, a); // 対角要素を表示 cout << setw(3) << k << "\t"; disp_eigenvalue(a); // 収束判定 double e = 0.0; for (int i = 1; i < N; i++) for (int j = 0; j < i; j++) e += fabs(a[i][j]); if (e < 0.00000000001) break; } cout << endl << "eigenvalue" << endl; disp_eigenvalue(a); return 0; } // QR分解 void decomp(double a[N][N], double q[N][N], double r[N][N]) { double x[N] = {0.0 ,0.0 ,0.0 ,0.0}; for (int k = 0; k < N; k++) { for (int i = 0; i < N; i++) x[i] = a[i][k]; for (int j = 0; j < k; j++) { double t = 0.0; for (int i = 0; i < N; i++) t += a[i][k] * q[i][j]; r[j][k] = t; r[k][j] = 0.0; for (int i = 0; i < N; i++) x[i] -= t * q[i][j]; } double s = 0.0; for (int i = 0; i < N; i++) s += x[i] * x[i]; r[k][k] = sqrt(s); for (int i = 0; i < N; i++) q[i][k] = x[i] / r[k][k]; } } // 行列の積 void multiply(double a[N][N], double b[N][N], double c[N][N]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { double s = 0.0; for (int k = 0; k < N; k++) s += a[i][k] * b[k][j]; c[i][j] = s; } } } // 対角要素を表示 void disp_eigenvalue(double a[N][N]) { for (int i = 0; i < N; i++) cout << setw(14) << fixed << setprecision(10) << a[i][i] << "\t"; cout << endl; }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; const int N = 4; // ヤコビ法 void jacobi(double a[N][N], double v[N][N]); // 対角要素を表示 void disp_eigenvalue(double a[N][N]); // 固有ベクトルを表示 void disp_eigenvector(double matrix[N][N]); // 1次元配列を表示 void disp_vector(double row[N]); // 正規化 (ベクトルの長さを1にする) void normarize(double x[N]); // ヤコビ法で固有値を求める int main() { double a[N][N] = {{5.0, 4.0, 1.0, 1.0}, {4.0, 5.0, 1.0, 1.0}, {1.0, 1.0, 4.0, 2.0}, {1.0, 1.0, 2.0, 4.0}}; double v[N][N] = {{1.0 ,0.0 ,0.0 ,0.0}, {0.0 ,1.0 ,0.0 ,0.0}, {0.0 ,0.0 ,1.0 ,0.0}, {0.0 ,0.0 ,0.0 ,1.0}}; // ヤコビ法 jacobi(a, v); cout << endl << "eigenvalue" << endl; disp_eigenvalue(a); cout << endl << "eigenvector" << endl; disp_eigenvector(v); return 0; } // ヤコビ法 void jacobi(double a[N][N], double v[N][N]) { for (int k = 1; k <= 100; k++) { // 最大値を探す int p = 0; int q = 0; double max_val = 0.0; for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { if (max_val < fabs(a[i][j])) { max_val = fabs(a[i][j]); p = i; q = j; } } } // θ を求める double t; if (fabs(a[p][p] - a[q][q]) < 0.00000000001) { // a_{pp} = a_{qq} のとき、回転角tをπ/4にする t = M_PI / 4.0; if (a[p][p] < 0) t = -t; } else { // a_{pp} ≠ a_{qq} のとき t = atan(2.0 * a[p][q] / (a[p][p] - a[q][q])) / 2.0; } // θ を使って 行列 U を作成し、A = U^t × A × U double c = cos(t); double s = sin(t); // U^t × A double t1; double t2; for (int i = 0; i < N; i++) { t1 = a[p][i] * c + a[q][i] * s; t2 = -a[p][i] * s + a[q][i] * c; a[p][i] = t1; a[q][i] = t2; // 固有ベクトル t1 = v[p][i] * c + v[q][i] * s; t2 = -v[p][i] * s + v[q][i] * c; v[p][i] = t1; v[q][i] = t2; } // A × U for (int i = 0; i < N; i++) { t1 = a[i][p] * c + a[i][q] * s; t2 = -a[i][p] * s + a[i][q] * c; a[i][p] = t1; a[i][q] = t2; } // 対角要素を表示 cout << setw(3) << k << "\t"; disp_eigenvalue(a); // 収束判定 if (max_val < 0.00000000001) break; } } // 対角要素を表示 void disp_eigenvalue(double a[N][N]) { for (int i = 0; i < N; i++) cout << setw(14) << fixed << setprecision(10) << a[i][i] << "\t"; cout << endl; } // 固有ベクトルを表示 void disp_eigenvector(double matrix[N][N]) { for (int i = 0; i < N; i++) { double row[N]; for (int j = 0; j < N; j++) row[j] = matrix[i][j] ; normarize(row); disp_vector(row); } } // 1次元配列を表示 void disp_vector(double row[N]) { for (int i = 0; i < N; i++) cout << setw(14) << fixed << setprecision(10) << row[i] << "\t"; cout << endl; } // 正規化 (ベクトルの長さを1にする) void normarize(double x[N]) { double s = 0.0; for (int i = 0; i < N; i++) s += x[i] * x[i]; s = sqrt(s); for (int i = 0; i < N; i++) x[i] /= s; }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; const int N = 4; // ハウスホルダー変換 void tridiagonalize(double a[N][N], double d[N], double e[N]); // QR分解 void decomp(double a[N][N], double d[N], double e[N]); // 1次元配列を表示 void disp_vector(double row[N]); // 2次元配列を表示 void disp_matrix(double matrix[N][N]); // ハウスホルダー変換とQR分解で固有値を求める int main() { double a[N][N] = {{5.0, 4.0, 1.0, 1.0}, {4.0, 5.0, 1.0, 1.0}, {1.0, 1.0, 4.0, 2.0}, {1.0, 1.0, 2.0, 4.0}}; double d[N]; double e[N]; // ハウスホルダー変換 tridiagonalize(a, d, e); // QR分解 decomp(a, d, e); cout << endl << "eigenvalue" << endl; disp_vector(d); cout << endl << "eigenvector" << endl; disp_matrix(a); return 0; } // ハウスホルダー変換 void tridiagonalize(double a[N][N], double d[N], double e[N]) { double v[N]; for (int k = 0; k < N - 2; k++) { double w[N] = {0.0 ,0.0 ,0.0 ,0.0}; d[k] = a[k][k]; double t = 0.0; for (int i = k + 1; i < N; i++) { w[i] = a[i][k]; t += w[i] * w[i]; } double s = sqrt(t); if (w[k + 1] < 0) s = -s; if (fabs(s) < 0.00000000001) e[k + 1] = 0.0; else { e[k + 1] = -s; w[k + 1] += s; s = 1 / sqrt(w[k + 1] * s); for (int i = k + 1; i < N; i++) w[i] *= s; for (int i = k + 1; i < N; i++) { s = 0.0; for (int j = k + 1; j < N; j++) { if (j <= i) s += a[i][j] * w[j]; else s += a[j][i] * w[j]; } v[i] = s; } s = 0.0; for (int i = k + 1; i < N; i++) s += w[i] * v[i]; s /= 2.0; for (int i = k + 1; i < N; i++) v[i] -= s * w[i]; for (int i = k + 1; i < N; i++) for (int j = k + 1; j <= i; j++) a[i][j] -= w[i] * v[j] + w[j] * v[i]; // 次の行は固有ベクトルを求めないなら不要 for (int i = k + 1; i < N; i++) a[i][k] = w[i]; } } d[N - 2] = a[N - 2][N - 2]; d[N - 1] = a[N - 1][N - 1]; e[0] = 0.0; e[N - 1] = a[N - 1][N - 2]; // 次の行は固有ベクトルを求めないなら不要 for (int k = N - 1; k >= 0; k--) { double w[N] = {0.0 ,0.0 ,0.0 ,0.0}; if (k < N - 2) { for (int i = k + 1; i < N; i++) w[i] = a[i][k]; for (int i = k + 1; i < N; i++) { double s = 0.0; for (int j = k + 1; j < N; j++) s += a[i][j] * w[j]; v[i] = s; } for (int i = k + 1; i < N; i++) for (int j = k + 1; j < N; j++) a[i][j] -= v[i] * w[j]; } for (int i = 0; i < N; i++) a[i][k] = 0.0; a[k][k] = 1.0; } } // QR分解 void decomp(double a[N][N], double d[N], double e[N]) { e[0] = 1.0; int h = N - 1; while (fabs(e[h]) < 0.00000000001) h--; while (h > 0) { e[0] = 0.0; int l = h - 1; while (fabs(e[l]) >= 0.00000000001) l--; for (int j = 1; j <= 100; j++) { double w = (d[h - 1] - d[h]) / 2.0; double s = sqrt(w * w + e[h] * e[h]); if (w < 0.0) s = -s; double x = d[l] - d[h] + e[h] * e[h] / (w + s); double y = e[l + 1]; double z = 0.0; double t; double u; for (int k = l; k < h; k++) { if (fabs(x) >= fabs(y)) { t = -y / x; u = 1 / sqrt(t * t + 1.0); s = t * u; } else { t = -x / y; s = 1 / sqrt(t * t + 1.0); if (t < 0) s = -s; u = t * s; } w = d[k] - d[k + 1]; t = (w * s + 2 * u * e[k + 1]) * s; d[k ] = d[k ] - t; d[k + 1] = d[k + 1] + t; e[k ] = u * e[k] - s * z; e[k + 1] = e[k + 1] * (u * u - s * s) + w * s * u; // 次の行は固有ベクトルを求めないなら不要 for (int i = 0; i < N; i++) { x = a[k ][i]; y = a[k + 1][i]; a[k ][i] = u * x - s * y; a[k + 1][i] = s * x + u * y; } if (k < N - 2) { x = e[k + 1]; y = -s * e[k + 2]; z = y; e[k + 2] = u * e[k + 2]; } } cout << setw(3) << j << "\t"; disp_vector(d); // 収束判定 if (fabs(e[h]) < 0.00000000001) break; } e[0] = 1.0; while (fabs(e[h]) < 0.00000000001) h--; } // 次の行は固有ベクトルを求めないなら不要 for (int k = 0; k < N - 1; k++) { int l = k; for (int i = k + 1; i < N; i++) if (d[i] > d[l]) l = i; double t = d[k]; d[k] = d[l]; d[l] = t; for (int i = 0; i < N; i++) { t = a[k][i]; a[k][i] = a[l][i]; a[l][i] = t; } } } // 1次元配列を表示 void disp_vector(double row[N]) { for (int i = 0; i < N; i++) cout << setw(14) << fixed << setprecision(10) << row[i] << "\t"; cout << endl; } // 2次元配列を表示 void disp_matrix(double matrix[N][N]) { for (int row = 0; row < N; row++) { for (int col = 0; col < N; col++) cout << setw(14) << fixed << setprecision(10) << matrix[row][col] << "\t"; cout << endl; } }