Numerical Solution to Ordinary Differential Equations By Gilberto E. Urroz, September 2004



Similar documents
A Brief Review of Elementary Ordinary Differential Equations

tegrals as General & Particular Solutions

5 Numerical Differentiation

SECOND-ORDER LINEAR DIFFERENTIAL EQUATIONS

Nonlinear Algebraic Equations. Lectures INF2320 p. 1/88

College of the Holy Cross, Spring 2009 Math 373, Partial Differential Equations Midterm 1 Practice Questions

Numerical Methods for Differential Equations

Second-Order Linear Differential Equations

1 Error in Euler s Method

4.3 Lagrange Approximation

1. First-order Ordinary Differential Equations

Separable First Order Differential Equations

2 Integrating Both Sides

Euler s Method and Functions

ab = c a If the coefficients a,b and c are real then either α and β are real or α and β are complex conjugates

Multi-variable Calculus and Optimization

Vector and Matrix Norms

Derivative Approximation by Finite Differences

Inner Product Spaces

Høgskolen i Narvik Sivilingeniørutdanningen STE6237 ELEMENTMETODER. Oppgaver

SECOND DERIVATIVE TEST FOR CONSTRAINED EXTREMA

4.5 Linear Dependence and Linear Independence

Class Meeting # 1: Introduction to PDEs

Solving DEs by Separation of Variables.

2.2 Derivative as a Function

Continued Fractions and the Euclidean Algorithm

Practice with Proofs

INTEGRATING FACTOR METHOD

Operation Count; Numerical Linear Algebra

LS.6 Solution Matrices

MATH 425, PRACTICE FINAL EXAM SOLUTIONS.

Linear and quadratic Taylor polynomials for functions of several variables.

x 2 + y 2 = 1 y 1 = x 2 + 2x y = x 2 + 2x + 1

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

DERIVATIVES AS MATRICES; CHAIN RULE

u dx + y = 0 z x z x = x + y = 0 6) 2

SOLUTIONS. f x = 6x 2 6xy 24x, f y = 3x 2 6y. To find the critical points, we solve

16.1 Runge-Kutta Method

correct-choice plot f(x) and draw an approximate tangent line at x = a and use geometry to estimate its slope comment The choices were:

3.2. Solving quadratic equations. Introduction. Prerequisites. Learning Outcomes. Learning Style

Introduction to Algebraic Geometry. Bézout s Theorem and Inflection Points

SOLVING LINEAR SYSTEMS

Solving ODEs in Matlab. BP205 M.Tremont

2.2 Separable Equations

Scalar Valued Functions of Several Variables; the Gradient Vector

Inner product. Definition of inner product

Constrained optimization.

Representation of functions as power series

The Method of Partial Fractions Math 121 Calculus II Spring 2015

MATH10212 Linear Algebra. Systems of Linear Equations. Definition. An n-dimensional vector is a row or a column of n numbers (or letters): a 1.

To give it a definition, an implicit function of x and y is simply any relationship that takes the form:

Solutions for Review Problems

This makes sense. t /t 2 dt = 1. t t 2 + 1dt = 2 du = 1 3 u3/2 u=5

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS. + + x 2. x n. a 11 a 12 a 1n b 1 a 21 a 22 a 2n b 2 a 31 a 32 a 3n b 3. a m1 a m2 a mn b m

On closed-form solutions to a class of ordinary differential equations

MATH2210 Notebook 1 Fall Semester 2016/ MATH2210 Notebook Solving Systems of Linear Equations... 3

1 Finite difference example: 1D implicit heat equation

Lecture 2. Marginal Functions, Average Functions, Elasticity, the Marginal Principle, and Constrained Optimization

1 Introduction to Matrices

1 Determinants and the Solvability of Linear Systems

Introduction to Partial Differential Equations By Gilberto E. Urroz, September 2004

ECG590I Asset Pricing. Lecture 2: Present Value 1

