cプログラミング。 ∫[0,b]exp(-x)dxの分割数を1,10,‥10⁶として数値計算し、厳密値1-exp(-b)との誤差を表示するプログラムを作ってください。 以下のように作成したのですが、実行結果が思わしくないです。どこを直せばよろしいのでしょうか。 #include <stdio.h> #include <stdlib.h> #include <math.h> double integ(double (*f)(), double x1, double x2, int n) { int i; double dx = (x2 - x1)/n, s = 0.0; for (i = 0; i < n; i++) s += f(x1 + (i+0.5)*dx)*dx; return s; } int main(int argc, char **argv) { double xi, a = 0, b= 49/100.0 ; int i; printf("Integration of exp(-x) for [%.2f, %.2f] and errors¥n",a, b); for(i = 0; i < 7; i++){ xi = integ(exp, a, b, pow(10, i)); printf("%.15f%.15e¥n", xi, 1-exp(xi)); } printf("strict value = %.15f¥n", 1-exp(-b)); return 0; } /* :~$ ./a.out Integration of exp(-x) for [0.00, 0.49] and errors 0.626034443470394-8.701795521887901e-01 0.632252966416497-8.818455416653783e-01 0.632315587376137-8.819633883288893e-01 0.632316213629581-8.819645669153122e-01 0.632316219892120-8.819645787011885e-01 0.632316219954751-8.819645788190567e-01 0.632316219955385-8.819645788202515e-01 strict value = 0.387373605815584
C言語関連