%% Section 6.2- The Runge-Kutta Method %% % In Problem Set C #16 I have asked you to implement the fourth order % Runge-Kutta method in MATLAB, which I'll refer to as the Runge-Kutta % method. Here I use my implementation, which I called *rungekutta*, to % find a numerical solution of % % $$y' = 2y -1, \quad y(0) = 1.$$ % % *rungekutta* takes the same input as *myeuler*. f = @(t,y) 2*y - 1; %% % First I use step size 0.1. [t1,y1] = rungekutta(f, 0, 1, 0.4, 4); [t1 y1] %% % Next I use step size 0.05. [t2,y2] = rungekutta(f, 0, 1, 0.4, 8); [t2 y2] %% % I will make a table of values at t = 0, 0.1,...,0.4. I will include % the exact solution, which is syms t y = dsolve('Dy = 2*y - 1, y(0) = 1', 't') %% % I will also include the approximate solution I had obtained with % Euler's method using step size 0.025. [ta,ya] = myeuler(f, 0, 1, 0.4, 16); ya %% % Here is the table. The first column is t, the second ya, the third y1, % the fourth y2 and the last y. T = [ ] for k = 0:4 digits(9) A = [vpa(t1(k+1)),vpa(ya(4*k+1)),vpa(y1(k+1)),vpa(y2(2*k+1)),... vpa(subs(y,'t',t1(k+1)))]; T = [T;A]; end [sym('t1'),sym('ya'),sym('y1'),sym('y2'),sym('y');T] %% % Here is a table of the errors. The second column is the error with % Euler's method, the third is the Runge-Kutta method with step size 0.1 % and the fourth is the Runge-Kutta method with step size 0.05. T = [ ] for k = 0:4 digits(9) A = [vpa(t1(k+1)),vpa(subs(y,'t',t1(k+1)))-vpa(ya(4*k+1)),... vpa(subs(y,'t',t1(k+1)))-vpa(y1(k+1)),... vpa(subs(y,'t',t1(k+1)))-vpa(y2(2*k+1))]; T = [T;A]; end [sym('t1'),sym('Ea'),sym('E1'),... sym('E2');T] %% % I see that with step size 0.1 the Runge-Kutta method gives errors that % are less than $(1.01)10^{-5}$. With step size 0.05 it gives errors that % are less than $10^{-6}$. With step size 0.025, Euler's method gives an % error greater than 0.02 at t=0.4.