📄 非线性方程的跟的求解.cpp
字号:
//////////////////////////////////////////////////////////////////////////////////////////
#include"myhead.h"
///////////////////////////////////////////////////////////////////////////////////////////
double fun_1_1(double);
double fun_1_2(double);
///////////////////////////////////////////////////////////////////////////////////////////
void fun_1()
{
system("cls");//执行系统清屏命令
double a,b;
cout<<" ******************************************************************"<<endl
<<" * 求解非线性方程的根 *"<<endl
<<" ******************************************************************"<<endl
<<" * 1、二分法: *"<<endl
<<" * 2、牛顿法: *"<<endl
<<" ******************************************************************"<<endl;
cout<<" 请选择一个数字(1-2):";
int ch=0;
cin>>ch;
if(ch==1)
{
double c;
double x,y;
cout<<" 请输入误差限:";
cin>>c;
cout<<" 现在进行根的搜索,请输入两个值";
cin>>a>>b;
cout<<setiosflags(ios::fixed)<<setprecision(7);
cout<<" a b"<<endl;
cout<<" "<<a<<" "<<b<<endl;
x=(a+b)/2;
double h=fun_1_1(x);
if(h<0)
{
y=b-x;
a=x;
}
else
{
y=x-a;
b=x;
}
while(y>c)
{
cout<<" "<<a<<" "<<b<<endl;
x=(a+b)/2;
h=fun_1_1(x);
if(h<0)
{
y=b-x;
a=x;
}
else
{
y=x-a;
b=x;
}
}
cout<<" 方程的根为:"<<x<<endl;
}
if(ch==2)
{
double c;
cout<<" 请输入误差限:";
cin>>c;
cout<<" 现在进行根的搜索,请输入两个值";
cin>>a>>b;
double x1=(a+b)/2;
double x2=x1-fun_1_1(x1)/fun_1_2(x1);
cout<<" x1 x2 "<<endl;
cout<<" "<<x1<<" "<<x2<<" "<<endl;
while(x2-x1>c||x2-x1<0-c)
{
x1=x2;
x2=x1-fun_1_1(x1)/fun_1_2(x1);
cout<<" "<<x1<<" "<<x2<<" "<<endl;
}
cout<<" 方程的根为:"<<x2<<endl;
}
system("pause");
}
///////////////////////////////////////////////////////////////////////////////////
double fun_1_1(double x)
{
return x*x*x+x*x-3*x-3;
}
//////////////////////////////////////////////////////////////////////////////////////
double fun_1_2(double x)
{
return 3*x*x+2*x-3;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -