%Script that demonstrates the Runge-Kutta integration for a first order problem using Matlab. % The problem to be solved is: %y'(t)+2*y(t)=3*exp(-4*t) %Note: this problem has a known exact solution % y(t)=2.5*exp(-2*t)-1.5*exp(-4*t) h=0.1; %h is the time step. t=0:h:4; %initialize time variable. yPrimeStar(1)=1.0; %initial condition (same for approximation). for i=1:length(t)-1, %Set up "for" loop. k1=3*exp(-4*t(i))-2*yPrimeStar(i); %Calculate derivative; ystar=yPrimeStar(i)+h*k1; %Estimate new value of y*(to+h). k2=3*exp(-4*(t(i)+h))-2*ystar; %Calculate derivative at end of step. yPrimeStar(i+1)=yPrimeStar(i)+h*(k1+k2)/2; %Estimate new value of y'*(to+h). end