2x + y = 3. Since the second equation is precisely the same as the first equation, it is enough to find x and y satisfying the system

CHAPTER 2. Eigenvalue Problems (EVP s) for ODE s

PSTricks. pst-ode. A PSTricks package for solving initial value problems for sets of Ordinary Differential Equations (ODE), v0.7.

1.7 Graphs of Functions

Linearly Independent Sets and Linearly Dependent Sets

1 Lecture: Integration of rational functions by decomposition

19.6. Finding a Particular Integral. Introduction. Prerequisites. Learning Outcomes. Learning Style

Numerical Methods for Solving Systems of Nonlinear Equations

Geometric Transformations

Math 241, Exam 1 Information.

Linear Programming in Matrix Form

Integrals of Rational Functions

8 Hyperbolic Systems of First-Order Equations

Solving Systems of Linear Equations Using Matrices

Math Review. for the Quantitative Reasoning Measure of the GRE revised General Test

14.1. Basic Concepts of Integration. Introduction. Prerequisites. Learning Outcomes. Learning Style

General Framework for an Iterative Solution of Ax b. Jacobi s Method

5.4 The Heat Equation and Convection-Diffusion

Math 432 HW 2.5 Solutions

EL-9650/9600c/9450/9400 Handbook Vol. 1

Differential Operators and their Adjoint Operators

Question 2: How do you solve a matrix equation using the matrix inverse?

Using a table of derivatives

The continuous and discrete Fourier transforms

F Matrix Calculus F 1

ORDINARY DIFFERENTIAL EQUATIONS

Linear Programming Notes V Problem Transformations

3.2 Sources, Sinks, Saddles, and Spirals

Homework #1 Solutions

Introduction to Partial Differential Equations. John Douglas Moore

General Theory of Differential Equations Sections 2.8, , 4.1

MATH 304 Linear Algebra Lecture 20: Inner product spaces. Orthogonal sets.

Math 120 Final Exam Practice Problems, Form: A

Lecture Notes on Elasticity of Substitution

MAT12X Intermediate Algebra

3. Reaction Diffusion Equations Consider the following ODE model for population growth

Nonlinear Algebraic Equations Example

A First Course in Elementary Differential Equations. Marcel B. Finan Arkansas Tech University c All Rights Reserved

Transcription:

Numerical Solution to Ordinary Differential Equations By Gilberto E. Urroz, September 4 In this document I present some notes related to finite difference approximations and the numerical solution of single and systems of ordinary differential equations (ODEs). Finite differences and numerical solutions To solve differential equations numerically we can replace the derivatives in the equation with finite difference approximations on a discretized domain. This results in a number of algebraic equations that can be solved one at a time (explicit methods) or simultaneously (implicit methods) to obtain values of the dependent function y i corresponding to values of the independent function x i in the discretized domain. Finite differences A finite difference is a technique by which derivatives of functions are approximated by differences in the values of the function between a given value of the independent variable, say x, and a small increment (x +h). For example, from the definition of derivative, df/ lim h (f(x+h)-f(x))/h, we can approximate the value of df/ by using the finite difference approximation with a small value of h. (f(x+h)-f(x))/h The following table shows approximations to the derivative of the function f(x) exp(-x) sin (x /), at x, using finite differences. The actual value of the derivative is -.356987479. The third column in the table shows the error in evaluating the derivative, i.e., the difference between the numerical derivative f/ x and the actual value. h f/ x error. -.44677.846399. -.3668489.98689. -.35798686.999389. -.3578734.99869. -.3569976.9789. -.3569885.779. -.35698734.39. -.3569874.39. -.3569875.49

