编写一个子程序NewTon(float x0,float eps,float x1).它的功能是用牛顿迭代法求
f(x)=x^3-2x^2+4x+1在x=0附近的一个实根。牛顿迭代公式为:
x n+1(为下标)=x n -f(x n)/f'(x n)
迭代收敛判据为:|f(x n+1)|<eps (eps为指定的迭代精度,例如1e-6)。若迭代成功,则返回非零值;否则,返回0.
求解编程题牛顿迭代
答案:1 悬赏:20
解决时间 2021-03-14 16:29
- 提问者网友:刪除丶後
- 2021-03-13 18:18
最佳答案
- 二级知识专家网友:情战辞言
- 2021-03-13 19:18
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{
int n=0;
float x0=0;
float xold,xnew;
float eps=1e-6;
float function(float x);
float d2function(float x);
int NewTon(float x0,float eps,float *x1);
system("cls");
xold=x0;
while (n++<200 && !NewTon(xold,eps,&xnew))
xold=xnew;
printf("x=%f\ty=%f\n",xnew,function(xnew));
getch();
return 0;
}
float function(float x)
{
return x*x*x-2*x*x+4*x+1;
}
float d2function(float x)
{
return 3*x*x-4*x+4;
}
int NewTon(float x0,float eps,float *x1)
{
*x1=x0-function(x0)/d2function(x0);
if (fabs(function(*x1))<eps)
return 1;
else
return 0;
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