MAT 275 – Vaz – Online
%%MAT 275 Matlab Assignment #3
%Exercise 1
clear all
clc
close all
% Part A
f=inline('2*y','t','y');
t = linspace(0,.5,100); y = 3*exp(2*t); % evaluate the exact solution
%Euler Approx.
[t5,y5] = euler(f,[0,.5],3,5); % use @f if defined in separate function
[t50,y50] = euler(f,[0,.5],3,50);
[t500,y500] = euler(f,[0,.5],3,500);
[t5000,y5000] = euler(f,[0,.5],3,5000);
e5=y(end)-y5(end); %error with N=5
e50=y(end)-y50(end);
e500=y(end)-y500(end);
e5000=y(end)-y5000(end);
ratio50=e5/e50;
ratio500=e50/e500;
ratio5000=e500/e5000;
N5 = y5(end);
N50 = y50(end);
N500 = y500(end);
N5000 = y5000(end);
Euler’s Method Table
N Approximation Error Ratio
5 7.4650 0.6899 N/A
50 8.0748 0.0801 8.6148
500 8.1467 0.0081 9.83381
5000 8.1540 8.1534e-04 9.9835
% Part B
%For every step taken in the Euler's method
%the size is reduced by 10, the error ratio
%is also decreases the error ratio by 10,
%proving that the order of the function is
%first order
%Part C
%Euler's method underestimates the actual curve
%because the concavity of the function is facing
%upwards, so Euler's will always under estimate
MAT 275 – Vaz – Online
%% Exercise 2
% Part A
t = 0:.45:10; y = -30:6:42 ; % define grid of values in t and y direction
[T,Y]=meshgrid(t,y); % creates 2d matrices of points in the ty-plane
dT = ones(size(T)); % dt=1 for all points
dY = -2*Y; % dy = -2*y; this is the ODE
quiver(T,Y,dT,dY) % draw arrows (t,y)->(t+dt, t+dy)
title('Slope Field with Euler Approximations and Exact')
axis tight % adjust look
hold on
% Part B
tvalues = linspace(0,200,10); %generates values from 0 to 10 with 200 steps
y = 3*exp(-2*t) %Evalutes y with t values
plot(t,y,'k','linewidth',2) %plots on top of the slope field
% Part C
f=inline('-2*y','t','y');
y = 3*exp(-2*t);
[t8,y8]=euler(f,[0,10],3,8)
plot(t8,y8,'ro-','linewidth',2)
title('Eulers approximation with N=8')
%The number of steps is so few in
%comparison to the exact result,
%that each step becomes larger and larger
%leaing to more significant errors.
% Part D
figure
t = 0:.4:10;
y = -1:0:.4:3; %Setting up slope field again
%But step size has changed to .4 with both values
% ranging from (-1,3)
[T,Y] = meshgrid(t,y);
dT = ones(size(T));
dY=-2*Y
quiver(T,Y,dT,dY)
axis tight
hold on
y = 3*exp(-2*t)
plot(t,y,'k','linewidth',2)
title('Eulers approximation with N=16')
Eulers
approximation
with
N=8
40
20
40
Eulers
approximation
with
N=
25
18
08
MAT 275 – Vaz – Online
MAT 275 – Vaz – Online
%% Exercise 3
clear all;
f = inline('2*y','t','y') %Define the ODE in an inline function form
t = linspace(0,.5,100);
y = 3*exp(2*t);
[t5,y5] = imeuler(f,[0,.5],3,5); %This uses the new file, imeuler.m - Improved
Euler
[t5,y5]
%The improved Euler's method produced an answer of
%8.1081 with only 5 steps, but the regular Euler's
%Method only produced 8.0748 with 50 steps.
%This shows that the improved Euler's method is more
%accurate with much fewer steps
%% Exercise 4
% Part A
[t5,y5]=imeuler(f,[0,.5],3,5); %running the approximations again with improved
Euler's
[t50,y50]=imeuler(f,[0,.5],3,50);
[t500,y500]=imeuler(f,[0,.5],3,500);
[t5000,y5000]=imeuler(f,[0,.5],3,5000);
disp('exact value of y(.5)') %displays the text exact value of y at t=0.5
disp(3*exp(2*.5))
app5=y5(end)%determines the value of improved euler's with 5 steps
app50=y50(end)
app500=y500(end)
app5000=y5000(end)
error5=y(end)-y5(end) %Error in Improved Euler with 5 steps
error50=y(end)-y50(end)
error500=y(end)-y500(end)
error5000=y(end)-y5000(end)
Ratio50=error5/error50
Ratio500=error50/error500
Ratio5000=error500/error5000
Improved Euler’s Method Table
N Approximation Error Ratio
5 8.1081 0.0467 N/A
50 8.1543 5.3555e-04 87.2394
500 8.1548 5.4284e-06 98.6567
5000 8.1548 5.4357e-08 99.8650
% Part B
% For every step taken by the Improved Euler
% Method, the factor increased by 10,
% The error was reduced by a factor of 100
% Hence, 10(step size)^2 = 100(error reduction).
MAT 275 – Vaz – Online
%% Exercise 5
% Part A
clear all;
figure
t = 0:.45:10; y = -30:6:42 ; % define grid of values in t and y direction
[T,Y]=meshgrid(t,y); %creates 2D matricies of points in ty-plane
dT = ones(size(T)); % dt=1 for all points
dY = -2*Y; % dy = -2*y; this is the ODE
quiver(T,Y,dT,dY) % draw arrows
axis tight
title ('Euler Approximation Compared to Exact')
hold on
% Part B
t = linspace(0,10,200);
y=3*exp(-2*t); %Evaluating y
plot(t,y,'k','linewidth',2) %plotting y ontop of slope field
% Part C
f = inline('-2*y','t','y');
y = 3*exp(-2*t);
[t8,y8]=imeuler(f,[0,10],3,8); % Using improved Euler's Method over 1 to 10
plot(t8,y8,'ro-','linewidth',2)
% Part D
figure
t = 0:.4:10; y = -1:0.4:3; %Setting up the slope field but with the step
size .4
[T,Y]=meshgrid(t,y);
dT=ones(size(T));
dY=-2*Y;
quiver(T,Y,dT,dY)
axis tight
hold on
y=3*exp(-2*t);
plot(t,y,'k','linewidth',2);
f=inline('-2*y','t','y');
y=3*exp(-2*t)
[t16,y16]=imeuler(f,[0,10],3,16);
plot(t16,y16,'ro-','linewidth',2)
title('Improved Eulers Approximation with N=16')
%The improved Euler's Method overestimates the values,
%but meets up with the exact values at step 10
%The Original Euler's method underestimates the values
%but meets up with the exact values near step 5
140
120
400
Euler
Approximation
Compared
to
Exact
MAT 275 – Vaz – Online