This exercise illustrates the fact that, as h, the value of the finite difference approximation, (f(x+h)-f(x))/h, approaches that of the derivative, df/, at the point of interest. A plot of the error as a function of h also reveals the fact that the error is proportional to the value of the x-increment h. The following plots, using different ranges of h and the error, are produced with MATLAB out of the data in the table.» h [e-,e-,e-3,e-4,e-5,e-6,e-7,e-8,e-9];» er [.846399,.98689,.999389,.99869,....9789,.779,.39,.39,....49];» plot(h,er,'o',h,er,'-');axis([..]);» title('error vs. x-increment');xlabel('h');ylabel('error');. error vs. x-increment.8.6 error.4.» axis([..]);...3.4.5.6.7.8.9. h x -3 error vs. x-increment.8.6 error.4.» axis([..]);...3.4.5.6.7.8.9. h

x -4 error vs. x-increment.9.8.7.6 error.5.4.3.....3.4.5 h.6.7.8.9 x -3 The graphs seem indicate that the error varies linearly with the increment h in the independent variable. It is very common to indicate this dependency by saying that "the error is of order h", or error O(h). The magnitude of the error can be estimated by using Taylor series expansions of the function f(x+h). Finite difference formulas based on Taylor series expansions The Taylor series expansion of the function f(x) about the point x x is given by the formula ( n) f ( x ) f ( x) ( x x ) n! Where f (n) (x ) (d n f/ n ) xx, and f () (x ) f(x ). If we let x x +h, then x-x h, and the series can be written as n ( n) f ( x ) n f '( x ) f "( x ) f ( x + h) h f ( x ) + h + h + O( h n!!! n Where the expression O(h 3 ) represents the remaining terms of the series and indicates that the leading term is of order h 3. Because h is a small quantity, we can write > h, and h>h >h 3 >h 4 > Therefore, the remaining of the series represented by O(h 3 ) provides the order of the error incurred in neglecting this part of the series expansion when calculating f(x +h). From the Taylor series expansion shown above we can obtain an expression for the derivative f (x ) as f ( x + h) f ( x ) f "( x ) f ( x + h) f ( x ) f '( x ) + h + O( h ) + O( h). h! h 3 n. 3 ),

In practical applications of finite differences, we will replace the first-order derivative df/ at x x, with the expression (f(x +h)-f(x ))/h, selecting an appropriate value for h, and indicating that the error introduced in the calculation is of order h, i.e., error O(h). Forward, backward and centered finite difference approximations to the first derivative The approximation df/ (f(x +h)-f(x ))/h is called a forward difference formula because the derivative is based on the value x x and it involves the function f(x) evaluated at x x +h, i.e., at a point located forward from x by an increment h. If we include the values of f(x) at x x - h, and x x, the approximation is written as df/ (f(x )-f(x -h))/h and is called a backward difference formula. The order of the error is still O(h). A centered difference formula for df/ will include the points (x -h,f(x -h)) and (x +h,f(x +h)). To find the expression for the formula as well as the order of the error we use the Taylor series expansion of f(x) once more. First we write the equation corresponding to a forward expansion: f(x +h) f(x )+f (x ) h+/ f (x ) h +/6 f (3) (x ) h 3 + O(h 4 ). Next, we write the equation for a backward expansion: f(x -h) f(x )-f (x ) h+/ f (x ) h -/6 f (3) (x ) h 3 + O(h 4 ). Subtracting these two equations results in f(x +h)- f(x -h) f (x ) h+/3 f (3) (x ) h 3 +O(h 5 ). Notice that the even terms in h, i.e., h, h 4,, vanish. Therefore, the order of the remaining terms in this last expression is O(h 5 ). Solving for f (x ) from the last result produces the following centered difference formula for the first derivative: df f ( x + h) f ( x h) (3) + f ( x) h O( h h 3 x x + 4 ), or, 4

