本文共 577 字,大约阅读时间需要 1 分钟。
标签:计算方法实验
/* 本实验用变步长梯形求积公式求x / (4 + x^2)在[0, 1]的定积分。*/#include#include double f(double x){ return x / (4 + x * x);}int main(){ double a = 0, b = 1, eps = 0.00001; //上下限,精度 double s, x, h = b - a, t1, t2 = h * (f(a) + f(b)) / 2, temp; do{ s = 0, x = a + h / 2; do{ s += f(x); x += h; }while(x < b); t2 = (t1 + h * s) / 2; temp = t1, t1 = t2, h /= 2; }while(fabs(t1 - temp) >= eps); //即fabs(t2 - t1) >= eps printf("answer = %f\n", t2); return 0;}
实验结果