EP501 HW5 Rajat Joshi

23
EP501 Homework - 5 Date of Submittal: November 08, 2013 By: Rajat Joshi

description

Numerical Methoda homework 5 solution

Transcript of EP501 HW5 Rajat Joshi

Page 1: EP501 HW5 Rajat Joshi

EP501 Homework - 5

Date of Submittal: November 08, 2013

By:

Rajat Joshi

Page 2: EP501 HW5 Rajat Joshi

11/08/2013 RAJAT JOSHI HW # 5

Q1. Numerical Stability

Solve the ODEdydx

=−xy , y ( x=0 )=1

using both analytical method (for the exact solution) and one (you get to choose) of the second-order numerical methods we learned in class. Compare your numerical solution with the exact solution. Use various step sizes (both stable and unstable) to investigate the behavior of the numerical solution against the analytical solution. Create plots of what you deem interesting and instructive. But don't just show plots, analyze the results. Tell a story.

In order to evaluate the exact solution of the ODE, it has to be solved using the following procedure to get an expression for the exact solution:

dydx

=−xy e.q. 1.1

e.q. 1.1 can be re-written as:

dyy

=−xdx e.q. 1.2

Integrating both sides yields:

ln ( y )=−x2

2+C e.q. 1.3

Solving for C using the initial conditions given to us (y = 1 for x = 0):

ln (1 )=0+C

C=0

Therefore, the exact solution of the given ODE in exponential form can be written as:

y=e−x2

2 e.q. 1.4

e.q. 1.4 can be used to evaluate the exact solution of the given ODE for varying values of x incremented by a specified step size, dx.

In order to compare the analytical solution (exact solution) with a second-order numerical solution, Modified Midpoint Method was used. The above ODE can be approximated as an FDE and solve using the Modified Midpoint by as follows:

1 | P a g e

Page 3: EP501 HW5 Rajat Joshi

11/08/2013 RAJAT JOSHI HW # 5

Predictor:

yi+

12

p = y i+Δx2

(−x i y i) e.q. 1.5

xi+

12

p =x i+Δx2 e.q. 1.6

Corrector second-order FDE’s:

y i+1c = y i+Δx(−x

i+12

p yi+

12

p ) e.q. 1.7

All completed MATLAB function and code for solving the given ODE using the exact solution as well as the modified midpoint method of solving using approximate FDE’s are attached in the zipped homework folder.

In order to investigate the behavior of the numerical solution the following properties need to be considered:

a. Consistency of the FDEb. Stability of the FDEc. Convergence of the FDE

a. Consistency of the FDE:

To determine the consistency of the FDE for the given first order ODE, Taylor series expansion of yi and yi+1 is written for ½ time step as follows:

y i+1= y i+1+ y|i+12

' ∆ x2

+ 12y|i+ 1

2

' ' (∆ x2 )2

+ 16y|i+1

2

' ' ' (∆ x2 )3

+… e.q. 1.8

y i= y i+1+ y|i+12

' (−∆ x2 )+ 12y|i+1

2

' ' (−∆ x2 )2

+ 16y|i+1

2

' ' ' (−∆ x2 )3

+… e.q. 1.9

Subtracting e.q. 1.9 from 1.8 yields:

y|i+12

' =y i+1− y i∆ x

− 124y|i+1

2

' ' ' (∆ x )2 e.q. 1.10

The above equation can be re-written in the form as:

y i+1= y i+∆ xf (xi+

12

, yi+

12

) e.q. 1.11

2 | P a g e

Page 4: EP501 HW5 Rajat Joshi

11/08/2013 RAJAT JOSHI HW # 5

Where,

yi+

12

= y i+Δx2

(−x i y i) e.q. 1.12

xi+

12

=x i+Δx2 e.q. 1.13

Substituting e.q. 1.12 and 1.13 in e.q. 1.11 yields:

y i+1= y i+∆ x [−(x i+ Δx2 )( y i+ Δx2 (−x i y i))] e.q. 1.14

y i+1− y i=∆ x [−( xi+ Δx2 )( y i+ Δx2 (−xi y i))]y i+1− y i∆ x

=[− y i( xi+ Δx2 )(1− Δx2 x i)] e.q. 1.15

To check for the consistency of the FDE the above equation, e.q. 1.15 should convert to the given ODE.