df f ( x + h) f ( x h) + O( h ). h This result indicates that the centered difference formula has an error of the order O(h ), while the forward and backward difference formulas had an error of the order O(h). Since h <h, the error introduced in using the centered difference formula to approximate a first derivative will be smaller than if the forward or backward difference formulas are used. Forward, backward and centered finite difference approximations to the second derivative To obtain a centered finite difference formula for the second derivative, we'll start by using the equations for the forward and backward Taylor series expansions from the previous section but including terms up to O(h 5 ), i.e., f(x +h) f(x )+f (x ) h+/ f (x ) h +/6 f (3) (x ) h 3 + /4 f (4) (x ) h 4 + O(h 5 ). and f(x -h) f(x )-f (x ) h+/ f (x ) h /6 f (3) (x ) h 3 + /4 f (4) (x ) h 4 O(h 4 ). Next, add the two equations and solve for f (x ): d f/ [f(x +h) f(x )+f(x -h)]/h + O(h ). Forward and backward finite difference formulas for the second derivatives are given, respectively, by and d f/ [f(x + h) f(x +h)+f(x)]/h + O(h), d f/ [f(x ) f(x -h)+f(x - h)]/h + O(h). Solution of a first-order ODE using finite differences - Euler forward method Consider the ordinary differential equation, subject to the boundary condition, dy/ g(x,y), y(x ) y. 5

To solve this differential equation numerically, we need to use one of the formulas for finite differences presented earlier. Suppose that we use the forward difference approximation for dy/;, i.e., dy/ (y(x+h)-y(x))/h. Then, the differential equation is transformed into the following difference equation: from which, (y(x+h)-y(x))/h g(x,y), y(x+h) y(x)+h g(x,y). This result is known as Euler's forward method for numerical solution of first-order ODEs. Since we know the boundary condition (x,y ) we can start by solving for y at x x +h, then we solve for y at x 3 x +h, and so on. In this way, we generate a series of points (x, y ), (x, y ),, (x n, y n ), which will represent the numerical solution to the original ODE. The upper limit of the independent variable x n is either given or selected arbitrarily during the solution. The term "discretizing the domain of the independent variable" refers to obtaining a series of values of the independent variable, namely, x i, i,;,..., n, that will be used in the solution. Suppose that the range of the independent variable (a,b) is known, and that we use a constant value h x to divide the range into n equal intervals. By making x a, and x n b, then we find that the values of x i, i,3,... n, are given by x i x +(i-) x a+(i-) x, and that for i n, x n x +(n-) x. This latter result can be used to find n given x, or, to find x given n, n (x n -x )/ x + (b-a)/ x +, x (x n -x )/(n-) (b-a)/(n-). The recurrent equation for solving for y is given by y i+ y i + x g(x i,y i ), for i,,..., n-. Because the method solves y i+ f(x i,y i, x), i.e., one value of the dependent variable at a time, the method is said to be an explicit method. The following example illustrates the application of the Euler first-order method to the solution of the differential equation dy/ g(x,y) x + y using MATLAB. First, we define function g(x,y): 6

» g inline('x+y','x','y') g Inline function: g(x,y) x+y We solve the equation in the range of values of x from x to x n. with an increment Dx.. The initial condition is y. for x : >>x ; y ; Dx.; xn.; The following commands generate a vector of values of x, a vector y of the same length of x, initialized with zeros, and determines the value of n as the length of vector y (or x):» x [x:dx:xn]; n length(x); y zeros(,n); The following for end loop takes care of calculating the values of y i for i,3,, n:» for j :n- y(j+) y(j) + Dx*g(x(j),y(j)); end; To produce a plot of the results we determine the minimum and maximum values of y:» ymin min(min(y)), ymax max(max(y)) ymin ymax 3.775 The plot is generated by using:» plot(x,y,'o',x,y,'-');title('euler solution dy/x+y');» xlabel('x');ylabel('y(x)'); 4 Euler solution dy/x+y 3.5 3.5 y(x).5.5..4.6.8..4.6.8 x 7

