非线性方程组的数值解法:区间二分法/迭代法/牛顿法/单弦点法/双弦点法

 //1.区间二分法
#include <iostream.h>
#include <math.h>
void main()
{  float f(float x);
   float a=3,b=4,y1,y2,y0;
   do{y1=f(a);
      y2=f(b);
   y0=f((a+b)/2);
   if(y0==0) cout<<"x="<<(a+b)/2<<endl;
      else if(y1*y0<0) b=(a+b)/2;
      else if(y2*y0<0) a=(a+b)/2;
     }while(fabs(b-a)>0.001);
   cout<<"x="<<(a+b)/2<<endl;
}  
float f(float x)
{ float y;
  y=x*x*x-2*x*x-4*x-7;
  return y;
}

****************************************************
//2迭代法
#include <iostream.h>
#include <math.h>
double f(double x);
main()
{
  double x0=1.0,x;
  x=f(x0);
  cout<<"x0="<<x0<<endl<<"f(x0)="<<x<<endl;
  do
  {
   x=f(x);
   if(x>0.00001)break;//为什么加了这条语句就才行。不加就是死循环呢?
  }while(x>0.00001);//这个不是有个条件了吗???
  cout<<"x="<<x<<endl;
  return 0;
}
double f(double x)
{double y;
 y=pow((x+0.2),0.2);
 return y;
}
 
为什么x0的初值不一样,答案也不一样??????
************************************************
//3.牛顿法
#include <iostream.h>
#include <math.h>
void main()
{  float f(float x);
   float f1(float x);
   float x0=2.0,x1;
   int k=1;
   do
   {   //cout<<f(x0)<<"--"<<f1(x0)<<endl;
    if(f1(x0)==0)  {cout<<"奇异"<<endl;break;}
       else x1=x0-f(x0)/f1(x0);
       if(fabs(x1-x0)<0.0001) {cout<<"x="<<x1<<endl;break;}
    else x0=x1;k++;
   }while(k<5); 
}
float f(float x)
{   float y;
 y=x*x*x-3*x-1;
 return y;
}
float f1(float x)
{   float y;
 y=3*x*x-3;
 return y;
}
 
***************************************
//单弦点法
#include <iostream.h>
#include <math.h>
void main()
{ float f(float x);
  float f1(float x);
  float x0=1.5,x1=1,x2,y,y1,y2;
  int k=1;
  y=f(x0);y1=f(x1);
  cout<<"f(x0)="<<y<<"  "<<"f(x1)="<<y1<<endl;
  for(;k<10;k++)
  {  x2=x1-f(x1)*(x1-x0)/(f(x1)-f(x0));
     y2=f(x2);
     if(f(x2)==0) {cout<<"x="<<x2<<endl;break;}
     else {y1=y2;x1=x2;}
  }
  cout<<"x="<<x2<<endl;
}
float f(float x)
{float y;
 y=x*x*x-2*x*x+10*x-2;
 return y;
}
//双弦点法
#include <iostream.h>
#include <math.h>
void main()
{ float f(float x);
  float xpoint(float x0,float x1);
  float x0=1.5,x1=1,x2,y,y1,y2;
  int k=1;
  y=f(x0);y1=f(x1);
  cout<<"f(x0)="<<y<<"  "<<"f(x1)="<<y1<<endl;
  for(;k<10;k++)
  {  x2=xpoint(x0,x1);
     y2=f(x2);
     if(fabs(f(x2))<0.000001) {cout<<"x="<<x2<<endl;break;}
     else
  {
      y=y1;y1=y2;
   x0=x1;x1=x2;
  }
  }
}
float f(float x)
{float y;
 y=x*x*x-2*x*x+10*x-2;
 return y;
}
float xpoint(float x0,float x1)
{float y;
 y=x1-f(x1)*(x1-x0)/(f(x1)-f(x0));
 return y;
}

 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/cinderellajoan/archive/2008/12/20/3561050.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值