% % Written by: % -- % John L. Weatherwax 2009-04-21 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % %----- close all; clc; clear all; addpath('../../BookCode'); fprime = @(t,y) ( 2*t*y ); y0 = 2; y_truth = @(t) ( 2*exp(t.^2) ); [tvals, yvals] = rkgen( fprime, [0,2], y0, 0.2, 1 ); v_1 = yvals(end); relative_error = ( yvals - y_truth(tvals) ) ./ y_truth(tvals); ph_rk_classical = plot( tvals, relative_error, '-r' ); hold('on'); [tvals, yvals] = rkgen( fprime, [0,2], y0, 0.2, 2 ); v_2 = yvals(end); relative_error = ( yvals - y_truth(tvals) ) ./ y_truth(tvals); ph_rk_butcher = plot( tvals, relative_error, '-g' ); [tvals, yvals] = rkgen( fprime, [0,2], y0, 0.2, 3 ); v_3 = yvals(end); relative_error = ( yvals - y_truth(tvals) ) ./ y_truth(tvals); ph_rk_merson = plot( tvals, relative_error, '-b' ); [ v_1, v_2, v_3, y_truth(2) ] legend( [ph_rk_classical,ph_rk_butcher,ph_rk_merson], 'RK classic', 'RK Butcher', 'RK merson', 'location', 'southwest' ); xlabel('time'); ylabel('y(t)'); title('relative error of various RK methods') grid('on'); %saveas( gcf, '../../WriteUp/Graphics/Chapter5/c_5_p_2_plot.eps', 'epsc' );