A function to implement Euler s first-order method The following function, Euler, 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. function [x,y] Euler(x,y,xn,Dx,g) % Euler st order method solving ODE % dy/ g(x,y), with initial % conditions yy at x x. The % solution is obtained for x [x:dx:xn] % and returned in y ymaxallowed e+; x [x:dx:xn]; y zeros(x); n length(y); y() y; for j :n- y(j+) y(j) + Dx*g(x(j),y(j)); if y(j+) > ymaxallowed disp('euler - WARNING: underflow or overflow'); disp('solution sought in the following range:'); disp([x Dx xn]); disp('solution evaluated in the following range:'); disp([x Dx x(j)]); n j; x x(,:n); y y(,:n); break; end; end; Next, we use function Euler to solve the differential equation from the previous example, namely, dy/ g(x,y) x+y, for different values of the x increment, x.5,.,., and.5, with the same initial conditions and range of values of x as before:» [x,y] Euler(,,,.5,g);» [x,y] Euler(,,,.,g);» [x3,y3] Euler(,,,.,g);» [x4,y4] Euler(,,,.5,g); The exact solution for this equation is y(x) -x - + e x. Set of values of the exact solution are calculated as follows:» xx [:.:]; yy -xx-+.*exp(xx); To plot the exact and numerical solutions we first determine the minimum and maximum values of y: -->ymax max([y y y3 y4 yy]) ymax.778 -->ymin min([y y y3 y4 yy]) ymin. 8

The plot of the solutions is produced through the use of the following calls to function plot:» plot(xx,yy,'-',x,y,'o',x,y,'+',x3,y3,'d',x4,y4,'x');» title('euler st order - dy/ x+y');xlabel('x');ylabel('y(x)');» legend('exact','dx.5','dx.','dx.','dx.5'); 8 exact Dx.5 Dx. Dx. Dx.5 Euler st order - dy/ x+y y(x) 6 4..4.6.8..4.6.8 x A second example of application of function Euler is shown next for the differential equation dy/ xy +, with initial condition x, y, in the range < x <, with x.5,.,.,.5, and.. The MATLAB commands used are exactly the same as before except for the definition of function g(x,y) and the title of the plot. The function g(x,y) xy+ is defined as:» g inline('x*y+','x','y') g Inline function: g(x,y) x*y+ Numerical solutions to the differential equation for the different values of x are obtained from:» [x,y] Euler(,,,.5,g);» [x,y] Euler(,,,.,g);» [x3,y3] Euler(,,,.,g);» [x4,y4] Euler(,,,.5,g); Next, we determine the minimum and maximum values of y:» ymin min(min([y y y3 y4 yy])) ymin EDU» ymax max(max([y y y3 y4 yy])) ymax 4.5874 9

The plot of the numerical solution is accomplished through:» plot(x,y,'o',x,y,'+',x3,y3,'d',x4,y4,'x');» legend('dx.5','dx.','dx.','dx.5');» title('euler st order - dy/ x*y+');xlabel('x');ylabel('y(x)'); 5 Dx.5 Dx. Dx. Dx.5 Euler st order - dy/ x*y+ y(x) 5..4.6.8..4.6.8 x The following example solves the differential equation dy/ g(x,y) x + sin(xy) in the interval < x < 6.5, with initial conditions x, y, for x.5,.,.,.5, and.. The steps are the same as in the two previous example:» g inline('x+sin(x*y)','x','y') g Inline function: g(x,y) x+sin(x*y)» [x,y] Euler(,,,.5,g);» [x,y] Euler(,,,.,g);» [x3,y3] Euler(,,,.,g);» [x4,y4] Euler(,,,.5,g);» ymin min(min([y y y3 y4 yy])) ymin» ymax max(max([y y y3 y4 yy])) ymax.778» plot(xx,yy,'-',x,y,'o',x,y,'+',x3,y3,'d',x4,y4,'x');» legend('exact','dx.5','dx.','dx.','dx.5');» title('euler st order - dy/ x*y+');xlabel('x');ylabel('y(x)');

