前提・実現したいこと
数値計算の積分で,DE公式を用いて計算したいのですが本来の値と誤差が大きくなってしまいます.以下の自分のコードはどこが間違っているのでしょうか.ご指摘お願いします.
計算する問題は,
/-1~1 1/(1+x^2)dx = pi/2
です.
発生している問題・エラーメッセージ
エラーメッセージ
該当のソースコード
C
1// 2double func(double x) 3{ 4 return 1.0 / (1.0 + x*x); 5} 6// 7 8double sqr(double t){ /*二乗する関数*/ 9 return t*t; 10} 11 12double phi(double t){ 13 return tanh(0.5*M_PI*sinh(t)); 14} 15 16double dphi(double t){ 17 return 0.5*M_PI*cosh(t)/sqr(cosh(0.5*M_PI*sinh(t))); 18} 19 20double de(double(*func)(double),double h, int *np, int *nm){ 21 double s,ds; // numerical value of the integral 22 double eps = 1.0e-16; 23 double x; 24 int n,nmax = 100; 25 //h = (xmax - xmin) / nint; // 一つの区間の幅 26 s = func(phi(0.0)*dphi(0.0));/*0項目*/ 27 for(n=1;n<=nmax;n++){ 28 x = n*h; 29 ds =func(phi(x)) * dphi(x) + func(phi(-x)) * dphi(- x); 30 s += ds; 31 if(fabs(ds)<eps){ 32 *np = n; 33 break; 34 } 35 } 36 for(n=1;n<=nmax;n++){ 37 x = -n*h; 38 ds =func(phi(x)) * dphi(x) + func(phi(-x)) * dphi(- x); 39 s += ds; 40 if(fabs(ds)<eps){ 41 *nm = n; 42 break; 43 } 44 } 45 s *= h; 46 return s; 47}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/21 05:42