0% found this document useful (0 votes)
148 views

Ode 45 Example

This document provides instructions for using MATLAB to numerically solve ordinary differential equations (ODEs). It discusses using the ode45 function to solve first-order ODEs and systems of ODEs. It also covers using MATLAB's eigenvalue and eigenvector functions to find the general solution to linear systems of first-order ODEs with constant coefficients. Several examples are provided to illustrate these techniques.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
148 views

Ode 45 Example

This document provides instructions for using MATLAB to numerically solve ordinary differential equations (ODEs). It discusses using the ode45 function to solve first-order ODEs and systems of ODEs. It also covers using MATLAB's eigenvalue and eigenvector functions to find the general solution to linear systems of first-order ODEs with constant coefficients. Several examples are provided to illustrate these techniques.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MTH U345

Ordinary Dierential Equations

Fall 2008

Lab 3: Using MATLAB for Dierential Equations 1


We are now familiar with using a spreadsheet to set up numerical methods for approximating solutions of a dierential equation. In this computer lab, we shall not only learn how to use MATLAB to obtain numerical solutions of 1st-order equations of the form x = f (t, x), but we shall use its algebraic capabilities to obtain general solutions to linear 1st-order systems with constant coecients. I. 1st-ORDER EQUATIONS (ode45). MATLAB has several numerical procedures for computing the solutions of rst-order equations and systems of the form y = f (t, y ); we shall concentrate on ode45, which is a souped-up Runge-Kutta method. The rst step is to enter the equation by creating an M-le which contains the denition of your equation, and is given a name for reference, such as dieqn (the sux .m will be added to identify it as an M-le.). The second step is to apply ode45 by using the syntax: (1) [t, y] = ode45( dieqn , [t0 , tf ], y0 );

where t0 is the initial time, tf is the nal time, and y0 is the initial condition, y(t0 ) = y0 . The same syntax (1) works for equations and systems alike. Example 1. y = y 2 t, y (0) = 0, for 0 t 4. 1. Creating the M-le. Start up MATLAB; the Command Window appears with the prompt >> awaiting instructions. Choose New from the File menu, and select M-le. You are now in a text editor where you create MATLAB les. Enter the following text: function ypr=example1(t,y) ypr=y^2-t; Name this M-le example1.m by selecting Save As from the File menu. Note: The semicolon at the end tells MATLAB to suppress displaying output. (If you leave out the semicolon and run ode45, MATLAB will display a lot of calculations that you dont need to see.) 2. Running ode45. Return to the Command Window, and enter the following: >> [t, y] = ode45( example1 , [0, 4], 0); The [0, 4] tells MATLAB to consider 0 t 4 and the last 0 tells it to start at y = 0. When you hit the enter key, MATLAB will do its computing, then give you another prompt. 3. Plotting the Solution. You can plot the solution y (t) by typing >> plot(t, y)

MTH U345

Lab 3

Fall 2008

and hitting the enter key. To give your plot a title and axes labels, type >>title( The solution to y >>xlabel( t ) >>ylabel( y ) and hit the enter key after each line. Notice that each title/label is identied by single quotation marks, e.g. The solution... . Note: You might expect that the title line should read y = y2 t instead of y = y2 t, but the former would indicate to MATLAB that the title ends with y , so we must put in the extra single quote (i.e. is two single quotes, not one double quote). You can also have MATLAB tabulate the t-values it has selected and the y -values it has computed by entering >> [t, y] in the Command Window. This should produce a vertical column of numbers, the last of which is t = 4.0000 and y = 1.9311, i.e. y (4) = 1.9311 as appears in the plot. Exercise 1. Consider the initial value problem y = t2 + cos y , y (0) = 0 which was encountered in Exercise 4 of Lab 3. Use MATLAB to plot the solution for 0 t 1, and nd the approximate value of y (1). Hand In: A printout of your plot and the value of y (1). II. LINEAR 1st-ORDER SYSTEMS (eigenvalues & eigenvectors) Recall that a rst-order system of linear dierential equations with constant coecients may be expressed in matrix notation as dY (2) = AY, dt where Y (t) is a vector-valued function and A is a square matrix (with constant coecients). Moreover, if 1 is an eigenvalue for A (i.e. det(A 1 I ) = 0) with associated eigenvector V1 (i.e. AV1 = 1 V1 ), then (3) Y (t) = e1 t V1 is a solution of (2). We shall now use MATLAB to compute the eigenvalues and eigenvectors of a given square matrix A, and therefore calculate the solutions of (2). The rst step is to enter the given matrix A: this is done by enclosing in square brackets the rows of A, separated by semicolons. If we only need the eigenvalues of A, then we can let E = eig (A), and the eigenvalues appear as the column vector E . If we want the eigenvalues and eigenvectors of A, then we can enter [V, D] = eig (A) in order to get two matrices: the matrix V has (unit length) eigenvectors of A as column vectors, and D is a diagonal matrix with the eigenvalues of A on the diagonal. Example 2. Suppose we want to nd the eigenvalues and eigenvectors for (4) A= 4 2 , 1 3 = y^2 t with y(0) = 0. )