as ∆ t→0 ,y i+1− y i∆ x

→ y'

Therefore, for Δx tending to 0, e.q. 1.15 becomes:

y '=[− y i( xi+ 02 )(1−0

2x i) ]

y '=−x i y i

Hence, it proves that as Δx tends to zero the original ODE is retained. Thereby, showing that the given FDE is consistent to the given ODE.

This consistent behavior is also demonstrated by plotting the solution generated by solving the FDE using the midpoint method and the exact solution of the ODE using the analytical method over values of x varying from 0 to 1 with Δx = 0.0001 (very small). The result of plotting both the solution is shown in figure 1 which clearly shows the midpoint solution overlapping completely with the exact solution.

3 | P a g e

Page 5: EP501 HW5 Rajat Joshi

11/08/2013 RAJAT JOSHI HW # 5

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

0.65

0.7

0.75

0.8

0.85

0.9

0.95

1

x

y

Solution of ODE

Exact SolutionMidPoint

Figure 1: Plot of both solutions of ODE for very small values of Δx

b. Stability of the FDE:

An FDE is said to be stable if it produces a bounded solution for a stable ODE and it is unstable if it produces an unbounded solution for a stable ODE.

To do the stability analysis of the FDE one needs to determine the amplification factor G of the ODE and then determine the condition ensuring |G| ≤ 1.

Using e.q. 1.14 and re-writing it in the following form to calculate G:

y i+1

y i=1+∆ x [−(x i+ Δx2 )(1+ Δx

2(−xi))]

Where,

G=y i+1

y i

G=1−∆ x (x i+ Δx2 )+ Δx2

2(x i)(x i+ Δx2 )

For the FDE to be stable |G| ≤ 1. Therefore,

|G|=|1−∆ x (x i+ Δx2 )+ Δx2

2(x i)(x i+ Δx2 )|≤1

−∆ x (x i+ Δx2 )+ Δx2

2(x i)(x i+ Δx2 )≤0

4 | P a g e

Page 6: EP501 HW5 Rajat Joshi

11/08/2013 RAJAT JOSHI HW # 5

∆ x (x i+ Δx2 )( Δx2 x i−1)≤0 e.q. 1.16

The values of xi and Δx are greater than 0 and therefore, the only term in equation 1.16 that can satisfy the inequality is:

( Δx2 x i−1)≤0

x i∆ x≤2 e.q. 1.17

Equating 1.17 is the condition that needs to be satisfied for the condition to be stable.

To validate the stability condition obtained per equation 1.17, the MATLAB code was executed for value of x varying from 0 to 10 and values of Δx such that it is extremely stable, stable, close to stability and unstable.

Plot for Δx = 0.01:

0 1 2 3 4 5 6 7 8 9 100

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

x

y

Solution of ODE

Exact SolutionMidPoint

Figure 2: Plot of both solutions of ODE

5 | P a g e

Page 7: EP501 HW5 Rajat Joshi

11/08/2013 RAJAT JOSHI HW # 5

0 2 4 6 8 1010

-25

10-20

10-15

10-10

10-5

x

Err

or

Solution of ODE

Figure 3: Plot of error versus x

0 1 2 3 4 5 6 7 8 9 100

0.01

0.02

0.03

0.04

0.05

0.06

0.07

0.08

0.09

0.1

x

Am

plifi

catio

n F

acto

r

Solution of ODE

Figure 4: Plot of G versus values x

As can be seen from figures 2 and 4, the FDE is extremely stable for the given value of Δx as the max value of the stability criteria is 0.1 which is considerably less than 2. Also, figure 2 shows how the solution using the midpoint method matches with the exact solution and figure 3 shows the error constantly decreasing.

6 | P a g e

Page 8: EP501 HW5 Rajat Joshi

11/08/2013 RAJAT JOSHI HW # 5

Plot for Δx = 0.2:

0 1 2 3 4 5 6 7 8 9 100

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

x

y

Solution of ODE

Exact SolutionMidPoint

Figure 5: Plot of both solutions of ODE

0 2 4 6 8 1010

-10

10-8

10-6

10-4

10-2

x

Err

or

Solution of ODE

Figure 6: Plot of error versus x

0 1 2 3 4 5 6 7 8 9 100

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2

x

Am

plifi

catio

n F

acto

r

Solution of ODE

Figure 7: Plot of G versus values x

