Sie sind auf Seite 1von 10

Using MATLAB To Solve Linear Systems Of Differential Equations

The MATLAB command window


When MATLAB starts, it displays a splash screen and then opens the command window.

MATLAB is designed to run on just about any kind of computer with any kind of graphics capability, so it does not rely on a graphical
interface. Instead, commands are entered as text inside the command window. The commands are typed at the MATLAB prompt
and then the command is executed when the enter key is pressed.
For example, we set one variable x to be 4 and another variable y to be 5 and then set z equal to their sum. Note that when we end a
line with a ;, the output is suppressed. We might want to supress the output for example when entering a very large matrix or if we
are assigning values inside a loop.

Entering vectors and matrices
We enter a vector or a matrix in MATLAB as a long list of numbers using a , to separate entries in the same row and a ; to
separate entries in one row from the next. Below we enter

1 3 3 2
(1, 2, 3), 2 , 12 0 9
3 3 3 4
x y A
| | | |
| |
= = =
| |
| |

\ . \ .
G G



in the MATLAB command window

Note that we use square paranthesis [ and ] to denote the start and end of the entries.
We can now do matrix and vector operations. We use + - * for addition, subtraction, and multiplication. Here are some sample
computations.

Solving a linear system
To solve a linear system, we use the forward slash command \. For example to solve

2 1 2 1
1 0 9 0
11 2 4 4
x
| | | |
| |
=
| |
| |
\ . \ .
G

we do



Note that MATLAB computes a very accurate approximation to the true solution in general, so the result will generally be a decimal
number. We can format the output by using the MATLAB command format. format short gives five digits, format long gives
15 digits, format short e gives 5 digits in exponential format, format long e gives 15 digits in exponential format.


MATLAB commands
The MATLAB command window is an interface to a library of sophisticated functions that perform a variety of operations on vectors
and matrices. To get help on these type help at the prompt or use the mouse to open the help window from the menubar. We are
going to be using the command eig which computes approximate eigenvalues and eigenvectors. Typing help eig at the MATLAB
prompt gives this



The input into eig is a matrix and the output is either a vector or two matrices. If we type e=eig(A) then e will be a column
vector containing approximations to the eigenvalues of A. If we type [V,D]=eig(A) then V will be a matrix containing
approximate eigenvectors of A while D is a diagonal matrix whose nonzero diagonal entries are the eigenvalues in the same order
as the eigenvectors contained in V. Here is an example from the notes on page 184

In this example, the diagonal matrix Val (note we can use more than one letter to name a variable) has diagonal entries 1, 3, and 2.
So the eigenvalues are

1 2 3
1, 3, 2 = = =
The corresponding eigenvectors are approximately

1 2 3
0 .5774 .3333
1 , .5774 , .6667
0 .5774 .6667
v v v
| | | | | |
| | |
=
| | |
| | |

\ . \ . \ .
G G G



We can display more digits in the approximate eigenvectors

MATLAB always normalizes the eigenvectors to have length 1. It does this by computing an eigenvector and then dividing by its
length

v
v
G
G

In the notes, we computed the eigenvectors
1 2 3
0 1 1
1 , 1 , 2
0 1 2
v v v
| | | | | |
| | |
= = =
| | |
| | |

\ . \ . \ .
G G G

We can use the MATLAB norm function to change these vectors into unit vectors

These are the same vectors as in the columns of Vec.
We do the first problem on page 185 in the notes:



We get the same eigenvalues as given in the answer, but not the same eigenvectors for the eigenvalue 3. But recall that any linear
combination of the eigenvectors associated to a particular eigenvalue is another eigenvector, so MATLAB has just computed two
linear combinations of the eigenvectors given in the answer. We can verify that the vectors given in the answer are eigenvectors

We can see the answers are eigenvectors for the eigenvalue 3 immediately. We can also verify that the vectors in the columns of
Vec are eigenvectors. To do this, we have to extract the columns into vectors. This is easy to do in MATLAB



Then we check that the vectors are eigenvectors

We can see these are eigenvectors for 3, 3, and 6 immediately.
MATLAB has a great deal of difficulty dealing with deficient matrices. This is problem 3 on page 190 of the notes.



MATLAB correctly computes the eigenvalue 1 with multiplicity 3. But we have to look at the columns of Vec and realize that there
is only one eigenvector. MATLAB generally can only compute the entries in the eigenvectors to at most fifteen places of accuracy, so
vectors that are different in the sixteenth digit or later will be considered to be the same. If the matrix is deficient, then we can get
considerably less accuracy especially if the matrix is big. If we look at the values in Vec in the long e format, we see

So the first and second columns of Vec are equal as far as the accuracy we can get out of MATLAB.
MATLAB has no problems dealing with complex eigenvectors and eigenvalues. This is problem 6 on page 190 of the notes.



We get the same eigenvalues, 4i and 4i, as given in the answer for the problem, but as usual MATLAB gives eigenvectors that have
been normalized to have norm 1. We can get the real and imaginary parts of Vec using the MATLAB commands real and imag,

We solve problem 21 in section 5.2:

5 0 6
2 1 2
4 2 4
x x
| |
|
=
|
|

\ .
G G




So the general solution is

0
1 2 3
.8018 .6667 .7442
( ) .2673 .3333 .2481
.5345 .6667 .6202
t t
x t c e c e c e

| | | | | |
| | |
= + +
| | |
| | |

\ . \ . \ .
G

We can use this to solve the initial value problem


5 0 6
2 1 2
4 2 4
3
(0) 2
4
x x
x
| |
|
=
|
|


\ .

| |

=
|

\ .
G G

G

We have to compute c
1
, c
2
, c
3
so that
1 2 3
3 .8018 .6667 .7442
(0) 2 .2673 .3333 .2481
4 .5345 .6667 .6202
x c c c
| | | | | | | |
| | | |
= = + +
| | | |
| | | |

\ . \ . \ . \ .
G

or
1
2
3
.8018 .6667 .7442 3
.2673 .3333 .2481 2
.5345 .6667 .6202 4
c
c
c
| || | | |
| | |
=
| | |
| | |

\ .\ . \ .

Note that the matrix for this system is exactly Vec!. Hence we can solve the initial value problem easily,

and therefore
0
3 .8018 .6667 .7442
( ) 2 33.6749 .2673 27.0000 .3333 64.4981 .2481
4 .5345 .6667 .6202
t t
x t e e e

| | | | | | | |
| | | |
= = + +
| | | |
| | | |

\ . \ . \ . \ .
G

Das könnte Ihnen auch gefallen