Solving Differential Equations Using MATLAB

Solving differential equations using MATLAB

Write the system of differential equations in a column format, and then solve the system using the Runge-Kutta method.

% Runge-Kutta method to solve differential equations

step1=0.01;

t0=0:step1:10;

y0=[0.5;0.6;0.7];

Len=length(t0);

y_out=zeros(Len,3);

y_out(1,1:3)=y0′;

for m1=2:1:Len

tt0=t0(m1-1);

yy0=(y_out(m1-1,1:3))’;

KK1=step1*function1(tt0,yy0);

KK2=step1*function1(tt0+step1/2,yy0+0.5*KK1);

KK3=step1*function1(tt0+step1/2,yy0+0.5*KK2);

KK4=step1*function1(tt0+step1,yy0+0.5*KK3);

yy0=yy0+(KK1+KK2*2+KK3*2+KK4)/6;

y_out(m1,1:3)=yy0′;

end

figure(1),plot(t0,y_out(:,1));

figure(2),plot(t0,y_out(:,2));

figure(3),plot(t0,y_out(:,3));

function y_out=function1(tt1,yy1)

y_out=zeros(3,1);

y_out(1)=yy1(2)+tt1*0.1;

y_out(2)=yy1(3)+tt1*0.2;

y_out(3)=sin(yy1(1))*0.01+tt1*0.3;

end

Leave a Comment