7 | P a g e

Page 9: EP501 HW5 Rajat Joshi

11/08/2013 RAJAT JOSHI HW # 5

As can be seen from figures 5 and 7, the FDE starts getting unstable for the given value of Δx as the max value of the stability criteria is 2.0 which is right at the edge of the stability criterion. Also, figure 5 shows how the solution using the midpoint method starts deviating from the exact solution, especially toward the values of x that make the stability criterion reach the instability condition and figure 7 shows that the error is much larger as compare to figure 3.

Plot for Δx = 0.5:

0 1 2 3 4 5 6 7 8 9 100

2000

4000

6000

8000

10000

12000

14000

x

y

Solution of ODE

Exact SolutionMidPoint

Figure 8: Plot of both solutions of ODE

0 2 4 6 8 1010

-5

100

105

x

Err

or

Solution of ODE

Figure 9: Plot of error versus x

8 | P a g e

Page 10: EP501 HW5 Rajat Joshi

11/08/2013 RAJAT JOSHI HW # 5

0 1 2 3 4 5 6 7 8 9 100

0.5

1

1.5

2

2.5

3

3.5

4

4.5

5

x

Am

plifi

catio

n F

acto

r

Solution of ODE

Figure 10: Plot of G versus values x

As can be seen from figures 8 and 10, the FDE is highly unstable for the given value of Δx as the max value of the stability criteria is 5.0 which is way above the stable condition. Also, figure 8 shows how the solution using the midpoint method deviates drastically from the exact solution, especially toward the values of x that make the stability criterion reach the instability condition and figure 9 shows that the error keeps growing causing the solution to diverge more and more from the exact solution.

c. As proven above that the FDE is consistent and as long as the stability condition as given by e.q. 1.17 is satisfied then the FDE is convergent (as seen in figure 3). The moment the stability condition is not satisfied, the solution of the FDE diverges from the exact solution (as can also be seen from figure 9).

Q2. Nonlinear Implicit FDE

Solve the following initial value problem with the implicit Euler method.

y '=1+0.5 y2 , y (0 )= y0=0.5

Because it is a nonlinear ODE, at each step the new value yi+1 needs to be solved from a nonlinear equation. Use Newton's method to solve for this in each step. Compare your numerical result with the exact solution

y (t )= 1

√0.5tan [√0.5 t+ tan−1 (√0.5 y0 ) ]

Again, vary the step size, investigate the behavior, and report what you find interesting.

9 | P a g e

Page 11: EP501 HW5 Rajat Joshi

11/08/2013 RAJAT JOSHI HW # 5

For the given ODE, f(t, y) is nonlinear in y and the implicit FDE for it is also nonlinear in yi+1. In order to solve the given initial value problem using the Implicit Euler method, an FDE is written is written as follows:

y i+1= y i+∆ t f i+1

y i+1= y i+∆ t (1+0.5 y i+12 ) e.q. 2.1

As can be clearly seen from the FDE presented by equation 2.1 that the value of y i+1 is dependent on the value of yi+1 and are nonlinear in yi+1. Therefore, this form of ODE requires computing the value of yi+1 iteratively using the Newton’s method as follows:

e.q. 2.1 can be written as:

F ( yi+1 )= y i+1− y i−∆ t (1+0.5 y i+12 ) e.q. 2.2

Taking the derivative w.r.t yi+1 of e.q. 2.2 yields:

F ' ( yi+1 )=1− y i+1 e.q. 2.3

Solving for yi+1 using the following Newton’s method algorithm iteratively yields the value which can be plugged in e.q. 2.1 RHS and solved for the final value of the FDE using the implicit Euler method.

y i+1k +1= y i+1

k −F ( y i+1

k )F ' ( y i+1

k )e.q. 2.4

The above Newton’s expression is executed till the relative error between progressively iterative values of yi+1 do not reach within the tolerance of 1e-12.

All completed MATLAB function and code for solving the given nonlinear ODE using the exact solution as well as the Implicit Euler method, Newton’s method of solving using approximate FDE’s are attached in the zipped homework folder.

The MATLAB code was executed for various values of Δt and a Δt value was chosen that could enabled the MATLAB code to run (as will be explained below) and the following plots were obtained:

For 0≤ t ≤1.73 and dt = 0.001:

10 | P a g e

Page 12: EP501 HW5 Rajat Joshi

