Enter password:
T = 1; % terminal time % Euler's method subplot(2,1,1); t_arr = linspace(0,1,100); y_arr = exp(t_arr); plot(t_arr,y_arr,'LineWidth',2) legend("Exact") hold on; for j = 1:3 h = 2^(-j); t_arr = linspace(0,T,floor(T/h)+1); y_arr = linspace(0,T,floor(T/h)+1); y_arr(1) = 1; for i = 1:T/h t = (i-1)*h; y_arr(i+1) = y_arr(i) + h * (y_arr(i)^2 * exp(-t)); end plot(t_arr,y_arr,'LineWidth',1) end title("Euler's method") hold off; % Taylor method of order 2 subplot(2,1,2); t_arr = linspace(0,1,100); y_arr = exp(t_arr); plot(t_arr,y_arr,'LineWidth',2) legend("Exact") hold on; for j = 1:3 h = 2^(-j); t_arr = linspace(0,T,floor(T/h)+1); y_arr = linspace(0,T,floor(T/h)+1); y_arr(1) = 1; for i = 1:T/h t = (i-1)*h; y_arr(i+1) = y_arr(i) + h * (y_arr(i)^2 * exp(-t)) + h^2/2 * (2*y_arr(i)*exp(-t)*y_arr(i)^2*exp(-t) - y_arr(i)^2*exp(-t)); end plot(t_arr,y_arr,'LineWidth',1) end title("Taylor's method order 2") hold off;