ap3114 lecture wk11 solving differential eqautions ii

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

Upload: guoxuanchan

Post on 12-Dec-2015

228 views

Category:

Documents


0 download

DESCRIPTION

Physics

TRANSCRIPT

AP3114/Computational Methods for Physicists and Materials Engineers

8. Solving differential equations IINov. 9, 2015

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

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

Finite difference formulas based on Taylor series expansions

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

Taylor Expansions

error = O(h)

Finite difference approximations to the first derivative

Forward difference

Backward difference

Centered difference

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

Centered difference

Forward difference

Backward difference

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.

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

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

Example 1

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

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:

Example 2

Example 2-- Results

Example 3

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

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 )

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),

Example

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

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.

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.

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.