11/08/2013 RAJAT JOSHI HW # 5

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.80

50

100

150

200

250

300

350

400

t

y

Solution of Nonlinear ODE

Exact SolutionImplicit Euler

Figure 11: Plot of both solutions of nonlinear ODE

0 0.5 1 1.5 210

-20

10-10

100

1010

t

Err

or

Solution of Nonlinear ODE

Figure 12: Plot of error versus t

As can be seen from figure 11 and 12 that the solution of the nonlinear FDE matches almost perfectly with the exact solution. However as mentioned above as well, there is a certain value of t beyond which the Newton’s Method starts diverging.

Reason for Newton’s Method Divergence:

The reason why the Newton’s method diverges beyond certain value of ‘t’ is because the solution of this nonlinear ODE contains a tangent function which has an asymptotic output. Implying, that the values of the solution reaches +∞ or -∞ at these asymptotes as shown by figures 13 and 14 below:

11 | P a g e

Page 13: EP501 HW5 Rajat Joshi

11/08/2013 RAJAT JOSHI HW # 5

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-60

-40

-20

0

20

40

60

80

t

y

Plot of (1/sqrt(0.5))*(tan(((sqrt(0.5))*t)+atan((sqrt(0.5))*y0)))

Figure 13: Plot of exact solution to show asymptote around 1.73

-5 0 5

-5

0

5

t

(1/sqrt(0.5)) (tan(((sqrt(0.5)) t)+atan((sqrt(0.5)) y0)))

y

Figure 14: Cleared representation of the asymptotic behavior of the solution using ‘explot’

In order to avoid crashing the MATLAB code, value of ‘t’ cat which the asymptotes occur for the solution was evaluated as follows:

We know that the first asymptote for a tangent function appear at a value of the angle π/2. Therefore for the given exact solution:

y (t )= 1

√0.5tan [√0.5 t+ tan−1 (√0.5 y0 ) ]

√0.5 t+ tan−1 (√0.5 (0.5 ) )=π2

12 | P a g e

Page 14: EP501 HW5 Rajat Joshi

11/08/2013 RAJAT JOSHI HW # 5

t= 1√0.5 [ π2 −tan−1 (√0.5 (0.5 )) ]t=1.740839502734206

So, the solution of our nonlinear initial value problem has its first asymptote for positive values of t at t = 1.740839502734206. Hence, the numerical method using the Newton’s algorithm and the Implicit Euler method will only be able to provide a solution to the FDE for t < 1.740839502734206.

It can be seen that if Δt is increased from 0.001 to 0.1 then the numerical method fails to obtain a solution beyond values of t greater than 1.4 (unlike 1.73 with Δt = 0.001).

0 0.2 0.4 0.6 0.8 1 1.2 1.40

1

2

3

4

5

6

7

8

9

t

y

Solution of Nonlinear ODE

Exact SolutionImplicit Euler

Figure 15: Plot of both solutions of nonlinear ODE

0 0.5 1 1.510

-20

10-10

100

1010

t

Err

or

Solution of Nonlinear ODE

Figure 16: Plot of error versus t

13 | P a g e

Page 15: EP501 HW5 Rajat Joshi

11/08/2013 RAJAT JOSHI HW # 5

Figures 15 and 16 show that solution not only stops at a much smaller value of t, but it also starts deviating from the exact solution, causing the error to grow and the solution to become unstable as the system tends to the asymptotic point.

Q3. Stability of RK4

Use the model ODE y’ = -αy to show that the G factor of the RK4 method is

G=1−(α ∆ t )+ 12

(α ∆ t )2−16

(α ∆ t )3+ 124

(α∆ t )4

The fourth order Runge-Kutta method (RK4) method is given by:

y i+1= y i+16

(∆ y1+2∆ y2+2∆ y3+∆ y 4 ) e.q. 3.1

Where,∆ y1=∆ tf (t i+, y i )

∆ y2=∆ tf (ti+ h2 , y i+∆ y1

2 )∆ y3=∆ tf (t i+ h2 , yi+∆ y2

2 )∆ y 4=∆ tf (t i+h , y i+∆ y3 )

To perform the stability analysis of the RK4 method the above set of equations must be applied to the model ODE y’ = -αy, and the results must be combined into a single-step FDE.

Therefore,

