1、实验三微分方程实验学时:2实验类型:综合/设计实验要求:必修一 实验目的通过本次实验,熟悉求解常微分方程初值问题的有关方法和理论,主要是欧拉法、改进欧拉法、四阶龙格库塔法,学会编制这三种方法的计算程序。体会这三种解法的功能、优缺点及适用场合。二 实验内容本实验将欧拉法、改进欧拉法和经典四阶龙哥库塔法编写为一个类 CDifferential_Equation,调用该类实现一阶微分方程初值问题的求解,并画出解函数的图像。CDifferential_Equation 的框架如下class CDifferential_Equationpublic:CDifferential_Equation(floa
2、t ma,float mb,float mh,float my0);CDifferential_Equation(void);float f(float x, float y);void Euler(void); /欧拉法void Euler_Improved(void); /改进欧拉法void Classical_Forth_Order_Runge_Kutta(void); /经典四阶龙哥库塔法float a,b,h,y0; /分别为区间左端点、区间右端点、步长float *x,*y; /存放各个结点和横、纵坐标int n; /结点个数,构造函数中得到;其中有参数的构造函数为CDiffere
3、ntial_Equation:CDifferential_Equation(float ma,float mb,float mh,float my0)a=ma;b=mb;h=mh;n=(b-a)/h;x=new floatn+1;y=new floatn+1;for(int i=0;i#include using namespace std;double f(double x,double y)return x+y;double *Euler_advanced(double y0,double h,double a,double b)int n=(b-a)/h;double *x=new do
4、ublen+1;double *y=new doublen+1;for(int i=0;i#include using namespace std;double f(double x,double y)return x+y;double *Euler_advanced(double y0,double h,double a,double b)int n=(b-a)/h;double *x=new doublen+1;double *y=new doublen+1;for(int i=0;i#include using namespace std;double f(double x,double
5、 y)return x+y;double *Runge_Kutta(double y0,double h,double a,double b)int n=(b-a)/h;double *x=new doublen+1;double *y=new doublen+1;for(int i=0;in;i+)xi=a+i*h;y0=y0;for(int i=1;in;i+)double K1=f(xi-1,yi-1);double K2=f(xi-1+h/2,yi-1+(h/2)*K1);double K3=f(xi-1+h/2,yi-1+(h/2)*K2);double K4=f(xi,yi-1+h*K3);yi=yi-1+(h/6)*(K1+2*K2+2*K3+K4);for(int i=1;in;i+)coutyin;system(“PAUSE“);return y;void main()Runge_Kutta(1,0.1,0,1);六.实验运行结果