3.5 3.5 Dx.5 Dx. Dx. Dx.5 Euler st order - dy/ x+sin(x*y) y(x).5..4.6.8..4.6.8 x Finite difference formulas using indexed variables In the presentation of the Euler forward method, above, we demonstrated how you can get, from the general formula for the first derivative, dy/ [y(x+h)-y(x)]/h, the recurrence formula for the explicit solution, namely, y i+ y i + x g(x i,y i ), for i,,..., n-. This suggest re-writing the formula for the derivative as, dy/ (y i+ -y i )/ x + O( x). Using this sub-index notation, we can summarize the forward, centered, and backward approximations for the first and second derivatives as shown below: First Derivative FORWARD: dy/ (y i+ y i )/ x+o( x). CENTERED: dy/ (y i+ y i- )/( x)+o( x ). BACKWARD: dy/ (y i y i- )/ x+o( x).

Second Derivative FORWARD: d y/ (y i+ y i+ +y i )/( x )+O( x). CENTERED: d y/ (y i+ y i +y i- )/( x )+O( x ). BACKWARD: d y/ (y i y i- +y i- )/( x )+O( x). Solution of a first-order ODE using finite differences - an implicit method Consider again the ordinary differential equation, dy/ g(x,y), subject to the boundary condition, y(x ) y. This time, however, we use the centered difference approximation for dy/, i.e. dy/ (y(x+h)-y(x-h))/(*h). With this approximation the ODE becomes, (y(x+h)-y(x-h))/(*h) g(x,y). In terms of sub-indexed variables, this latter equation can be written as: y i- + x g(x i,y i )-y i+, ( i,3,..., n- ) where the substitutions y(x) y i, y(x+h) y i+,y(x-h) y i-, and h x, have been used. If the function g(x,y) is linear in y, then the equations described above consist of a set of (n-) equations. For example, if n 5, we have 3 equations: y + x g(x,y )-y 3 y + x g(x 3,y 3 )-y 4 y 3 + x g(x 4,y 4 )- Since y is known (it is the initial condition), there are still 4 unknowns, y, y 3, y 4, and. We need to find a fourth equation to obtain a solution. We could use, for example, the forward difference equation applied to i, i.e., or (y -y )/ x g(x,y ), y - x g(x,y )-y. The values of x i, and n (or x), can be obtained as in the Euler forward (explicit) solution.

Example -- Solve the ODE dy/ y sin(x), with initial conditions y(), in the interval < x < 5. Use x.5, or n (5-)/.5 +. Exact solution: the exact is y(x) exp(-cos(x))/(cosh()-sinh()). Numerical solution: Using a centered difference formula for dy/, i.e., dy/ (y i+ y i- )/( x), into the ODE, we get (y i+ y i- )/( x) y i sin(x i ), which results in the (n-) implicit equations: y i- + x sin(x i ) y i y i+, (i, 3,, n-). We already know that y (initial condition), thus we have (n-) unknowns left. We still need to come up with an additional equation, which could be obtained by using a forward difference formula for i, i.e., or dy/ x (y -y )/ x -y sin(x ), (+ x sin(x )) y - y. These equations can be written in the form of a matrix equation, for example, for n 5: + x sin( x ) x sin( x ) x sin( x ) 3 x sin( x ) 4 y y y y y 3 4 5 y where y represents the initial condition for y. [Note: The data requires n. The example for n 5 is presented above to provide a sense of the algorithm to fill out the matrix of data]. The matricial equation can be written as A y b. Matrix A and column vector b can be defined using MATLAB, as indicated below, and the solution found by using left-division. First, we enter the basic data for the problem:» x ; xn 5; Dx.5; y ; x [x:dx:xn]; n (xn-x)/dx+ n. 3

