采用加分(30以上)!牛顿迭代法求解xe^x-1=0,用c++源程序写!

只要主要原理就可以了

程序第一次运行较慢,等几秒钟!
#include<iostream.h>
#include<math.h>
double function(double x)
{
double y=x*exp(x)-1;
return y;
}
double function1(double x,double y)
{
double x1=x-y/((1+x)*exp(x));
return x1;
}
void main()
{
double x,y;
int n(0);
cout<<"请输入初值x0:";
cin>>x;
cout<<"正在计算,稍等!"<<endl;
while(1)
{
n++;
y=function(x);
cout<<"第"<<n<<"次迭代运行的结果y="<<y<<endl;
if(fabs(y)<=1e-15){
cout<<"y的绝对值近似为0,精度1e-16."<<endl;
break;
}
else x=function1(x,y);
}
cout<<"函数x*exp(x)-1=0的根是:"<<x<<endl;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-07-20
#include <iostream>
#include <cmath>
using namespace std;
static k=0;
static int count=1;

double f(double x)
{
return (x*exp(x)-1);//返回f(x)=xe^x-1的值

}
int main()
{
double x0,x1,x2;
double e=0.00001;//精度
cout<<"请输入任意两个数作为初值,X0<X1"<<endl;
cin>>x0>>x1;

x2=x1-f(x1)*(x1-x0)/(f(x1)-f(x0));
while(x2-x1>e||x1-x2>e)
{
x0=x1;
x1=x2;
x2=x1-f(x1)*(x1-x0)/(f(x1)-f(x0));
count+=1;
}
cout<<"方程的根为:"<<x2<<endl;
cout<<"迭代的次数为:"<<count<<endl;
return 0;
}
可以输入任意两个数作为初值,X1>X0
原理,只有一条: x2=x1-f(x1)*(x1-x0)/(f(x1)-f(x0));
我相信您书上已经说得很清楚本回答被提问者采纳
第2个回答  2009-07-20
解释一下什么是牛顿迭代法呗?很久不用,记不得几个名字了
相似回答