Beruflich Dokumente
Kultur Dokumente
Exercise 1
(a)
clc
f=inline('2*y','t','y');
t=linspace(0,.5,100); y=3*exp(2*t); % defines the exact solution of the ODE
[t5,y5]=euler(f,[0,.5],3, 5); % solves the ODE using Euler with 5 steps
[t50,y50]=euler(f,[0,.5],3, 50); % solves the ODE using Euler with 50 steps
%[t50,y50]
[t500,y500]=euler(f,[0,.5],3, 500); % solves the ODE using Euler with 500
steps
%[t500,y500]
[t5000,y5000]=euler(f,[0,.5],3, 5000); % solves the ODE using Euler with 5000
steps
%[t5000,y5000]
e5=y(end)-y5(end)
e50=y(end)-y50(end)
e500=y(end)-y500(end)
e5000=y(end)-y5000(end)
ratio1=e5/e50
ratio2=e5/e500
ratio3=e5/e5000
Ans :
N
approximation
error
ratio
7.465
0.6899
50
8.0748
0.0801
8.6148
500
8.1467
0.0081
9.8381
5000
8.1540
8.1534e-004
9.9835
(b)
e5 = y(end) y5(end) 0.6899 and e50 = y(end) y50(end) 0.0801 result in a ratio e5/e50
8.6148. We clearly see that e50 is (over 8 times) smaller than e5, so that taking smaller step sizes
helps indeed reduce the error. Since the error for Eulers method is proportional to the step size
h (for h sufficiently small), we expect that the ratio of the errors corresponding to two different
step sizes h1 and h2, say, should be equal to the ratio of the step sizes, i.e. e1/e2 = h1/h2 : if e1 =
C.h1 and e2 = C.h2 (where the constant C is independent of h), then e1/e2 = h1/h2 indeed. In our
case the ratio of the step sizes is 10 compared to a ratio of the errors 8.6, suggesting that the
step sizes involved are not yet sufficiently small. If we find the Euler approximations using, for
example, 500 and 5000 steps, y500 and y5000 say, we find e500/e5000 1.9982, almost exactly
equal to the corresponding ratio h1/h2 = (1/500)/ (1/1000) = 2
(c)
We find the Euler approximations for 500 and 5000 steps using the Matlab commands
>> [t500,y500]=euler(f,[0,0.5],3,500) ;
>> [t5000,y5000]=euler(f,[0,0.5],3,5000); the values of the approximations at t = 0.5 are
y500(end) 8.1467 and y5000(end) 8.1540, and we find the corresponding errors using the
commands
>> e500=y(end)y500(end) >> e5000=y(end)y5000(end) We obtain e500 0.0081 and e5000
8.1534e04 = 0.00081534, resulting in a ratio e500/e5000 9.9835, almost exactly equal
to the ratio of the step sizes, namely 10.
Exercise 2
(a)
clc
%(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)
axis tight
hold on
(b)
%(b)
f=inline('-2*y','t','y');
t=linspace(0,10,200); y=3*exp(-2*t); % defines the exact solution of
the ODE
[t50,y50] = euler(f,[0,10],3,50);
plot(t50,y50,'k-','linewidth',2);
(c)
%(c)
[t8,y8] = euler(f,[0,10],3,8);
plot(t8,y8,'ro-','linewidth',2);
Ans :
Here is the direction field picture, including the Euler approximation with N = 8 steps.This small
value of N results in a very poor, oscillatory approximation because the red segments
representing the Euler approximation follow the direction field for too long so that each red
segment ends on a direction line very different from the one where it started. This precludes the
possibility of generating a smooth approximation
(d)
clc
% clear screen
%(a)
t= 0:.4:10; y=-1:0.4:3;
% define grid of values in t and y direction
[T,Y]=meshgrid(t,y)
% creates 2d matrices of points in the typlane
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)
axis tight
hold on
%(b)
f=inline('-2*y','t','y');
t=linspace(0,10,200); y=3*exp(-2*t); % defines the exact solution of
the ODE
[t50,y50] = euler(f,[0,10],3,50);
plot(t50,y50,'k-','linewidth',2);
%(c)
[t16,y16] = euler(f,[0,10],3,16);
plot(t16,y16,'ro-','linewidth',2);
Ans:
Here is the direction field picture, including the Euler approximation with N = 16 steps. This
quite small value of N results in a better than N = 8 approximation because the red segments
representing the Euler approximation follow the direction field is not short enough so that each
red segment ends on a direction line quite similar to the one where it started
Exercise 3
Function Improved Euler
function [t,y] = impeuler(f,tspan,y0,N)
m = length(y0);
t0 = tspan(1);
tf = tspan(2);
h = (tf-t0)/N; % evaluate the time step size
t = linspace(t0,tf,N+1); % create the vector of t values
y = zeros(m,N+1);
% allocate memory for the output y
y(:,1) = y0';
% set initial condition
for n=1:N
y(:,n+1) = y(:,n) +
(h/2)*[f(t(n),y(:,n))+f(t(n)+h,y(:,n)+
(h*f(t(n),y(:,n))))];
% implement Eulers method
end
t = t'; y = y'; % change t and y from row to column vectors
end
Test :
clc
f=inline('2*y','t','y');
[t5,y5] = impeuler(f,[0,.5],3,5);
[t5,y5]
ans =
0
3.0000
0.1000
3.6600
0.2000
4.4652
0.3000
5.4475
0.4000
6.6460
0.5000
8.1081
Comment : This method is more optimal than the one above. Its really improved Eulers
method .
In exercises 4 and 5, we use Improved Eulers method at exercise 3 to solve the problems.
Exercise 4 :
clc
%(a)
f=inline('2*y','t','y');
t=linspace(0,.5,100); y=3*exp(2*t);
[t5,y5] = impeuler(f,[0,.5],3,5);
[t50,y50] = impeuler(f,[0,.5],3,50);
[t500,y500] = impeuler(f,[0,.5],3,500);
[t5000,y5000] = impeuler(f,[0,.5],3,5000);
%[t5,y5]
%[t50,y50]
%[t500,y500]
%[t5000,y5000]
e5=y(end)-y5(end)
e50=y(end)-y50(end)
e500=y(end)-y500(end)
e5000=y(end)-y5000(end)
ratio1=e5/e50
ratio2=e5/e500
ratio3=e5/e5000
Ans
N
approximation
error
ratio
8.1081
0.0467
50
8.1543
5.3555e-004
87.2394
500
8.1548
5.4284e-006
98.6567
5000
8.1548
5.4357e-008
99.8650
(b)
To compute the errors in these approximations we use the Matlab commands:
>> e50 = y(end)y50(end)
>> e500 = y(end)y500(end)
>> e5000 = y(end)y5000(end)
that yield, respectively,
e50 = 5.3555e004,
e500 =5.4284e006,
e5000 =5.4357e008.
The ratios of the errors are computed using the Matlab commands
>> ratio2=e50/e500
>> ratio3=e500/e5000
that give the values
ratio2 = 98.6566
ratio3 = 99.8650.
We see that ratios of the numbers of steps used are both 10 (500/50 and 5,000/500). The ratios of
the errors are almost the square of 10, i.e. 100, though slightly smaller. These ratios indeed seem
to confirm the theoretical result that says that the error in the improved Euler approximation, for
sufficiently small h, is proportional to h2, or, equivalently, the ratio of the errors equals the square
of the ratio of the step sizes: if e1= Ch12 e2= Ch22
(where the constant C is independent of h), then we have e1/e2 = h12/ h22 = (h1/ h2) 2 indeed.
In our case, that is [(1/50)/(1/500)]2 = (500/50) 2 = 102 = 100.
Exercise 5 :
(a) (b) (c)
clc
%(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)
axis tight
hold on
%(b)
f=inline('-2*y','t','y');
t=linspace(0,10,200); y=3*exp(-2*t); % defines the exact solution of
the ODE
[t50,y50] = impeuler(f,[0,10],3,50);
plot(t50,y50,'k-','linewidth',2);
%(c)
[t8,y8] = impeuler(f,[0,10],3,8);
plot(t8,y8,'ro-','linewidth',2);
Ans :
(d)
%(a)
t= 0:.4:10; y=-1:0.4:3;
% 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)
axis tight
hold on
%(b)
f=inline('-2*y','t','y');
t=linspace(0,10,200); y=3*exp(-2*t); % defines the exact solution of
the ODE
[t50,y50] = impeuler(f,[0,10],3,50);
plot(t50,y50,'k-','linewidth',2);
%(c)
[t16,y16] = impeuler(f,[0,10],3,16);
plot(t16,y16,'ro-','linewidth',2);
Ans :
Compare the results with the ones obtained with Eulers method
N=8:
N=16 :
Red line : Improved Eulers method
Blue line : Eulers method