Docsity

Numerical Solutions to Ordinary Differential Equations: Euler Method and Taylor Method - P, Assignments of Computer Science

Toggle sidebar
bg1
Numerical Computing S’09 MATH/CSCI 4800-01
Homework-7
Due Tuesday April 28, 2009 by 5 PM
Illegible reports will be returned without grading. Show all work. All Matlab scripts, Matlab output and
plots should be printed out and stapled to the report.
1. (Pencil-and-paper) Text: 6.1 Exercises, page 294, problems 4(c) and 6(c).
(a) The ODE is
y0+ 2y= 4t.
The complementary function is ce−2tand a particular solution is 2t−1,yielding the general
solution y=ce−2t+ 2t−1.The initial condition y(0) = 0 determines cto b e 1. Then the
desired solution to the IVP is
y=e−2t+ 2t−1.
(b) Euler’s method is
yn+1 =yn+hf(tn, yn).
Here, f(t, y) = −2y+ 4twhence
yn+1 =yn+h[−2yn+ 4tn] = (1 −2h)yn+ 4htn.
This is a linear difference equation. It has a particular solution 2tn−1.The homogeneous solution
is yn=crnwith r= 1 −2h. Therefore the general solution is
yn=c(1 −2h)n+ 2tn−1.
The initial condition y0= 0 determines c= 1,so that
yn= (1 −2h)n+ 2tn−1.
When specialized to h= 1/4 the above simplifies to
yn=1
2n+n
2−1
and yields the following:
y1= 0, y2= 0.25, y3= 0.625, y4= 1.0625.
The exact solution at the final time is y(1) = e−2+ 1.Then the (global) error at t= 1 is
y(1) −y4=e−2+ 1 −1.0625 = 0.0728.
2. (Matlab) Text: 6.1 Computer Problems, page 295, problem 5(c).
See attached Matlab Log. Figure 1 displays the plot.
3. (Pencil-and-paper) Text: 6.2 Exercises, page 306, problem 2(c). Repeat the problem by applying the
second-order Taylor method.
The IVP is the same as in problem 1 above, i.e., y0=f(t, y )=4t−2y, y(0) = 0,for which the exact
solution is y=e−2t+ 2t−1.
pf3
pf4
pf5

Partial preview of the text

Download Numerical Solutions to Ordinary Differential Equations: Euler Method and Taylor Method - P and more Assignments Computer Science in PDF only on Docsity!

Numerical Computing S’09 MATH/CSCI 4800- Homework-

Due Tuesday April 28, 2009 by 5 PM

Illegible reports will be returned without grading. Show all work. All Matlab scripts, Matlab output and plots should be printed out and stapled to the report.

  1. (Pencil-and-paper) Text: 6.1 Exercises, page 294, problems 4(c) and 6(c).

(a) The ODE is y′^ + 2y = 4t. The complementary function is ce−^2 t^ and a particular solution is 2t − 1 , yielding the general solution y = ce−^2 t^ + 2t − 1. The initial condition y(0) = 0 determines c to be 1. Then the desired solution to the IVP is y = e−^2 t^ + 2t − 1.

(b) Euler’s method is yn+1 = yn + hf (tn, yn). Here, f (t, y) = − 2 y + 4t whence

yn+1 = yn + h[− 2 yn + 4tn] = (1 − 2 h)yn + 4htn.

This is a linear difference equation. It has a particular solution 2tn − 1. The homogeneous solution is yn = crn^ with r = 1 − 2 h. Therefore the general solution is

yn = c(1 − 2 h)n^ + 2tn − 1.

The initial condition y 0 = 0 determines c = 1, so that

yn = (1 − 2 h)n^ + 2tn − 1.

When specialized to h = 1/4 the above simplifies to

yn =

2 n^

n 2

and yields the following:

y 1 = 0, y 2 = 0. 25 , y 3 = 0. 625 , y 4 = 1. 0625.

The exact solution at the final time is y(1) = e−^2 + 1. Then the (global) error at t = 1 is

y(1) − y 4 = e−^2 + 1 − 1 .0625 = 0. 0728.

  1. (Matlab) Text: 6.1 Computer Problems, page 295, problem 5(c). See attached Matlab Log. Figure 1 displays the plot.
  2. (Pencil-and-paper) Text: 6.2 Exercises, page 306, problem 2(c). Repeat the problem by applying the second-order Taylor method. The IVP is the same as in problem 1 above, i.e., y′^ = f (t, y) = 4t − 2 y, y(0) = 0, for which the exact solution is y = e−^2 t^ + 2t − 1.

0 0.02 0.04 0.06 0.08 0. 0

h

error

Global Error of Euler solution against h

Figure 1: Problem 2, error at t = 1 plotted against h.

(a) The trapezoid method is

yn+1 = yn +

h 2

[

f (tn, yn) + f

tn + h, yn + hf (tn, yn)

)]

Applied to the given IVP, it reduces to

yn+1 = yn(1 − 2 h + 2h^2 ) + 4h(1 − h)tn + 2h^2.

