【算法题】y^7+0.5y=x,给定x求y的牛顿下山法解析
#include<iostream> #include<cmath> using namespace std; #define JD 0.001 #define MAXCount 20000 double Next(double pre,double x){ double ans=0; ans=pre-(pow(pre,7)+0.5*pre-x)/(7*pow(pre,6)+0.5); return ans; } bool Jd(double pre,double x){ double value=(pow(pre,7)+0.5*pre-x); if(abs(value)<JD){ return true; } return false; } bool Wc(double Tyn,double pre,double x){ double value0=(pow(Tyn,7)+0.5*Tyn-x); double value=(pow(pre,7)+0.5*pre-x); if(abs(value0)<abs(value)){ return true; } return false; } int main() { double x=0; cin>>x; double PYn=0.5*x; double Tyn=PYn; int count=0; while (!Jd(Tyn,x)&&count<MAXCount) { PYn=Tyn; Tyn=Next(Tyn,x); double mark=0.5; if(!Wc(Tyn,PYn,x)){ Tyn=(Tyn-PYn)*mark+PYn; mark/=2; } ++count; } cout<<Tyn; }
#算法题#