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

📄 newton.cpp

📁 用newton算法去方程的近似解
💻 CPP
字号:
#include<iostream>
#include<math.h>
using namespace std;
double newtonA(double x)
{
	return x*x-2*x*exp(-x)+exp(-2*x);
}
double newtonAX(double x)
{
	return 2*x-2*exp(-x)+2*x*exp(-x)-2*exp(-2*x);
}
double newtonB(double x)
{
	return cos(x+sqrt(2.0))+x*(x/2+sqrt(2.0));
}
double newtonBX(double x)
{
	return -sin(x+sqrt(2.0))+x+sqrt(2.0);
}
double newtonC(double x)
{
	return x*x*x-3*x*x*pow(2.0,-x)+3*x*pow(4.0,-x)-pow(8.0,-x);
}
double newtonCX(double x)
{
	return 3*x*x-6*x*pow(2.0,-x)+3*x*x*pow(2.0,-x)*log(2.0)
		+3*pow(4.0,-x)-3*x*pow(4.0,-x)*log(4.0)
		+pow(8.0,-x)*log(8.0);
}
double newtonD(double x)
{
	return exp(6*x)+3*log(2.0)*log(2.0)*exp(2*x)-log(8.0)*exp(4*x)-log(2.0)*log(2.0)*log(2.0);
}
double newtonDX(double x)
{
	return 6*exp(6*x)+6*log(2.0)*log(2.0)*exp(2*x)-4*log(8.0)*exp(4*x);
}
int main()
{
	double p0=0.0,px=0.0;
	int N=1000;
	int i=0;
	/*
	p0=0.5;
	px=0.5;
	while(i<N)
	{
		px=p0-newtonA(p0)/newtonAX(p0);
		if(px-p0<0.00001&&px-p0>-0.00001)
		{
			cout<<"习题2.4:NO.1_a"<<endl;
			cout<<i+1<<" "<<px<<endl;
			return 0;
		}
		i++;
		p0=px;
	}
	cout<<"wrong answer"<<endl;
	*/
	/*p0=-1.5;
	px=0;
	i=0;
	while(i<N)
	{
		px=p0-newtonB(p0)/newtonBX(p0);
		if(px-p0<0.00001&&px-p0>-0.00001)
		{
			cout<<"NO.1_b"<<endl;
			cout<<i+1<<" "<<px<<endl;
			return 0;
		}
		p0=px;
		i++;
	}
	cout<<"wrong answer"<<endl;
	*/
	/*p0=0.5;
	i=0;
	while(i<N)
	{
		px=p0-newtonC(p0)/newtonCX(p0);
		if(px-p0<0.00001&&px-p0>-0.00001)
		{
			cout<<"NO.1_c"<<endl;
			cout<<i+1<<" "<<px<<endl;
			return 0;
		}
		p0=px;
		i++;
	}
	cout<<"wrong answer"<<endl;
	*/
	p0=-0.5;
	i=0;
	while(i<N)
	{
		px=p0-newtonD(p0)/newtonDX(p0);
		if(px-p0<0.00001&&px-p0>-0.00001)
		{
			cout<<"NO.1_d"<<endl;
			cout<<i+1<<" "<<px<<endl;
			return 0;
		}
		p0=px;
		i++;
	}
	cout<<"wrong answer"<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -