今天给各位分享c语言迭代法求方程根的知识,其中也会对迭代法求解方程的根进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
1、(C语言)牛顿迭代法求方程的根2、c语言用迭代法编程求近似根3、C语言编程:牛顿迭代法求方程的根4、用迭代法求方程的根
(C语言)牛顿迭代法求方程的根
#include stdio.h
#include math.h
/******start******/
double Resolve( double,double,double,double );
/******end******/
int main()
{
double a,b,c,d=0;
double y;
printf(“input a b c d:”);
scanf(“%lf%lf%lf%lf”, a, b, c, d);
y = Resolve(a,b,c,d);
printf(“output:\n%.4lf\n”, y);
return 0;
}
/******start******/
double f(double a,double b,double c,double d,double x)
{
return a*x*x*x + b*x*x + c*x +d ;
}
double f1(double a,double b,double c,double x)
{
return 3*a*x*x + 2*b*x + c ;
}
double Resolve( double a,double b,double c,double d )
{
double x0,x1,e;
x0=1;
e=f(a,b,c,d,x0);
do {
x1=x0-e/f1(a,b,c,x0) ;
x0=x1;
e=f(a,b,c,d,x0);
}while( e = 1e-5 );
return x1 ;
}
/******end******/
c语言用迭代法编程求近似根
不动点的定义,设X是一个集合,T 是X到X的自映射,如果存在一个x0属于X,使得Tx0=x0,
则称x0为映射T的一个不动点。
不动点迭代,Tx=x
即将Tx的 n-1 次方作为变量带入,得到n次方
#includestdio.h
#includemath.h
double between = 0.0;//确定内存足够
double diedai(double x,double cement)
{
between = exp(x)+10*x-2;//计算结果
if(between1e-5)//精度是否达到要求
{
x = (2-exp(x))/10;
diedai(x,cement);//否则进行迭代
}
return x;
}
int main()
{
double cement = 1e-5;//设置精度
double result;
result = diedai(0,cement);
printf(“\n达到要求精度时X=%f”,result);
getchar();
}
结果:
纯手打,费脑力,只求采纳
C语言编程:牛顿迭代法求方程的根
程序流程分析:
① 赋值x0=1.5,即迭代初值;
② 用初值x0代入方程中计算此时的f(x0)及f’(x0),程序中用变量f描述方程的值,用fd描述方程求导之后的值;
③ 计算增量d=f/fd;
④ 计算下一个x,x=x0-d;
⑤ 把新产生的x替换x0,为下一次迭代做好准备;
⑥ 若d绝对值大于1e-3,则重复②③④⑤步。
源程序代码:
#include math.h
main()
{
float x,x0,d,f,fd;
x0=0;
do {
f=2*x0*x0*x0-4*x0*x0+3*x0-6;
fd=6*x0*x0-8*x0+3;
d=f/fd;
x=x0-d;
x0=x;
}while(fabs(d)1e-3);
printf(“x=%f\n”,x);
}
用迭代法求方程的根
C++行么?
这两天忙考试,过两天可以帮你改成C的,实验报告我也,有电子档的
下载地址:
2简单迭代法
#includeiostream
#includemath.h
using namespace std;
double f(double); //申明函数
int main()
{
double a,i=0,a1,pr;
cout”please input the start number:”;
cina;
cout”please input precision:”;
cinpr; //输入精度
do
{
a1=a;
a=f(a);
i++;
if(i1000000)
{cout”Iterative is defeated!”;return 0;}
}
while((a-a1)pr||(a-a1)-pr);
cout”Iterative Numbers is “iendl;
cout”The answer is “aendl; //输出迭代次数和结果
}
double f(double x) //定义函数
{
return pow((x+1.0),(1.0/3));
}
3Aitken迭代法
#includeiostream
#includemath.h
using namespace std;
double f(double); //申明函数
int main()
{
double a,i=0,a1,fa,faa,pr;
cout”please input the start number:”;
cina;
cout”please input precision:”;
cinpr; //输入精度
do
{
a1=a;
fa=f(a);
faa=f(fa);
a=faa-(faa-fa)*(faa-fa)/(faa-fa*2+a);
i++;
if(i1000000)
{cout”Iterative is defeated!”;return 0;}
}
while((a-a1)pr||(a-a1)-pr);
cout”Iterative Numbers is “iendl;
cout”The answer is “aendl; //输出迭代次数和结果
return 0;
}
double f(double x) //定义函数
{
return pow((x+1.0),(1.0/3));
}
4Newton迭代
#includeiostream
#includemath.h
using namespace std;
double f(double); //申明函数
int main()
{
double a,i=0,a1,pr;
cout”please input the start number:”;
cina;
cout”please input precision:”;
cinpr; //输入精度
do
{
a1=a;
a=f(a);
i++;
if(i1000000)
{cout”Iterative is defeated!”;return 0;}
}
while((a-a1)pr||(a-a1)-pr);
cout”Iterative Numbers is “iendl;
cout”The answer is “aendl; //输出迭代次数和结果
}
double f(double x) //定义函数
{
return x-(x*x*x-x-1)/(3*x*x-1);
}
结果:
#includeiostream
#includemath.h
using namespace std;
double f(double); //申明函数
double g(double,double); //申明弦截法函数
int main()
{
double a,i=0,b,b1,pr;
cout”please input the first number:”;
cina;
cout”please input the second number:”;
cinb;
cout”please input precision:”;
cinpr; //输入精度
do
{
b1=b;
b=g(a,b);
a=b1;
i++;
if(i1000000)
{cout”Iterative is defeated!”;return 0;}
}
while((b-a)pr||(b-a)-pr);
cout”Iterative Numbers is “iendl;
cout”The answer is “aendl; //输出迭代次数和结果
}
double f(double x) //定义函数
{
return x*x*x-x-1;
}
double g(double a,double b) //定义弦截法函数
{
return b-f(b)*(b-a)/(f(b)-f(a));
}
关于c语言迭代法求方程根和迭代法求解方程的根的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。