Top Banner
AP3114/Computational Methods for Physicists and Materials Engineers 8. Solving differential equations II Nov. 9, 2015
22

AP3114 Lecture WK11 Solving Differential Eqautions II

Dec 12, 2015

Download

Documents

GuoXuanChan

Physics
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: AP3114 Lecture WK11 Solving Differential Eqautions II

AP3114/Computational Methods for Physicists and Materials Engineers

8. Solving differential equations IINov. 9, 2015

Page 2: AP3114 Lecture WK11 Solving Differential Eqautions II

Contents

Finite differences and numerical solutions Finite differences Solution of a first-order ODE using finite

differences - Euler forward method Solution of a first-order ODE using finite

differences - an implicit method

Page 3: AP3114 Lecture WK11 Solving Differential Eqautions II

Finite differences

Example

with a small value of hdf/dx ~

df/dx(x = 2) = 0.23569874791

As h0, the value of the finite difference approximation, (f(x+h)-f(x))/h, approaches that of the derivative, df/dx

Page 4: AP3114 Lecture WK11 Solving Differential Eqautions II
Page 5: AP3114 Lecture WK11 Solving Differential Eqautions II

Finite difference formulas based on Taylor series expansions

Let x = x0+h then x-x0= h

Taylor Expansions

error = O(h)

Page 6: AP3114 Lecture WK11 Solving Differential Eqautions II

Finite difference approximations to the first derivative

Forward difference

Backward difference

Centered difference

Page 7: AP3114 Lecture WK11 Solving Differential Eqautions II

Forward, backward and centered finite difference approximations to the second derivative

Centered difference

Forward difference

Backward difference

Page 8: AP3114 Lecture WK11 Solving Differential Eqautions II

Euler forward method – 1st order ODE

Consider the ordinary differential equation, dy/dx = g(x,y),

subject to the boundary condition, y(x1) = y1.

dy/dx = (y(x+h)-y(x))/h. Forward difference approximation

Define g(x,y)=(y(x+h)-y(x))/h, then

y(x+h) = y(x)+h g(x,y). ⋅ Euler's forward method

(x1,y1) x2= x1+h x3= x2+h (x1, y1), (x2, y2), …, (xn, yn)

yi+1= yi + ∆x g(x⋅ i,yi) Explicit method.

Page 9: AP3114 Lecture WK11 Solving Differential Eqautions II

Example 1(x1,y1) x2= x1+h x3= x2+h (x1, y1), (x2, y2), …, (xn, yn)

yi+1= yi + ∆x g(x⋅ i,yi)

Page 10: AP3114 Lecture WK11 Solving Differential Eqautions II

Example 1

http://help.scilab.org/docs/5.5.1/en_US/plot2d.html

Page 11: AP3114 Lecture WK11 Solving Differential Eqautions II

Euler1 implements the calculation steps outlined in the previous example. The function detects if there is overflowing introduced in the solution and stops the calculation at that point providing the current results.

Euler’s 1st–order MethodEuler1:

Page 12: AP3114 Lecture WK11 Solving Differential Eqautions II

Example 2

Page 13: AP3114 Lecture WK11 Solving Differential Eqautions II

Example 2-- Results

Page 14: AP3114 Lecture WK11 Solving Differential Eqautions II

Example 3

Page 15: AP3114 Lecture WK11 Solving Differential Eqautions II

Implicit Method Solving 1st order ODEOrdinary differential equation, dy/dx = g(x,y),B.C y(x1) = y1.

Centered difference approximation for dy/dx, i.e.

dy/dx = (y(x+h)-y(x-h))/(2*h).

With this approximation the ODE becomes, (y(x+h)-y(x-h))/(2*h) = g(x,y).

In terms of sub-indexed variables, this latter equation can be written as: yi-1+2 ∆x g(x⋅ ⋅ i,yi)-yi+1= 0, ( i = 2,3, ..., n-1 )

where y(x) = yi, y(x+h) = yi+1, y(x-h) = yi-1, and h = ∆x

Page 16: AP3114 Lecture WK11 Solving Differential Eqautions II

Implicit Method Solving 1st order ODE

For example, if n = 5, we have 3 equations:

y1+2 ∆x g(x⋅ ⋅ 2,y2)-y3= 0 y2+2 ∆x g(x⋅ ⋅ 3,y3)-y4= 0 y3+2 ∆x g(x⋅ ⋅ 4,y4)-y5= 0

Since y1 is known (it is the initial condition), there are still 4 unknowns, y2, y3, y4, and y5. We use, for example, the forward difference equation applied to i = 1, i.e.,

(y2-y1)/∆x = g(x1,y1), or

y2-∆x g(x⋅ 1,y1)-y1= 0. The values of xi, and n(or ∆x), can be obtained as in the Euler forward (explicit) solution.

yi-1+2 ∆x g(x⋅ ⋅ i,yi)-yi+1= 0, ( i = 2,3, ..., n-1 )

Page 17: AP3114 Lecture WK11 Solving Differential Eqautions II

ExampleODE: dy/dx = y sin(x)

Initial conditions y(0) = 1, in the interval 0 < x < 5. Use ∆x = 0.5,

dy/dx = (yi+1−yi-1)/(2 ∆x), ⋅

Numerical solution:

yi-1 + 2 ∆x sin(x⋅ ⋅ i) y⋅ i− yi+1 = 0, (i = 2, 3, …, n-1).

y1= 1 (1+∆x sin(x1)) y⋅ 1- y⋅ 2= 0.

dy/dx|x= 1= (y2-y1)/∆x = -y1sin(x1),

Page 18: AP3114 Lecture WK11 Solving Differential Eqautions II

Example

Page 19: AP3114 Lecture WK11 Solving Differential Eqautions II

Example -- ComparisonExact solution: the exact is y(x) = exp(-cos(x))/(cosh(1)-sinh(1)).

Page 20: AP3114 Lecture WK11 Solving Differential Eqautions II

20

HW-9

Due: 9am, Nov. 16, 2015Late policy: 10% off per day

To be submitted online to Canvas:Save all your codes (*.sci files) into one folder to submit; Do use “//” to add comments clearly which question you are referring to, when we execute your codes, answers shall be shown on the screen.

Page 21: AP3114 Lecture WK11 Solving Differential Eqautions II

1. For the following functions approximate the derivative df/dx at x = a with (f(a+h)-f(a))/h using values of h = 0.1, 0.01, 0.001, 0.0001, 0.00001. Plot the error involved in the numerical estimate of the derivative against the value of h.

Note:Record your intermediate results using “Print Screen”Save your results and submit them through your usual way.

Page 22: AP3114 Lecture WK11 Solving Differential Eqautions II

2. Given the ODE, dy/dx = y sin(x), ⋅

and the boundary condition, y(0) = 1,

write a SCILAB function that uses the Euler method to obtain a numerical solution to this ODE in the interval 0 < x < 2.5. Use ∆x = 0.25, 0.1, and 0.05. The exact solution is given by

y = exp(-cosx+1). Plot the numerical solution against the exact solution for comparison.

Note:Record your intermediate results using “Print Screen”Save your results and submit them through your usual way.