Next, we fill the main diagonal, and the two diagonals below the main diagonal in matrix A using:» A zeros(n,n); A(,) ; for j :n, A(j,j) -; end; % main diagonal» A(,) +Dx*sin(x()); for j 3:n, A(j,j-) *Dx*sin(x(j-)); end;» for j 3:n, A(j,j-) ; end; % third diagonal The right-hand side vector is defined as: >> b zeros(n,); b() ; % Right-hand side vector The implicit solution is obtained from: >> y A\b; % Solving for y To compare the implicit solution we calculate also the explicit solution obtained through the Euler first-order solution:» ff inline('y*sin(x)','x','y');» [xx,yy] Euler(x,y,xn,Dx,ff); To produce data reproducing the exact solution we use:» fe inline('exp(-cos(x))/(cosh()-sinh())');» xe [:.5:5]; ye fe(xe); The following commands will generate the plot showing the exact, implicit, and explicit solution in the same set of axes:» plot(xe,ye,'-',x',y,'+',xx',yy','o');» legend('exact','implicit','explicit'); 8 7 6 Exact Implicit Explicit 5 4 3.5.5.5 3 3.5 4 4.5 5 4

Explicit versus implicit methods The idea behind the explicit method is to be able to obtain values such as y i+ f(xi, y i ), y i+ f(x i,x i+,y i,y i+ ), etc. In other words, your solution proceeds by solving explicitly for a new unknown value in the solution array, given all previous values in the array. On the other hand, implicit methods imply the simultaneous solution of n linear algebraic equations that provide, at once, the elements of the solution array. With this distinction in mind between explicit and implicit methods, we outline explicit and implicit solutions for second-order, linear ODEs. Outline of explicit solution for a second-order ODE For example, to solve the ODE d y/ +y, in the x-interval (,) subject to y(), dy/ at y. Use x.. First, we discretize the differential equation using the finite difference approximation d y/ (y i+ - y i+ +y i )/( x ), which results in (y i+ - y i+ +y i )/( x )+y i. An explicit solution can be obtained from the recurrence equation: y i+ y i+ -(+ x ) y i, i,,..., n-;. This equation is based on the two previous values of y i, therefore, to get started we need the values y y, and y y. The value y is provided in the initial condition, y(), i.e., y. The value of y can be obtained from the second initial condition, dy/, by replacing the derivative with the finite difference approximation: which results in or dy/ (y - y )/ x, (y - y )/ x, y y + x. 5

The x-domain is discretized in a similar fashion as in the previous examples for first derivatives, i.e., by making x a, and x n b, and computing the values of x i, i,3,... n, with where, x i x +(i-) x a+(i-) x, n (x n -x )/ x+ (b-a)/ x+. The implementation of the solution for this example is left as an exercise for the reader. Outline of the implicit solution for a second-order ODE We use the same problem from the previous section: solve the ODE d y/ +y, in the x-interval (,) subject to y(), dy/ at x. Use x.. We discretize the differential equation using the finite difference approximation which results in d y/ (y i+ - y i+ +y i )/( x ), (y i+ -*y i +y i- )/( x )+y i. From this result we get the following implicit equations: y i- -(- x ) y i +y i+, for i,3,..., n-. There are a total of (n-) equations. Since we have n unknowns, i.e., y, y,...,y n, we need two more equations to solve a system of linear equations. The remaining equations are provided by the two initial conditions: From the initial condition, y(), we can write y. For the second initial condition, dy/, at x, we will use a forward difference, i.e., or dy/ (y - y )/ x, y - y x. The x-domain is discretized in a similar fashion as in the previous examples. The n equations resulting from discretizing the domain can be written as a matrix equation similar to that of Example. Solution to the matrix equation can be accomplished, for example, through the use of left-division for matrices. The implementation of the solution for this example is left as an exercise for the reader. 6

MATLAB provides a number of functions for the numerical solution of differential equations. These functions are designed to operate on single differential equations (i.e., similar to the examples presented so far), as well as on systems of differential equations. Therefore, before presenting the MATLAB functions for solving ordinary differential equations, we present some concepts related to systems of such equations. Systems of ordinary differential equations To introduce the idea of systems of differential equations we will limit the coverage of the subject to first-order, linear equations with constant coefficients. A system of ordinary differential equations consists of a set of two or more equations with an equal number of unknown functions, y ( x ), y ( x ), etc. As an example consider the following homogeneous system: dy dy + 3 y y, y + y. In a homogeneous system the right-hand sides of the equations are zero. The following example represents a non-homogeneous system of ordinary differential equations: dy dy + y 5 y sin( x ), 4 y + 3 y e x. Systems of ordinary differential equations using matrices A homogeneous system of ODEs can be written as a single matrix differential equation by using vector functions and a matrix of coefficients as illustrated in the following example. First, we re-write the homogeneous system presented above to read: dy 3 y + y, dy y y. Then, we define the vector function f(x) [y (x) y (x)] T, and the matrix A [-3 ; -], and write the differential equation: d f(x) A f(x). This result is equivalent to writing: d y y ( x) 3 ( x) y y ( x). ( x) The non-homogeneous system presented earlier can be re-written as 7

dy y + 5 y sin( x ), dy 4 y 3 y + e x. For this system we will use the same vector function f(x) defined earlier, but change the matrix A to A [- 5; 4-3]. We also need to define a new vector function, g(x) [-sin(x) exp(x)] T. With these definitions, we can re-write the non-homogeneous system as: or d f(x) A f(x)+ g(x), d y y ( x) ( x) 4 5 3 y y ( x) sin( x) +. ( x) exp( ) x Converting second-order linear equations to a system of equations d y A second-order linear ODE of the form + + cy r( x ), can be transformed into a bdy linear system of equations by introducing the relationship, u( x ) thus, the equation reduces to + bu+ cy r( x ), or system of equations is: du du du bu cy + r( x ), dy u. dy d, so that y du, bu cy + r( x ). The resulting Which can be written in matricial form as df/ A f(x)+g(x), with u b c r( x) f ( x), A, ( ). g x y For example, the solution to the second order differential equation 8

d y 5 dy + 3 y x, can be obtained by solving the equivalent first-order linear system: du 5 u + 3 y+ x, dy u. The procedure outlined above to transform a second order linear equation can be used to convert a linear equation of order n into a system of first-order linear equations. For example, if the original ODE is written as: d n y + n a n d ( n ) y d +... + y a ( n ) + a dy + a y ( ) r x, we can re-write it as d n y n a n d ( n ) y d -... - y a ( n ) - a dy - a y + ( ) r x, and transform it into a system of n first-order linear equations given by: du n a u n n - a u n n -... - a u - a u - a y + r( x ), du n du n 3 n, u u du n,..., dy u, u, or, in matricial form, un un M f ( x) u u y, a A, M n a n M a n 3 M L L L L O L a M a M r( x) M g( x) For example, to transform the following fourth-order (n4) linear ODE 9

subjected to we would write: or dy y, d -, y d 4 y 3 d 3 y d y 5 dy + + + y, 4 3 d, 3 y -, at x, into a first-order linear system, 3 du 3 / -3u 3 (x)+u (x)-5u (x)-y(x)+x /, du / u 3 (x), du / u (x), and dy/ u (x), u3 ( x) 3 5 u3( x) x / ( ) ( ) d u x u x +. u( x) u( x) y( x) y( x) with v(x) [u 3 (x);u (x);u (x); y(x)] T, A [-3,,-5,-;,,,;,,,;,,,]; and g(x) [x /;; ; ] T, the system of differential equations is written as dv/ Av+g(x). The initial conditions are y(), u () dy/ -, u () du / d y/, u 3 () du / d u / d 3 y/ 3 -, or u [ -;;-;]. Initial-value problems Any differential equation of the form df/ A f(x)+g(x), subject to initial conditions f(x ) f, is referred to as an initial-value problem (IVP). For a constant matrix A, the solution can be found using one of several functions available in Matlab. Some of these functions are: ode3: IVP solver of order or 3 ode45: IVP solver of order 4 or 5 Use the following commands to get additional information on these functions:» help ode3» help ode45 Also, read Chapter 8 (Ordinary Differential Equations) in the Using Matlab guide for additional applications of these functions.