The solution of this difference equation, subject to y 0 = 0, is

yn = (1 − 2 h + 2h^2 )n^ + 2tn − 1.

(The details follow those in problem 1 above.) For h = 1/4 and tn = nh = n/4 it reduces to

yn =

)n

n 2

and yields the following:

y 1 =

= 0. 1250 , y 2 =

= 0. 3906 , y 3 =

= 0. 7441 , y 4 =

The exact solution at the final time t = 1 is y(1) = e−^2 + 1, giving the global error

y(1) − y 4 = e−^2 + 1 − 1 .1526 = − 0. 0173.

(b) The second-order Taylor method is

yn+1 = yn + hfn + h^2 2

[ft + f fy ]n.

For the given ODE, f = 4t − 2 y, ft = 4, fy = − 2 , and the method reduces to

yn+1 = yn + h(4tn − 2 yn) +

h^2 2

[4 − 2(4tn − 2 yn)]

= yn(1 − 2 h + 2h^2 ) + 4h(1 − h)tn + 2h^2.

This is precisely the same as the trapezoid method discussed in part (a) above, and the same results apply. (This agreement of the two methods is a coincidence, and does not hold in general.)

  1. (Pencil-and-paper) Text: 6.3 Exercises, page 318, problem 2(d).

The system of equations is dy dt

= f (t, y), y(0) =

[

]

Here, y =

[

y 1 y 2

]

, f =

[

y 1 + 3y 2 2 y 1 + 2y 2

]

The exact solution is y(t) =

[

3 e−t^ + 2e^4 t − 2 e−t^ + 2e^4 t

]

The trapezoid method, in vector form, is

yn+1 = yn + h 2

[

f (tn, yn) + f

tn + h, yn + hf (tn, yn)

)]

or, writing the equations separately,

y (^1) n+1 = y (^1) n +

h 2

[

f 1 (tn, y (^1) n , y (^2) n ) + f 1 (tn + h, y (^1) n + hf 1

tn, y (^1) n , y (^2) n ), y (^2) n + hf 2 (tn, y (^1) n , y (^2) n )

)]

y (^2) n+1 = y (^2) n +

h 2

[

f 2 (tn, y (^1) n , y (^2) n ) + f 2 (tn + h, y (^1) n + hf 1

tn, y (^1) n , y (^2) n ), y (^2) n + hf 2 (tn, y (^1) n , y (^2) n )

)]

In our case we have f 1 (t, y 1 , y 2 ) = y 1 + 3y 2 , and f 2 (t, y 1 , y 2 ) = 2y 1 + 2y 2. We also have h = 1/ 4. Then the above equations reduce to

y (^1) n+1 = y (^1) n +

[

{y (^1) n + 3y (^2) n } + y (^1) n + (1/4)(y (^1) n + 3y (^2) n ) + 3{y (^2) n + (1/4)(2y (^1) n + 2y (^2) n )}

]

y (^2) n+1 = y (^2) n +

[

{ 2 y (^1) n + 2y (^2) n } + 2{y (^1) n + (1/4)(y (^1) n + 3y (^2) n )} + 2{y (^2) n + (1/4)(2y (^1) n + 2y (^2) n )}

]

The above equations can be used to construct the solutions at times t = 1/ 4 , 1 / 2 , 3 /4 and 1. The results at these four times are given below in column form. [ y 1 y 2

]

[

]

The error at the last time step is found to be [ y 1 (1) − y 14 = 31. 0574 y 2 (1) − y 24 = 31. 0806

]

The computation was done on Matlab; see the attached Log.

  1. (Extra Credit, Pencil-and-paper and Matlab) Text: 6.3 Computer Problems, page 319, problem 6.

Problem 2.

Matlab Log

% Script File P2HW7.m

% Computes solution of y'=4t-2y by progressively reducing

% the step size, and with the Euler method, on the t-interval

% [0,1]

% Exact solution is y=exp(-2t)+2t-1.

clear all

clc

y(1)=0;

% Define exact solution

yexact=exp(-2)+1;

% Compute numerical solution for varying h

for k=0:

h=0.1*2^(-k);

n=1/h;

for j=1:n

t(j)=(j-1)*h;

y(j+1)=y(j)+h(4t(j)-2*y(j));

end

error(5-k+1)=yexact-y(n+1);

end

% Plot error

plot(0.1*2.^(-[5 4 3 2 1 0 ]),error,'--ro');

xlabel('h'); ylabel('error');

title('Global Error of Euler solution against h');

Problem 4.

Matlab Log

% Script File P4HW7.m

% Computes solution of y'=5t^4y by progressively reducing

% the step size, and with the Taylor method of order 2,

% on the t-interval [0,1].

% Exact solution is y=exp(t^5).

clear all

clc

y(1)=1;

% Define exact solution at t=

yexact=exp(1);

% Compute numerical solution for varying h

for k=0:

disp('y1'); disp(y1);

disp('y2'); disp(y2);

disp('error'); disp(error);

Numerical Output

y

y

error