Skip to content
Permalink
bfe5886bbf
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
39 lines (34 sloc) 849 Bytes

06_initial_value_ode

Problem 2

Script:

%Part a.
y_analytical = @(t) exp(-t);

%Part b.
dt = 0.01; %step size
a = figure(1);
set(0, 'defaultAxesFontsize', 16)
set(0, 'defaultTextFontsize', 16)
set(0, 'defaultLineLineWidth', 2)
hold on
t = [0:dt:3]';
y_n = zeros(size(t));
y_n(1) = 1; %initial condition
for i = 2:length(t)
	dy = ode2(t, y_n(i-1));
    y_n(i) = y_n(i-1) + dt*dy;
end
plot(t, y_analytical(t), 'k-', t, y_n, 'o-');
title('Analytical vs. Euler')
legend('Analytical', 'Euler', 'Location', 'Northeast')
saveas(a, 'euler.png');

%Part c.
b = figure(2);
y_heun = heun_sol_order1(@(t,y) ode2(t,y), dt, 1, [0 3]);
plot(t, y_analytical(t), 'k-', t, y_heun, 'o-');
title('Analytical vs. Heun')
legend('Analytical', 'Heun', 'Location', 'Northeast')
saveas(b, 'heun.png');

Part a.

Analytical vs. Euler