Dev C++ 问题中的不动点迭代
Fixed point iteration in Dev C++ problems
需要有关此代码的帮助。我一直在寻找代码是否有错误,因为cos x - x
的近似解是-1.57
,而不是0.739
。谢谢
double f(double x)
{
double y;
y = cos(x) - x;
return y;
}
int main()
{
double p,p0;
int i=1,N;
cout<<"p0 = ";
cin>>p0;
cout<<"N = ";
cin>>N;
while(i <= N)
{
p = f(p0);
if(fabs(p-p0) < eps)
{
cout<<p<<endl;
break;
}
cout<<"Iteration "<<i<<": p = "<<p<<endl;
i++;
p0 = p;
cout<<"The solution is "<<p<<endl;
if(i>N)
{
cout<<"Solution not found (method diverges)"<<endl;;
break;
}
}
cout<<"The approximated solution is x = "<<p<<" in the iteration "<<i-1<<endl;
system("PAUSE");
return 0;
}
感谢您的帮助!
简单迭代的方法是代入x = F(x)。对于你的等式 x = cos(x).
#include <iostream>
#include <cmath>
using namespace std;
double f(double x)
{
return cos(x);
}
int main()
{
double p,p0=1,eps = 0.001;
int i=1,N=1000;
while(i <= N)
{
p = f(p0);
if(fabs(p-p0) < eps)
{
cout<<p<<endl;
break;
}
cout<<"Iteration "<<i<<": p = "<<p<<endl;
i++;
p0 = p;
cout<<"The solution is "<<p<<endl;
if(i>N)
{
cout<<"Solution not found (method diverges)"<<endl;;
break;
}
}
cout<<"The approximated solution is x = "<<p<<" in the iteration "<<i-1<<endl;
return 0;
}
需要有关此代码的帮助。我一直在寻找代码是否有错误,因为cos x - x
的近似解是-1.57
,而不是0.739
。谢谢
double f(double x)
{
double y;
y = cos(x) - x;
return y;
}
int main()
{
double p,p0;
int i=1,N;
cout<<"p0 = ";
cin>>p0;
cout<<"N = ";
cin>>N;
while(i <= N)
{
p = f(p0);
if(fabs(p-p0) < eps)
{
cout<<p<<endl;
break;
}
cout<<"Iteration "<<i<<": p = "<<p<<endl;
i++;
p0 = p;
cout<<"The solution is "<<p<<endl;
if(i>N)
{
cout<<"Solution not found (method diverges)"<<endl;;
break;
}
}
cout<<"The approximated solution is x = "<<p<<" in the iteration "<<i-1<<endl;
system("PAUSE");
return 0;
}
感谢您的帮助!
简单迭代的方法是代入x = F(x)。对于你的等式 x = cos(x).
#include <iostream>
#include <cmath>
using namespace std;
double f(double x)
{
return cos(x);
}
int main()
{
double p,p0=1,eps = 0.001;
int i=1,N=1000;
while(i <= N)
{
p = f(p0);
if(fabs(p-p0) < eps)
{
cout<<p<<endl;
break;
}
cout<<"Iteration "<<i<<": p = "<<p<<endl;
i++;
p0 = p;
cout<<"The solution is "<<p<<endl;
if(i>N)
{
cout<<"Solution not found (method diverges)"<<endl;;
break;
}
}
cout<<"The approximated solution is x = "<<p<<" in the iteration "<<i-1<<endl;
return 0;
}