给你一个示例程序,也是做积分,是y=x*x的[0,2]的定积分。
func(float x)
{
float f;
float a,b,c;
a=1,b=0,c=0;
f=a*x*x+b*x+c;
return f;
}
test_jifen()
{
float dx=0.01;
float bound_a=0,bound_b=2,x,y;
float temp;
int j;
x=bound_a;
y=0;
while(x
{
temp=(func(x))*dx;
y+=temp;
x+=dx;
}
QMessageBox::about(this,"result","y="+QString::number(y,'g'));//这句用于输出结果
}
我用qt测试过,结果是2.68几,把dx的值变成0.001更接近理论值2.66667的无限循环。
仅供参考。
亲,建议用MATLAB吧