⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newton.cpp

📁 运筹学中牛顿法和黄金分割法一位搜索的算法C++实现
💻 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 + -