?? newton.cpp
字號:
//Newton法求根程序
#include <math.h>
#include <iostream.h>
//Function
double f(double x)
{
double y1,y2,y3;
y1=x*exp(x)-1;
y2=x*x*x-x-1;
y3=(x-1)*(x-1)*(2*x-1);
return(y3);
}
double fd(double x)
{
double y1,y2,y3;
y1=exp(x)+x*exp(x);
y2=3*x*x-1;
y3=2*(x-1)*(2*x-1)+2*(x-1)*(x-1);
return(y3);
}
void main()
{
double x0,x1;
double root;
double e1,e2;
int N,k;
cout<<"歡迎使Newton求根法程序"<<endl;
l1: cout<<"請輸入初始值--->x0"<<endl;
cin>>x0;
cout<<"請輸入限制奇異標志--->e1"<<endl;
cin>>e1;
cout<<"請輸入計算精度--->e2"<<endl;
cin>>e2;
cout<<"請輸入限制跌代次數--->N"<<endl;
cin>>N;
for(k=1;k<=N;k++)
{
cout<<x0<<endl;
if(fd(x0)<e1)
cout<<"ERROR,奇異方程,無法求解,難道是傳說中的XX在世,本程序無法處理"<<endl;
x1=x0-f(x0)/fd(x0);
if(fabs(x1-x0)<e2)
goto l2;
x0=x1;
}
if(k>N)
{cout<<"FAILED:選擇初始值錯誤請重新選擇初始值"<<endl;
goto l1;
}
l2: root=x1;
cout<<"ROOT="<<root<<endl;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -