L2=g(x(i)+0.5*h,y1(i)+0.5*h*K1,y2(i)+0.5*h*L1); K3=f(x(i)+0.5*h,y1(i)+0.5*h*K2,y2(i)+0.5*h*L2); L3=g(x(i)+0.5*h,y1(i)+0.5*h*K2,y2(i)+0.5*h*L2); K4=f(x(i)+h,y1(i)+h*K3,y2(i)+h*L3); L4=g(x(i)+h,y1(i)+h*K3,y2(i)+h*L3); x(i+1)=x(i)+h;
y1(i+1)=y1(i)+h*(1/6)*(K1+2*K2+2*K3+K4); y2(i+1)=y2(i)+h*(1/6)*(L1+2*L2+2*L3+L4); end
disp('[x y1 y2]') [y1 ;y2]' subplot(121)
plot(x,y1,'r-',x,y2,'b--','LineWidth',4) legend('y1','y2');
title('R--K四阶龙格库塔算法下方程组的解'); ylabel('y1曲线 y2曲线') t=0:0.1:10; subplot(122)
plot(t,2*exp(-t)+sin(t),'b--',t,2*exp(-t)+cos(t),'r-.') legend('y1','y2');
title('R--K四阶龙格库塔算法下方程组的解'); ylabel('y1曲线 y2曲线
')
(2)
2y1 y2 2sinx y1
,x [0,10],
y2 998y1 999y2 999cosx 999sinx y1(0) 2
。
y2(0) 3
clc;clear;
f=@(x,y1,y2)(-2*y1+y2+2*sin(x));
g=@(x,y1,y2) (998*y1-999*y2+999*cos(x)-999*sin(x)); h=0.1;