#include
#include
void main()
{
void function1();//搜索法
void function2();//二分法
void function4();//牛顿法
int choice;
printf("请选择求解的方法:\n\t1.搜索法\n\t2.二分法\n\t3.牛顿法\n:");
switch(1)
{
case 1: function1();
case 2: function2();
case 4: function4();
}
}
void function1()//搜索法计算非线性方程的解
{
double expression1(double);
double lpoint=1.0,rpoint=2.0,step=0.0001;
while(expression1(lpoint)<-0.00001)
{
lpoint=lpoint+step;
}
printf("运用搜索法所求结果:%f\n",lpoint);
}
void function2()//二分法计算非线性方程的解
{
double expression1(double);
double lpoint=1,rpoint=2,mpoint;
mpoint=(lpoint+rpoint)/2;
while(fabs(expression1(mpoint))>0.00001)
{
mpoint=(lpoint+rpoint)/2;
if(expression1(lpoint)*expression1(mpoint)<0)
rpoint=mpoint;
else
lpoint=mpoint;
}
printf("运用二分法所求结果:%f\n",mpoint);
}
void function4()//牛顿法计算非线性方程的解
{
double expression1(double);
double expression2(double);
double x=1.5;
while(expression1(x)>0.00001)
{
x=x-expression1(x)/expression2(x);
}
printf("运用牛顿法所求结果:%f\n",x);
}
double expression1(double x)
{
double result;
result=x*x*x-x*x-1;
return result;
}
double expression2(double x)
{
double result;
result=3*x*x-2*x;
return result;
}