?? newton.cpp
字號:
#include <iostream.h>
float abs(float x);
float f(float t);
float ff(float t);
float fff(float t);
float newton(float t1,float e);
void main()
{
float t1,e,solution;
cout<<"the destiny function is:t*t*t*t-4*t*t*t-6*t*t-16*t+4."<<endl
<<"please the initial value of t"<<endl;
cin>>t1;
cout<<"please input the value of precision:";
cin>>e;
if(e<0)
{
cout<<"the value of precison is invalid,please input again:";
cin>>e;
};
solution=newton(t1,e);
cout<<"solution is:"<<solution<<endl;
cout<<"f(x)="<<f(solution)<<endl
<<"end";
}
float abs(float x)
{
if(x<0)
return x*(-1.0);
else return x;
}
float f(float t)
{
return t*t*t*t-4*t*t*t-6*t*t-16*t+4;
}
float ff(float t)
{
return 4*t*t*t-12*t*t-12*t-16;
}
float fff(float t)
{
return 12*t*t-24*t-12;
}
float newton(float t1,float e)
{
float t2,t3;
t2=t1;
for(;;)
{
if (abs(ff(t2))<e)
return t2;
else
{
if (fff(t2)==0)
{
cout<<"failed!";
break;
}
else
{
t3=t2-ff(t2)/fff(t2);
if(abs(t3-t2)<e)
return t3;
else t2=t3;
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -