c.txt
来自「数值计算中用的牛顿迭代法解方程的算法」· 文本 代码 · 共 54 行
TXT
54 行
#include<stdio.h>
#include<math.h>
#include<conio.h>
float f(float x)
...{
return 2*x*x*x-4*x*x+3*x-6;
}
float fd(float x)
...{
return 6*x*x-8*x+3;
}
int main(void)
...{
float x0=1.5,x=1.5;
do...{
x0=x;
x=x0-f(x0)/fd(x0);//迭代公式求近似根;
}while(fabs(x-x0)>1e-4);
printf("the asymtomatic root is %f ",x0);
getch();
return 0;
}
/**//*2.000005*/二分法:
#include <stdio.h>
#include <math.h>
#include <conio.h>
float f(float x)
...{
return 2*x*x*x-4*x*x+3*x-6;
}
int main(void)
...{
float l=-10,r=10,root,mid;
while(fabs(l-r)>1e-4)...{
mid=(l+r)/2;
if(!f(mid))...{
root=mid;
break;
}if(f(l)*f(mid)<0)
r=mid;
else
l=mid;
}
root=mid;
printf("the only one root is %f ",root);
getch();
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?