1 / 6100%
Lab 3 - Haonan Yu - MAT 275
Table of Contents
MAT 275 MATLAB LAB 3 NAME: Haonan Yu ..................................................................... 1
LAB DAY and TIME:F 1:30 PM - 2:20 PM ........................................................................... 1
Instructor:Salinas ................................................................................................................. 1
Exercise 1 .......................................................................................................................... 1
Exercise 2 .......................................................................................................................... 2
Exercise 3 .......................................................................................................................... 4
Exercise 4 .......................................................................................................................... 4
Exercise 5 .......................................................................................................................... 5
MAT 275 MATLAB LAB 3 NAME: Haonan Yu
LAB DAY and TIME:F 1:30 PM - 2:20 PM
Instructor:Salinas
Exercise 1
part a
f=@(t,y)(3*y);
t=linspace(0,.5,100) ; % defines the exact solution of the ODE
y=2*exp(3*t);
[t5,y5]=euler(f,[0,.5],2,5); % value 5 steps
[t50,y50]=euler(f,[0,.5],2,50); % value 50 steps
[t500,y500]=euler(f,[0,.5],2,500); % value 500 steps
[t5000,y5000]=euler(f,[0,.5],2,5000); % value 5000 steps
e5 = y(end) - y5(end); % error at 5step
e50 = y(end) - y50(end); % error at 50step
e500 = y(end) - y500(end); % error 500step
e5000 = y(end) - y5000(end); % error 5000 step
ratio2 = e5/e50; % Ratio in step 5
ratio3 = e50/e500;% Ratio in step 50
ratio4 = e500/e5000;% Ratio in step 5000
disp('| N | approximation | error | ratio |')
disp('| 5 | 7.4259 | 1.5375 | N/A |')
disp('| 50 | 8.7678 | 0.1956 | 7.8619 |')
disp('| 500 | 8.9433 | 0.0201 | 9.7273 |')
disp('| 5000 | 8.9614 | 0.0020 | 9.9720 |')
%part b
% becasue when the step increase the error will go down the number
will be
1
Lab 3 - Haonan Yu - MAT 275
% close to real number.
%part c
%Euler method use the tangent line so the number will be under
estimate of
%the actual number
| N | approximation | error | ratio |
| 5 | 7.4259 | 1.5375 | N/A |
| 50 | 8.7678 | 0.1956 | 7.8619 |
| 500 | 8.9433 | 0.0201 | 9.7273 |
| 5000 | 8.9614 | 0.0020 | 9.9720 |
Exercise 2
part a
t=0:.3:5; y = -20:2:25;% 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= -3*Y;% dy = -3*y; this is the ODE
quiver(T,Y,dT,dY)% draw arrows (t,y)->(t+dt, t+dy)
axis tight% adjust look
hold on
% Part (b)
t=linspace(0,5,100); %determine t is from 0 to 5 100 velue
y=2*exp(-3.*t); %determine y
plot(t,y,'k','linewidth',2) %plot the graph
% Part (c)
f=@(t,y)(-3*y);
[t6,y6]=euler(f,[0,5],2,6);
plot(t6,y6,'ro-','linewidth',2)%plot the graph
hold off
%part d
figure
t=0:.3:5; y = -1:.4:2; % 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= -3*Y; % dy = -3*y; this is the ODE
quiver(T,Y,dT,dY)% draw arrows (t,y)->(t+dt, t+dy)
axis tight
hold on
t=(0:.3:5);
y=2*exp(-3.*t);%determine y
plot(t,y,'k','linewidth',2)%plot the graph
f=@(t,y)(-3*y);
[t12,y12]=euler(f,[0,5],3,12);
plot(t12,y12,'ro-','linewidth',2)
hold off
2
44444
4444444
14444443
Hd
44
3
os
—
_
>
—
Fs
,
oN
™!,
|
ed
OP.
x
Fs
6°
>
™=
;
—
FF
,
oN
FF
Ls
?.
~~
:
a
al
oO.
™
~
L
—F
Fr
6°
~
SF
Ls
,
NS
;
FL
9.
~
=]
—
Fs
os
9
~~
;
SF
_
,o™%
~
— ~
a
~
FS
FEEEEG
”
—
~~
FEEEEY
©
ed
~
FEE
EEG
a
=
|
bE
EE
ER
©
Lo
of
i
(a
eS
Ww
eS
ir
i
ml Le
—
Le
eS
3
7
7
a
Oi
-
oS
o
4.5
3.5
Lab 3 - Haonan Yu - MAT 275
3
Lab 3 - Haonan Yu - MAT 275
Exercise 3
type 'impeuler.m'
f=@(t,y)(3*y);
[t5,y5] = impeuler(f,[0,.5],2,5);
[t5,y5]
function [tn,yn] = impeuler(f,tspan,y0,N)
p = (tspan(2)-tspan(1))/N;
m = tspan(1);
tn = m;
y = y0(:);
yn = y.';
for n=1:N
f1 = feval(f,m,y);
f2 = feval(f,m+p,y+p*f1);
y = y+p*(f1+f2)/2;
m = m+p;
yn = [yn; y.']; tn = [tn; m];
end
ans =
0 2.0000
0.1000 2.6900
0.2000 3.6181
0.3000 4.8663
0.4000 6.5451
0.5000 8.8032
Exercise 4
f=@(t,y)(3*y);
t=linspace(0,.5,100) ; y=2*exp(3*t);
[t5,y5]=impeuler(f,[0,.5],2,5);
[t50,y50]=impeuler(f,[0,.5],2,50);
[t500,y500]=impeuler(f,[0,.5],2,500);
[t5000,y5000]=impeuler(f,[0,.5],2,5000);
e5 = y(end) - y5(end);
e50 = y(end) - y50(end);
e500 = y(end) - y500(end);
e5000 = y(end) - y5000(end);
ratio2 = e5/e50;
ratio3 = e50/e500;
ratio4 = e500/e5000;
disp('| N | approximation | error | ratio |')
disp('| 5 | 8.8032 | 0.1602 | N/A |')
disp('| 50 | 8.9614 | 0.0200 | 81.229 |')
disp('| 500 | 8.9634 | 0.000002 | 97.987 |')
4
Lab 3 - Haonan Yu - MAT 275
disp('| 5000 | 8.9634 | 0.000002 | 99.798 |')
| N | approximation | error | ratio |
| 5 | 8.8032 | 0.1602 | N/A |
| 50 | 8.9614 | 0.0200 | 81.229 |
| 500 | 8.9634 | 0.000002 | 97.987 |
| 5000 | 8.9634 | 0.000002 | 99.798 |
Exercise 5
part a
t=0:.3:5; y = -20:2:25;% 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= -3*Y;% dy = -3*y; this is the ODE
quiver(T,Y,dT,dY)% draw arrows (t,y)->(t+dt, t+dy)
axis tight% adjust look
hold on
% Part (b)
t=linspace(0,5,100); %determine t is from 0 to 5 100 velue
y=2*exp(-3.*t); %determine y
plot(t,y,'k','linewidth',2) %plot the graph
% Part (c)
f=@(t,y)(-3*y);
[t6,y6]=impeuler(f,[0,5],2,6);
plot(t6,y6,'ro-','linewidth',2)%plot the graph
hold off
%part d
figure
t=0:.3:5; y = -1:.4:2; % 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= -3*Y; % dy = -3*y; this is the ODE
quiver(T,Y,dT,dY)% draw arrows (t,y)->(t+dt, t+dy)
axis tight
hold on
t=(0:.3:5);
y=2*exp(-3.*t);%determine y
plot(t,y,'k','linewidth',2)%plot the graph
f=@(t,y)(-3*y);
[t12,y12]=impeuler(f,[0,5],3,12);
plot(t12,y12,'ro-','linewidth',2)
hold off
5
rE
EEEEE
PREEEEE
REEEELG
PEEEERE
FRE
REEE
EEEEE
PREEEEE
REREELE
PREEEEE
PRE
EEEE
eEEEE
bEEEEEE
REE
EEE
REE
EEE
PREEEEH
PEEEEEE
_dddaaga
i
i
—
10F
EEE
ERE
“2
4.5
3.5
2.5
1.50.5
=
\4
YS
o®W
'’
N
ON ON
NON
ON
NOON
Af
#
fF
O5f
ff
f
ff
f
ff
ff ff
f
ff Ff
Ff
EF
fats
t
Peeper
et,
ts
tS
4.5
3.5
2.9
1.5
0.5
Lab 3 - Haonan Yu - MAT 275
6
Students also viewed