Spring 2020

Math 151B: Applied Numerical Methods

Discussion Section 1A

Wonjun Lee



Week2

Tuesday

About HW1 and definitions of local truncation error, consistency, convergency, and stability.

Link to the video

Enter password:

Disucssion Section Note.

Download

Matlab code for Question 3.
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;
            
Trulli
Euler's method vs. Taylor's method of order 2. data1: h=0.5, data2: h=0.25, data3: h=0.125.