unit18.mws

ORDINARY DIFFERENTIAL EQUATIONS POWERTOOL

Unit 18 -- Finite Difference Method

Prof. Douglas B. Meade

Industrial Mathematics Institute

Department of Mathematics

University of South Carolina

Columbia, SC 29208

URL: http://www.math.sc.edu/~meade/

E-mail: meade@math.sc.edu

Copyright © 2001 by Douglas B. Meade

All rights reserved

-------------------------------------------------------------------

>

Outline of Unit 18

>

Initialization

> restart;

> with( DEtools ):

> with( plots ):

> with( linalg ):

Warning, the name changecoords has been redefined

Warning, the name adjoint has been redefined

Warning, the protected names norm and trace have been redefined and unprotected

>

18.A Overview of the Finite Difference Method

The finite difference method for a two-point boundary value problem

Diff(y,`$`(x,2))+p(x)*Diff(y,x)+q(x)*y = f(x)

y(a) = r

y(b) = s .

seeks to obtain approximate values of the solution at a collection of points, x[k] , in the interval [a, b] . Typically, the interval is uniformly partitioned into N equal subintervals of length h = (b-a)/N . In this case, x[k] = a+k*h for k = 0, 1, ..., N . Let y[k] = y(x[k]) , k = 0, 1, ..., N , denote the value of the solution at each node. Note that the boundary conditions imply y[0] = r and y[N] = s so there are actually only ( N-1 ) unknowns. To determine a system of ( N-1 ) equations satisfied by y[1] , y[2] , ..., y[N-1] , replace the first and second derivatives of the unknown function at the interior partion points with the centered difference quotient approximations

eval(Diff(y,x),x = x[k]) = (y[k+1]-y[k-1])/(2*h)