∆ y1=∆ tf (t n , yn )=−α ∆ t y i

∆ y2=∆ tf (t n+∆ t2 , yn+∆ y1

2 )=∆ t(−α { y i+12

[− (α∆ t ) y i ]})∆ y2=−α ∆t y i(1−α ∆ t2 )

14 | P a g e

Page 16: EP501 HW5 Rajat Joshi

11/08/2013 RAJAT JOSHI HW # 5

∆ y3=∆ tf (t n+ ∆t2 , yn+∆ y2

2 )=∆ t (−α {y i+12 [−(α ∆ t ) y i(1−α ∆ t

2 )]})∆ y3=−α ∆ t y i[1−α∆ t2

+(α ∆t2 )2]

∆ y 4=∆ tf (t n+∆ t , yn+∆ y3 )=∆ t (−α [ y i−(α ∆ t ) y i(1−α∆ t2

+(α∆ t2 )2)])

∆ y 4=−(α∆ t ) y i[1−α∆ t+ (α∆ t )2

2−

(α ∆ t )3

4 ]Combining the values of Δy1, Δy2, Δy3 and Δy4 in e.q. 3.1 yields:

y i+1= y i−(α ∆ t ) y i+(α∆ t )2

2y i−

(α ∆ t )3

6y i+

(α ∆ t )4

24y i

G=y i+1

y i=1−(α∆ t )+ (α∆ t )2

2−

(α∆ t )3

6+

(α ∆ t )4

24

Q4. Adams-Bashforth Two-Point Method

The Adams-Bashforth Two-Point Explicit Method, as shown in Lecture20, is

y i+1= y i+∆ t2

( 3 f i−f i−1 )

Use the model ODE y’ = -αy = f(t, y) to derive the expression of G and the stability condition. Show all your steps.

The amplification factor (G) is determined by applying the given FDE to solve the model ODE, y’ = -αy.

The FDE given is:

15 | P a g e

Page 17: EP501 HW5 Rajat Joshi

11/08/2013 RAJAT JOSHI HW # 5

y i+1= y i+∆ t2

( 3 f i−f i−1 ) e.q. 4.1

For a two-point method applied to a linear ODE with constant Δt, the amplification factor G is the same for all steps. Thus,

G=y i+1

y i=y iy i−1

From the above expression, it can be deduced that:

y i−1=y iG

e.q. 4.2

For the given function, e.q. 4.1 can also be written as:

y i+1= y i+∆ t2

(α y i−1−3αy i) e.q. 4.3

Substituting e.q. 4.2 in e.q. 4.3 yields:

y i+1= y i+∆ t2 (α y iG−3αyi)

y i+1= y i[1+∆ t2 ( αG−3 α)]

y i+1

y i=[1+∆ t

2 ( αG−3α)] e.q. 4.4

Substituting the relationship of the amplification factor in e.q. 4.4 and solving for G:

G=1+ α ∆ t2 ( 1

G−3)

G2−G=α∆ t2

(1−3G )

G2+( 3α ∆ t−22 )G−α∆ t

2=0 e.q. 4.5

The above equation, e.q. 4.5 is a quadratic equation and can be solved to find the roots of G:

16 | P a g e

Page 18: EP501 HW5 Rajat Joshi

11/08/2013 RAJAT JOSHI HW # 5

G=( 2−3α∆ t

2 )±√(3 α∆ t−22 )

2

+2α ∆ t

2

G=2−3α ∆t ±√9α 2∆ t2+4−12α ∆ t+8α∆ t

G=2−3α ∆t ±√9α 2∆ t2+4−4 α∆ t

G1=2−3α∆ t+√9α2∆ t 2+4−4α ∆ t e.q. 4.6

G2=2−3α∆ t−√9α 2∆ t2+4−4α ∆ t e.q. 4.7

Condition of Stability:

The condition of stability must satisfy the following condition for all values of G:

|Gi|≤1

|2−3α∆ t ±√9α 2∆ t2+4−4α ∆ t|≤1

±√9α2∆ t 2+4−4 α∆ t ≤3α ∆ t−1

Squaring both sides yields:

9α 2∆ t2+4−4α ∆ t ≤9α 2∆ t 2+1−6 α∆ t

α ∆ t ≤−1.5

The above expression is the condition for the stability that needs to be satisfied for the FDE to be stable.

17 | P a g e