方程求根程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
#define PI 3.141592653
double x0,x1,x2,x[20];
double e[20];
double f(double x)
{
return cos(x)-0.5;
}
void print1(int n)
{
int j;
cout<<"Iteration Error e[n+1]/e[n]"<<endl;
for(j=0;j<n;j++)
if(j<n-1)cout<<setw(8)<<j<<" "<<setw(20)<<x[j]-PI/3<<" "<<setw(20)<<e[j+1]/e[j]<<endl;
else cout<<setw(8)<<j<<" "<<setw(20)<<x[j]-PI/3<<" "<<endl<<endl;
}
void print2(int n)
{
int j;
cout<<"Iteration Error e[n+1]/e[n] e[n+1]/e[n]^2"<<endl;
for(j=0;j<n;j++)
if(j<n-1)cout<<setw(8)<<j<<" "<<setw(20)<<x[j]-PI/3<<" "<<setw(20)<<e[j+1]/e[j]<<" "<<setw(20)<<e[j+1]/pow(e[j],2)<<endl;
else cout<<setw(8)<<j<<" "<<setw(20)<<x[j]-PI/3<<" "<<endl<<endl;
}
void print3(int n)
{
int j;
cout<<"Iteration Error e[n+1]/e[n] |e[n+1]|/|e[n]|^1.618"<<endl;
for(j=0;j<n;j++)
if(j<n-1)cout<<setw(8)<<j<<" "<<setw(20)<<x[j]-PI/3<<" "<<setw(20)<<e[j+1]/e[j]<<"
"<<setw(20)<<fabs(e[j+1])/pow(fabs(e[j]),1.618)<<endl;
else cout<<setw(8)<<j<<" "<<setw(20)<<x[j]-PI/3<<" "<<endl<<endl;
}
void Bisection()
{
cout<<"******************result of Bisection**********************"<<endl;
cout<<"初始化x1=0,x2=PI/2"<<endl;
x1=0;x2=PI/2;
int i=0;
while(1)
{
x[i]=(x1+x2)/2;
e[i]=x[i]-PI/3;
if(f(x1)*f(x[i])>0)
x1=x[i];
else if(f(x2)*f(x[i])>0)
x2=x[i];
else
x1=x2=x[i];
i++;
if(16==i)break;
}
print1(i);
}
void linearinterpolation()
{
cout<<"*****************result of linearinterpolation******************"<<endl;
cout<<"初始化x1=0,x2=PI/2"<<endl;
x1=0;x2=PI/2;
int i=0,j;
while(1)
{
x[i]=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
e[i]=x[i]-PI/3;
if(f(x1)*f(x[i])>0)
x1=x[i];
else if(f(x2)*f(x[i])>0)
x2=x[i];
else
x1=x2=x[i];
i++;
if(16==i)break;
}
}
void linearinterpolation()
{
int i=0;
cout<<"**********************************result of Nowton Raphson*********************************"<<endl;
cout<<"初始化x0=PI/2"<<endl;
x0=PI/2;
while(1)
{
x0=x0-(f(x0)/(-sin(x0)));
x[i]=x0;
e[i]=x0-PI/3;
i++;
if(5==i)break;
}
print2(i);
}
void Secant()
{
cout<<"***********************result of Secant**********************"<<endl;
cout<<"初始化x0=0,x1=PI/2"<<endl;
x0=0;x1=PI/2;
int i=0;
while(1)
{
if(0==i%2)
{
x0=x1-(x1-x0)*f(x1)/(f(x1)-f(x0));
x[i]=x0;
}
else
{
x1=x0-(x0-x1)*f(x0)/(f(x0)-f(x1));
x[i]=x1;
}
e[i]=x[i]-PI/3;
i++;
if(i==7)break;
}
}
double g(double x)
{
return x+cos(x)-0.5;
}
void Directiteration1()
{
cout<<"*********************result of Direct iteration1****************"<<endl;
cout<<"初始化x0=0"<<endl;
x0=0;
int i=0;
x0=g(x0);
e[i]=x0-PI/3;
while(1)
{
x[i]=x0;
x0=g(x0);
i++;
e[i]=x0-PI/3;
if(i==16)break;
}
print1(i);
}
double g2(double x)
{
return 2*x*cos(x);
}
void Directiteration2()
{
cout<<"*******************result of Direct iteration2******************"<<endl;
cout<<"初始化x0=PI/3+0.0635232"<<endl;
x0=PI/3+0.0635232;
int i=0;
while(1)
{
x[i]=x0;
e[i]=x0-PI/3;
x0=g2(x0);
i++;
if(i==16)break;
}
print1(i);
}
double h(double x)
{
return 4*x*(x-PI)/pow(PI,2);
}
void Directiteration3()
{
cout<<"*******************result of Direct iteration3******************"<<endl;
cout<<"初始化x0=PI/2"<<endl;
x0=PI/2;
int i=0;
while(1)
{
x0=x0-f(x0)/h(x0);
e[i]=x0-PI/3;
x[i]=x0;
i++;
if(i==10)break;
}
print1(i);
}
int main()
{
int choice;
int tag;
while(1)
{
tag=0;
do{
if(tag!=0)
cout<<"Y our choice is illegal!Please input again!"<<endl;
tag++;
cout<<" =============Menu============="<<endl;
cout<<" || 1.Bisection|| ||"<<endl;
cout<<" || 2.linearinterpolation ||"<<endl;
cout<<" || 3.Nowton Raphson ||"<<endl;
cout<<" || 4.Secant ||"<<endl;
cout<<" || 5.Direct iteration1 ||"<<endl;
cout<<" || 6.Direct iteration2 ||"<<endl;
cout<<" || 7.Direct iteration3 ||"<<endl;
cout<<" || 8.all of seven ||"<<endl;
cout<<" || 9.return ||"<<endl;
cout<<" ==============================="<<endl;
cout<<" Please input your choice[ ]\b\b";
cin>>choice;
cout<<endl<<endl;
}while(choice<1||choice>9);
if(1==choice||8==choice)Bisection();
if(2==choice||8==choice)linearinterpolation();
if(3==choice||8==choice)NowtonRaphson();
if(4==choice||8==choice)Secant();
if(5==choice||8==choice)Directiteration1();
if(6==choice||8==choice)Directiteration2();
if(7==choice||8==choice)Directiteration3();
if(9==choice)exit(0);
}
return 0;
}。