yc0119001 发表于:2002-3-27 15:58:37
第二T(1):
steffson ,在VC6.0下通过:
#include <iostream.h>
#include <math.h>
long double log_s( long double a)
{ long double re;
re=log((long double)pow(2.0,a)+(long double)pow(3.0,a))/log(4.0);
return re; }
long double steff(long double b)
{ long double re;
re=b-pow((log_s(b)-b),2)/(log_s(log_s(b))-2*log_s(b)+b);
return re;}
void main()
{ char st;
int n(0);
long double x0(1.5),e(0.000005),x1(0);
for (x1=steff(x0);fabs(x1-x0)>e;n++)
{cout<<"n: "<<n<<" "<<x0<<"\n";
x0=x1;x1=steff(x0);cin>>st;}
cout<<"n: "<<n<<" "<<x0<<endl;
}
补充日期: 2002-03-27 16:01:23
第二T(1):NEWTON:
#include <iostream.h>
#include <math.h>
long double fx( long double a)
{ long double re;
re=pow(2.0,a)+pow(3.0,a)-pow(4.0,a);
return re; }
long double ffx(long double b)
{ long double re;
re=pow(2.0,b)*log(2.0)+pow(3.0,b)*log(3.0)-pow(4.0,b)*log(4.0);
return re;}
void main()
{ char st;//屏幕显示控制
int n(0);
long double x0(1.5),e(0.000005),x1(0);
for (x1=x0-fx(x0)/ffx(x0);fabs(x1-x0)>e;n++)
{cout<<"n: "<<n<<" "<<x0<<"\n";
x0=x1;x1=x0-fx(x0)/ffx(x0);
cin>>st;//屏幕显示控制
}
cout<<"n: "<<n<<" "<<x1<<endl;
}
补充日期: 2002-03-27 16:03:01
第二T(1):弦切法:
#include <iostream.h>
#include <math.h>
long double x2(0);
long double fx( long double a)
{ long double re;
re=pow(2.0,a)+pow(3.0,a)-pow(4.0,a);
return re; }
long double xx(long double x0,long double x1)
{ x2=x1-(x1-x0)*fx(x1)/(fx(x1)-fx(x0));
return x2;}
void main()
{ char st;/*控制屏幕显示*/
int n(0);
long double x0(1.0),e(0.000005),x1(2.0),f2(0);
x2=xx(x0,x1);
for (f2=fx(x2);fabs(f2)>e || fabs(x2-x1)>e;n++)
{cout<<"n: "<<n<<" "<<x2<<"\n";
x0=x1;x1=x2;x2=xx(x0,x1);f2=fx(x2);
cin>>st;/*控制屏幕显示*/}
}
补充日期: 2002-03-27 16:04:58
第二T(2)二分法:
#include <iostream.h>
#include <math.h>
#define PI 3.1415925535897932
long double fx(long double x)
{ return x-tan(x);}
void main()
{ char st;
long double a(PI),b(3*PI/2.0),e(0.5E-5),x(0),n(0);
for( x=(a+b)/2;fabs(b-a)>e;x=(a+b)/2,n++)
{ cout<<"n: "<<n<<" a="<<a<<" , b="<<b<<" , x="<<x<<"\n";
if (fx(x)==0 ) break;
if (fx(a)*fx(x)<0)
b=x;
else
a=x;
cin>>st;
}
if (fx(x)!=0) cout<<"x*=:"<<x<<endl;
cout<<fx(x)<<endl;
}
补充日期: 2002-03-27 16:06:45
第二T:steffson, nwton,弦切法:
好象做不出来,可能是本人水平有限,请哪位老大指点一下!
补充日期: 2002-03-27 16:07:51
第七T(1):
#include <iostream.h>
#include <math.h>
long double fx( long double a)
{ long double re;
re=pow(a,6)-6*pow(a,5)+10*pow(a,4)-32*a
+32;
return re; }
long double ffx(long double b)
{ long double re;
re=6*pow(b,5)-30*pow(b,4)+40*pow(b,3)-32;
return re;}
void main()
{ char st;
int n(0);
long double x0(1.5),e(0.000005),x1(0);
for (x1=x0-fx(x0)/ffx(x0);fabs(x1-x0)>e;n++)
{cout<<"n: "<<n<<" "<<x0<<"\n";
x0=x1;x1=x0-fx(x0)/ffx(x0);
cin>>st;
}
cout<<"n: "<<n<<" "<<x0<<endl;
}
补充日期: 2002-03-27 16:08:50
第七T(2):
#include <iostream.h>
#include <math.h>
long double fx( long double a)
{ long double re;
re=pow(a,6)-6*pow(a,5)+10*pow(a,4)-32*a
+32;
return re; }
long double ffx(long double b)
{ long double re;
re=6*pow(b,5)-30*pow(b,4)+40*pow(b,3)-32;
return re;}
void main()
{ char st;
int n(0),m(4);
long double x0(1.5),e(0.000005),x1(0);
for (x1=x0-m*fx(x0)/ffx(x0);fabs(x1-x0)>e;n++)
{cout<<"n: "<<n<<" "<<x0<<"\n";
x0=x1;x1=x0-m*fx(x0)/ffx(x0);
// cin>>st;
}
cout<<"n: "<<n<<" "<<x1<<endl;
}
补充日期: 2002-03-27 16:09:57
第七T(3):
#include <iostream.h>
#include <math.h>
long double fx( long double a)
{ long double re;
re=pow(a,6)-6*pow(a,5)+10*pow(a,4)-32*a
+32;
return re; }
long double ffx(long double b)
{ long double re;
re=6*pow(b,5)-30*pow(b,4)+40*pow(b,3)-32;
return re;}
long double ff(long double c)
{ long double re;
re=30*pow(c,4)-120*pow(c,3)+120*pow(c,2);
return re;}
void main()
{ char st;
int n(0);
long double x0(1.5),e(0.000005),x1(0);
for (x1=x0-fx(x0)*ffx(x0)/(pow(ffx(x0),2)-fx(x0)*ff(x0));fabs(x1-x0)>e;n++)
{ cout<<"n: "<<n<<" "<<x0<<"\n";
x0=x1;x1=x0-fx(x0)*ffx(x0)/(pow(ffx(x0),2)-fx(x0)*ff(x0));
// cin>>st;
}
cout<<"n: "<<n<<" "<<x1<<endl;
}
补充日期: 2002-03-27 16:12:38
这些程序均在VC6。0上运行通过!但小数位数只有后5位,可能精度不够,请求支援!!!SOS!!!
^^^^^^^
! @ ^ @ !
( _ )
<<<< >>>>
| |
/| |\
who am I ?
[少妇军团] 我知道我错了。。。[詠園⑧訜開] [三件事、三句话、三乐、三不要][有情岁月] 想念[M的员工之家] 还有人认识我吗[詠園⑧訜開] 如果[溧水人家] 哇哈哈..睡前狂发美女..[玩.美.食.客] 【食客】9月22 吃的喝的[心情与脚步之间] 【谢谢版友】50岁生日追记
ID: 03299222 不败的天空 发表于:2002-3-27 17:11:02 第2楼
灵活性不好,有些完全可以用 递规 算法,这样更好
还有如果要用你的算法算另外的线性方程 改动起来很不方便
建议用宏定义,这样只要改动宏定义就可以实现对不同线性方程的求解
cout的输出位数只有6位有效数字
建议采用printf
#include <stdio.h>
补充日期: 2002-03-27 17:13:13
//*************************************
//程序功能:Steffenson加速收敛法 求线性方程的解
//
//作 者:杭永东
//
//日 期: 2002.03.26
//*************************************
#include <math.h>
#include <iostream.h>
#include <stdio.h>
//*************************************
//
//注:更改此宏定义的表达式可以解不同的线性方程
// 根据宏定义要求,x必须以(x)形式出现式中
//
//*************************************
#define fi(x) pow(2.0L,(x))+pow(3.0L,(x))-pow(4.0L,(x))
long double x0,e;
long double Dd(long double x0)
{
long double y=fi(x0);
long double z=fi(y);
long double x1=x0-pow((y-x0),2.0L)/(z-2.0L*y+x0);
if((((x1-x0)>0)? (x1-x0): (x0-x1))<e)
return x1;
return Dd(x1); //递规
}
main()
{
cout<<"Please input x0,e:";
cin>>x0>>e;
printf("\n%.9f",Dd(x0));
}