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

📄 erfenfa.cpp

📁 数值分析中的用于解方程的二分法
💻 CPP
字号:
// erfenfa.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream.h"
#include "math.h"

int main(int argc, char* argv[])
{
	double x,a,b,c,ee;
	double fa,fc,fb;
	ee=0.000005;
//	y=pow(x,3)-1;
	cout<<"请输入a的值:";
	cin>>a;
	cout<<"请输入b的值:";
	cin>>b;

	x=a;
	fa=sin(x)-x*x/2;
	x=b;
	fb=sin(x)-x*x/2;
	if (fa*fb>=0)
		cout<<"方程在[a b]内没有实根"<<endl;
	else
	{
	while (1)
	{
		c=(a+b)/2.0;
		x=c;
		fc=sin(x)-x*x/2;		
		x=a;
        fa=sin(x)-x*x/2;
        if ((fabs(fc-0.0))<ee)
			break;
		else
			if ((fa*fc)>0)
				a=c;
			else
				b=c;
	}
	}

	cout<<"方程的根为"<<c<<endl;    
	return 0;
}

⌨️ 快捷键说明

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