我创建了一些具有多种功能的代码,输入X值后,它会输出一行,该行已计算出已声明的给定X值和h值的数字。但是,在终端中,每个值仅输出一行,并且我需要实现某种类型的迭代结构,该结构的h从10 ^ -1到10 ^ -8不等。这是代码:
#include <stdio.h>
#include <math.h>
int main()
{
double u(double x)
{
return(pow(x,3.0));
}
double dudx(double x)
{
return(3.0 * pow(x,2.0));
}
double fdiff(double x, double h)
{
return((u(x+h) - u(x))/h);
}
double bdiff(double x, double h)
{
return((u(x) - u(x-h))/h);
}
double cdiff(double x, double h)
{
return((u(x+h)-u(x-h))/(2*h));
}
double fderr(double x, double h)
{
return(fabs(fdiff(x,h)-dudx(x)));
}
double bderr(double x, double h)
{
return(fabs(bdiff(x,h)-dudx(x)));
}
double cderr(double x, double h)
{
return(fabs(cdiff(x,h)-dudx(x)));
}
double x;
double h=1.0 * pow(10,-1.0);
printf("Enter an Integer ");
scanf("%lf",&x);
printf("u(x): %lf du/dx: %lf fd: %lf bd: %lf cd: %lf fderr: %lf bderr: %lf cderr: %lf\n", u(x), dudx(x), fdiff(x,h), bdiff(x,h), cdiff(x,h), fderr(x,h), bderr(x,h), cderr(x,h));
}
我很确定我必须在此区域使用某种类型的for循环:
double h=1.0 * pow(10,-1.0);
但是,我不知道如何实现它!因此,重申一下,我如何创建一个以h值为10 ^ -1开始并以10 ^ -8结尾的迭代结构? 预先谢谢你!