% Runge-Kutta method for solving the differential equation % y' = t^2-y with y(0)=1 for t in the range from 0 to 2 h = 0.05; % stepsize tstart = 0; % smallest t value tlast = 2; % larest t value ystart = 1; % y(0) =1 y = ystart; t = tstart; outmat = [t,y]; % a place to store results % the main RK loop while(t <= tlast) a = rkf(t,y); b = rkf(t+h/2,y+h*a/2); c = rkf(t+h/2,y+h*b/2); d = rkf(t+h,y+h*c); t = t + h; y = y + h*(a+2*b+2*c+d)/6; outmat = [outmat; t,y]; % save results in a matrix end; disp(outmat) % print out the results on the screen plot(outmat(:,1),outmat(:,2)) % plot a graph of the results grid; title('Runge-Kutta method for solving y'' = t - y^2');