c6.cpp
资源名称:数值分析课程设计.zip [点击查看]
上传用户:zhdd911129
上传日期:2007-05-11
资源大小:722k
文件大小:1k
源码类别:
matlab例程
开发平台:
Matlab
- //C6.cpp
- //Adanved Euler's Methom
- #include<iostream.h>
- const N=20;
- double f(double x,double y)
- {
- return x*y*y*(-1);
- }
- //n为[0,5]之间欲求离散点个数,将会设为20
- void EulerMethom(double x0,double y0,int n)
- {
- double y1,h;
- int i;
- h=5/(double)n;
- for (i=0;i<n;i++)
- {
- y1=y0+h*f(x0+i*h,y0);
- y1=y0+h*(f(x0+(i+1)*h,y1)+f(x0+i*h,y0))/2;
- cout<<"The vaule at "<<(i+1)*h<<"is: "<<y1<<endl;
- y0=y1;
- }
- }
- void main()
- {
- double x0=0,y0=2;
- EulerMethom(x0,y0,N);
- }
- //N=500时,y(2.5)=0.275871
- //N=20时,y(2.5)=0.282357,距精确值0.275862相当远
- //迭代次数对结果精确性影响非常大,N=500时,程序耗时2秒多
- //且有相当的误差,Euler法确实不敢恭维