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

📄 newton.cpp

📁 牛顿法方程求根的经典算法
💻 CPP
字号:
//解方程x^2-a=0
#include "math.h"
#include "iostream.h"
#include "stdio.h"
double Fangcheng(double x);
double Daohanshu(double x);
const a=3;
void main()
{
	double x0,x1,e;
	cout<<"输入初值X0:";
	cin>>x0;
	cout<<"输入误差限e:";
	cin>>e;
	x1=x0-Fangcheng(x0)/Daohanshu(x0);
	while(fabs(x1-x0)>=e)
	{
		x0=x1;
		//printf("x=%f",x0);
		x1=x0-Fangcheng(x0)/Daohanshu(x0);
		//printf("=%20.7f",x1);
		cout<<"中间结果为"<<x1<<endl;
     }
	//cout<<x1<<endl;
	printf("%20.7f",x1);
}
double Fangcheng(double x)
{
	return x*x-a;
}
double Daohanshu(double x)
{
	return 2*x;
}

⌨️ 快捷键说明

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