#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;
double f(double x)
{
return exp(2*x)+x-4;
}
int main()
{
double a=0,b=1,x;
int count=1;
do
{
x=b-f(b)*(b-a)/(f(b)-f(a));
printf("%-3d: ",count++);
printf("%-10lf ",x);
cout<<endl;
if(f(a)*f(x)<0)
{
if(abs(a-x)<10e-5)
break;
a=x;
}
else
{
if(abs(b-x)<10e-5)
break;
b=x;
}
}while(1);
return 0;
}
使用迭代法求解方程根
本文介绍了一个使用迭代法求解特定形式方程根的C++程序实例。通过不断迭代逼近的方法寻找方程exp(2*x) + x - 4 = 0的根,并展示了如何使用do...while循环实现这一过程。
1万+

被折叠的 条评论
为什么被折叠?