eval(Diff(y,`$`(x,2)),x = x[k]) = (y[k+1]-2*y[k]+y[...

For linear problems, the resulting system of equations is linear. For nonlinear problems, an iterative method such as Gauss-Seidel is typically used to obtain an approximate solution.

>

18.B Example 1

Consider the BVP

> ode1 := diff( y(x), x$2 ) + 2 * diff( y(x), x ) - 10 * y(x) = 7*exp(-x) + 4;

> bc1 := y(0)=2, y(1)=-5;

ode1 := diff(y(x),`$`(x,2))+2*diff(y(x),x)-10*y(x) ...

bc1 := y(0) = 2, y(1) = -5

>

with boundary conditions specified at the points

> a := 0;

> b := 1;

a := 0

b := 1

>

To find the finite difference solution to this BVP with

> N := 10:

>

we have

> h := (b-a)/N;

h := 1/10

>

and

> X := k -> a+k*h;

X := proc (k) options operator, arrow; a+k*h end pr...

>

Define the finite difference operators

> Yp := k -> (y[k+1]-y[k-1])/2/h;

Yp := proc (k) options operator, arrow; 1/2*(y[k+1]...

> Ypp := k -> (y[k+1]-2*y[k]+y[k-1])/h^2;

Ypp := proc (k) options operator, arrow; (y[k+1]-2*...

>

The boundary conditions provide the two conditions

> eq[0] := y[0] = rhs(bc1[1]);

eq[0] := y[0] = 2

> eq[N] := y[N] = rhs(bc1[2]);

eq[10] := y[10] = -5

>

and the equations for the values at the interior nodes are

> for k from 1 to N-1 do

> eq[k] := eval( ode1,

> {x=X(k), y(x)=y[k],

> diff(y(x),x)=Yp(k),

> diff(y(x),x$2)=Ypp(k)} );

> end do;

eq[1] := 110*y[2]-210*y[1]+90*y[0] = 7*exp(-1/10)+4...

eq[2] := 110*y[3]-210*y[2]+90*y[1] = 7*exp(-1/5)+4

eq[3] := 110*y[4]-210*y[3]+90*y[2] = 7*exp(-3/10)+4...

eq[4] := 110*y[5]-210*y[4]+90*y[3] = 7*exp(-2/5)+4

eq[5] := 110*y[6]-210*y[5]+90*y[4] = 7*exp(-1/2)+4

eq[6] := 110*y[7]-210*y[6]+90*y[5] = 7*exp(-3/5)+4

eq[7] := 110*y[8]-210*y[7]+90*y[6] = 7*exp(-7/10)+4...

eq[8] := 110*y[9]-210*y[8]+90*y[7] = 7*exp(-4/5)+4

eq[9] := 110*y[10]-210*y[9]+90*y[8] = 7*exp(-9/10)+...

>

Even though this system of equations is linear, it's simpler to use fsolve to find an approximate solution.

> fd_sol1 := fsolve( {seq( eq[k], k=0..N )}, {seq( y[k], k=0..N )} );

fd_sol1 := {y[10] = -5., y[0] = 2., y[5] = -1.77613...
fd_sol1 := {y[10] = -5., y[0] = 2., y[5] = -1.77613...

>

Reformatting these results for plotting

> fd_table1 := eval( [seq([X(k),y[k]],k=0..N)], fd_sol1 ):

>

and for tabular presentation

> stackmatrix( fd_table1 );

matrix([[0, 2.], [1/10, .7287947840], [1/5, -.15108...

>

Because of the simple nature of this example, Maple is able to determine the exact solution to this problem.

> infolevel[dsolve] := 3:

> exact_sol1 := combine(dsolve( { ode1, bc1 }, y(x) ));

> infolevel[dsolve] := 0:

Methods for second order ODEs:

Trying to isolate the derivative d^2y/dx^2...

Successful isolation of d^2y/dx^2

--- Trying classification methods ---

trying a quadrature

trying high order exact linear fully integrable

trying differential order: 2; linear nonhomogeneous with symmetry [0,1]

trying a double symmetry of the form [xi=0, eta=F(x)]

Try solving first the homogeneous part of the ODE

   -> Tackling the linear ODE "as given":

      checking if the LODE has constant coefficients

      <- constant coefficients successful

   -> Determining now a particular solution to the non-homogeneous ODE

      building a particular solution using variation of parameters

   <- solving first the homogeneous part of the ODE successful

exact_sol1 := y(x) = 1/55*(-253*exp(sqrt(11))-167*e...
exact_sol1 := y(x) = 1/55*(-253*exp(sqrt(11))-167*e...

>

To compare the solutions, plot the exact and finite difference solutions

> plot( [rhs(exact_sol1),fd_table1], x=a..b,

> color=[BLACK,GREEN], thickness=[1,3],

> legend=[`exact`,`finite difference`] );

[Maple Plot]

>

This also shows good agreement between the finite difference and exact solutions.

>

18.C Example 2

Consider the BVP

> ode2 := x^2 * diff( y(x), x$2 ) + x * diff( y(x), x ) + 4 * y(x) = -2*x + 7;

> bc2 := y(1)=7, y(4)=-1;

ode2 := x^2*diff(y(x),`$`(x,2))+x*diff(y(x),x)+4*y(...

bc2 := y(1) = 7, y(4) = -1

>

The boundary conditions are given at

> a := 1;

> b := 4;

a := 1

b := 4

>

The finite difference method with

> N := 10;

N := 10

>

means the stepsize is

> h := (b-a)/N;

h := 3/10

>

and

> X := k -> a+k*h;

X := proc (k) options operator, arrow; a+k*h end pr...

>

Define the finite difference operators

> Yp := k -> (y[k+1]-y[k-1])/2/h;

Yp := proc (k) options operator, arrow; 1/2*(y[k+1]...

> Ypp := k -> (y[k+1]-2*y[k]+y[k-1])/h^2;

Ypp := proc (k) options operator, arrow; (y[k+1]-2*...

>

The boundary conditions provide the two conditions

> eq[0] := y[0] = rhs(bc2[1]);

eq[0] := y[0] = 7

> eq[N] := y[N] = rhs(bc2[2]);

eq[10] := y[10] = -1

>

and the equations for the values at the interior nodes are

> for k from 1 to N-1 do

> eq[k] := eval( ode2,

> {x=X(k), y(x)=y[k],

> diff(y(x),x)=Yp(k),

> diff(y(x),x$2)=Ypp(k)} );

> end do;

eq[1] := 377/18*y[2]-302/9*y[1]+299/18*y[0] = 22/5

eq[2] := 280/9*y[3]-476/9*y[2]+232/9*y[1] = 19/5

eq[3] := 779/18*y[4]-686/9*y[3]+665/18*y[2] = 16/5

eq[4] := 517/9*y[5]-932/9*y[4]+451/9*y[3] = 13/5

eq[5] := 1325/18*y[6]-1214/9*y[5]+1175/18*y[4] = 2

eq[6] := 826/9*y[7]-1532/9*y[6]+742/9*y[5] = 7/5

eq[7] := 2015/18*y[8]-1886/9*y[7]+1829/18*y[6] = 4/...

eq[8] := 1207/9*y[9]-2276/9*y[8]+1105/9*y[7] = 1/5

eq[9] := 2849/18*y[10]-2702/9*y[9]+2627/18*y[8] = -...

>

Even though this system of equations is linear, it's simpler to use fsolve to find an approximate solution.

> fd_sol2 := fsolve( {seq( eq[k], k=0..N )}, {seq( y[k], k=0..N )} );

fd_sol2 := {y[8] = 3.313083012, y[10] = -1., y[0] =...
fd_sol2 := {y[8] = 3.313083012, y[10] = -1., y[0] =...

>

Reformatting these results for plotting

> fd_table2 := eval( [seq([X(k),y[k]],k=0..N)], fd_sol2 ):

>

and for tabular presentation

> stackmatrix( fd_table2 );

matrix([[1, 7.], [13/10, 11.83640948], [8/5, 13.621...

>

Again, because of the simple nature of this example, Maple is able to determine the exact solution to this problem.

> exact_sol2 := combine(dsolve( { ode2, bc2 }, y(x) ));

exact_sol2 := y(x) = 1/20*(-23*sin(2*ln(x))+113*sin...

>

To compare the solutions, plot the exact and finite difference solutions

> plot( [rhs(exact_sol2),fd_table2], x=a..b,

> color=[BLACK,GREEN], thickness=[1,3],

> legend=[`exact`,`finite difference`] );

>

[Maple Plot]

This also shows good agreement between the finite difference and exact solutions.

>

[Back to ODE Powertool Table of Contents]

>

>