实验7 常微分方程与MATLAB应用

)1(21,12'y x x y x y 。 四、实验要求：

1、用改进的欧拉法（取h=0.05）及四阶R-K 方法（取h=0.1）求解，并输出x i =1+0.1i ，i=0,1,……,10的数值解y i 。

2、利用MATLAB 中数值解法“ode45”与符号解法“dsolve ”求解，并用图形表示各种方法的精度。

)1(ln 1+=x x

y 1、利用改进欧拉法解方程：程序内容为：

fun=@(x,y)x^(-2)-y/x;

h=0.05;

X=1:h:2;

Y(1)=1;

for i=2:21

Y(i)=Y(i-1)+h/2*(fun(X(i-1),Y(i-1))+fun(X(i),Y(i-1))+h*fun(X(i-1),Y(i -1)));

end ;

Y

Y =

Columns 1 through 9

1.0000 0.9989 0.9957 0.9909 0.9848 0.9778 0.9701 0.9618 0.9530

Columns 10 through 18

0.9440 0.9348 0.9254 0.9160 0.9065 0.8971 0.8876 0.8783 0.8690

Columns 19 through 21

0.8598 0.8508 0.8418

x=1:0.05:2;

y=1./x.*(log(x)+1)

y =

Columns 1 through 8

1.0000 0.9988 0.9957 0.9911 0.9853 0.9785 0.9710

0.9630

Columns 9 through 16

0.9546 0.9459 0.9370 0.9279 0.9188 0.9096 0.9004

0.8912

Columns 17 through 21

0.8821 0.8731 0.8641 0.8553 0.8466

fun=@(x,y)x^(-2)-y/x;

h=0.1;

X=1:h:2;

Y(1)=1;

for n=2:11

k1=fun(x(n-1),Y(n-1));

k2=fun(x(n-1)+h/2,Y(n-1)+h/2*k1);

k3=fun(x(n-1)+h/2,Y(n-1)+h/2*k2);

k4=fun(x(n-1)+h,Y(n-1)+h*k3);

Y(n)=Y(n-1)+h/6*(k1+2*k2+2*k3+k4)

end;

Y

Y =

Columns 1 through 9

1.0000 0.9957 0.9853 0.9710 0.9546 0.9370 0.9188 0.9004 0.8821

Columns 10 through 11

0.8641 0.8466

x=1:0.1:2;

y=1./x.*(log(x)+1)

y =

Columns 1 through 9

1.0000 0.9957 0.9853 0.9710 0.9546 0.9370 0.9188 0.9004 0.8821

Columns 10 through 11

0.8641 0.8466

2、MATLAB中数值解法“ode45”为：

[x1,y1] = ode45(@(x,y)x^(-2)-y/x,[1,2],y0);

dsolve('Dy=x^(-2)-y/x','y(1) = 1','x')

ans =

(log(x)+1)/x

[x1,y1] = ode45(@(x,y)x^(-2)-y/x,[1,2],1);

fplot('(log(x)+1)/x',[1,2]);hold on, plot(x1,y1,'ro');