MTH U345

Lab 3

Fall 2008

and use them to nd the general solution of (2). Enter the matrix A as follows: >> A = [4 2; 1 3] Now request the eigenvalues of A by entering >> E = eig(A) MATLAB displays the eigenvalues 5 and 2 as the column vector E . Finally, request eigenvectors and eigenvalues of A by entering >> [V, D] = eig(A). MATLAB displays the following: V= 0.8944 0.7071 0.4472 0.7071 D= 5 0 0 2. (Actually, 0.8944 may appear as 8.9443e-01, where e-01 means to multiply by 101 .) The matrix D has the eigenvalues 5 and 2 on the diagonal; the eigenvector corresponding to 5 appears as the rst column of the matrix V , namely V1 = (0.8944, 0.4472). Notice that this is a unit length eigenvector since (0.8944)2 + (0.4472)2 1 (with some small round-o error). Since we can multiply both components of an eigenvector by the same number and still get an eigenvector, we could instead take V1 = (2, 1). Similarly, we could replace the eigenvector V2 = (0.7071, 0.7071) corresponding to the eigenvalue 2 by V2 = (1, 1). This means that we have found two linearly independent solutions of (2), Y1 (t) = 2 1 e5t and Y2 (t) = e2t , and we can write the general solution as 1 1 (5) Y (t) = C1 e5t 2 1 2C1 e5t C2 e2t + C2 e2t = . 1 1 C1 e5t + C2 e2t

If we were given an initial condition Y (0), then we could evaluate C1 and C2 . Exercise 2. Consider the system of equations dx = 4x 2y dt dy (6) = x + y. dt (a) Letting Y = x , introduce a matrix A so that (6) is in the form (2). y

MTH U345

Lab 3

Fall 2008

(b) Use MATLAB to determine the eigenvalues and eigenvectors of A. (c) Use (b) to nd two linearly-independent solutions and the general solution of (6). (d) Use (c) to nd the solution of (6) satisfying the initial conditions x(0) = 1 and y (0) = 1. Example 3. Suppose we let (7) Proceeding as before, we obtain V= .40825 + .40825i .40825 .40825i .81650i .81650i D= 4 + 2i 0 0 4 2i A= 2 2 . 4 6

which means that A has complex eigenvalues 1 = 4 + 2i, 2 = 4 2i, and associated eigenvectors V1 = (1 + i, 2i), V2 = (1 i, 2i). This means that one solution of (2) is given by Y1 (t) = e(4+2i)t 1+i 1+i = e4t (cos 2t + i sin 2t) , 2i 2i

and the general solution is given by Y (t) = C1 e4t cos 2t sin 2t cos 2t + sin 2t + C2 e4t . 2 sin 2t 2 cos 2t

Given an initial condition Y (0), we could evaluate C1 and C2 . Exercise 3. Consider the system of equations dx = x 4y dt dy (8) = 3x 2y. dt (a) Use MATLAB to determine the eigenvalues and eigenvectors of the associated matrix. (b) Use (a) to nd two linearly-independent solutions and the general solution of (8). (c) Use (b) to nd the solution of (8) satisfying the initial conditions x(0) = 1 and y (0) = 1.

You might also like