【发布时间】:2011-10-07 06:19:57
【问题描述】:
几个小时前我已经发布了关于牛顿方法的问题,我得到了答案并想感谢大家,现在我已经尝试自己实现代码
#include <iostream>
#include <math.h>
using namespace std;
#define h powf(10,-7)
#define PI 180
float funct(float x){
return cos(x)-x;
}
float derivative (float x){
return (( funct(x+h)-funct(x-h))/(2*h));
}
int main(){
float tol=.001;
int N=3;
float p0=PI/4;
float p=0;
int i=1;
while(i<N){
p=p0-(float)funct(p0)/derivative(p0);
if ((p-p0)<tol){
cout<<p<<endl;
break;
}
i=i+1;
p0=p;
if (i>=N){
cout<<"solution not found "<<endl;
break;
}
}
return 0;
}
但是我写了输出“未找到解决方案”,当 n=3 时,在三次迭代后的书中,它找到了像这样的解决方案.7390851332,所以我的问题是我应该改变多小或者我应该如何改变我的代码这样那,得到正确答案吗?
【问题讨论】:
标签: c++ numerical-methods