Escolar Documentos
Profissional Documentos
Cultura Documentos
1 What is Matlab?
Matlab is an interactive system for numerical computation. You can use Matlab to solve
system of algebraic equations, compute the eigenvalues and eingenvectors of a matrix, find
the roots of a polynomial. But you can also use it to do calculus! You can find the derivatives,
integral of a function in less than one second. You certainly can use it to solve the differential
equations! That is what we are supposed to do. Moreover, you can use its wonderful graphics
and visualization facilities to generate the curves, surfaces, motion pictures, etc. Certainly
Matlab can do much more. If you want to know more about Matlab, you can visit
http://www.math.ukans.edu/docs/
Another way is, double-click the icon ”terminal” and you will see an xterm window.
Type ”matlab” or ”matlab6” (there are two versions) and press ”Enter” you will see the
Command Window.
1
Now you have entered the Matlab world. All you need is to type a command after the
prompt, >>, and Matlab will work for you.
• − — subtraction
• ∗ — multiplication
• ∧ — power
You now can do some basic arithmetic. Type ”a = 2 ∗ (−2) + 3∧ 2 − 1/4” and press the
”Enter”. You will see
>> a = 2 ∗ (−2) + 3∧ 2 − 1/4
a=
4.7500
>>
That means that you are supposed to calculate a = 2 · (−2) + 32 − 1/4 and Matlab gives
you the answer 4.7500. The output is stored in a. If you type a = 2 ∗ (−2) + 32 − 1/4; (end
up with a semi-colon (;) then ”Enter”. You will see
>> a = 2 ∗ (−2) + 3∧ 2 − 1/4;
>>
The output doesn’t display but the result is still stored in a. If you want to see the value
of a, type
>> a
a=
4.7500
>>
You can see the output again! If you type 2 ∗ (−2) + 3∧ 2 − 1/4 then the result is stored
in ans. You will see
2
>> 2 ∗ (−2) + 3∧ 2 − 1/4
ans =
4.7500
>>
You can type two or more commands simultaneously. But you have to separate them by
either a ”;” (without display) or a ”,’(with display). Try!
Here you need to use the parentheses () for the assigned data. pi actually is also a
build-in function. Certainly it an approximation of π. Type help elfun to see other basic
functions.
If you have questions about these functions you can always type
help function name. For example help exp
Matlab will explain how to use the code exp!
3
2.4 How to solve a first-order differential equation?
The build-in code dsolve is for solving ordinary differential equations. (Again type help
dsolve you can see the explanations.) If you want to solve the differential equation y 0 + 2y =
3x, type
>> dsolve(0 Dy + 2 ∗ y = 3 ∗ x0 ,0 x0 )
ans =
3/2 ∗ x − 3/4 + exp(−2 ∗ x) ∗ C1
>>
Matlab gives you the solution 32 x − 34 + C1 e−2x . In your command D stands for the first
derivative. (Note it is capital D!!!) The equation is included in ’ ’. ’x’ after the comma
indicates that x is the independent variable. If you don’t type ’x’ Matlab will automatically
take t as the independent variable and treats x in the equation as some constant. Then you
will get a wrong answer. If you want the solution to be stored in a variable y type
>> y = dsolve(0 Dy + 2 ∗ y = 3 ∗ x0 ,0 x0 )
y=
3/2 ∗ x − 3/4 + exp(−2 ∗ x) ∗ C1
>>
Sometimes the solution looks long and ugly. You may use
y=simplify(y)
and try to get a nice formula. (The function is still in y. But if you want store the
function (solution) in a new variable z, simply type z=y or directly type z=simplify(y)
before doing simplification.)
If you want the display looks even better. Type
>> pretty(y)
y=
3/2 x - 3/4 + exp(-2 x) C1
>>
4
>> y = dsolve(0 Dy + 2 ∗ y = 3 ∗ x0 ,0 x0 ); pretty(y)
3/2 x - 3/4 + exp(-2 x) C1
>>
So the solution is y = C1 x + C2 x ln x.
Let us solve the problem
y 00 − 2y 0 + y = et arctan t, y(0) = 1, y 0 (0) = 2.
Type
>> y = dsolve(0 D2y − 2 ∗ Dy + y = exp(t) ∗ atan(t)0 ,0 y(0) = 10 ,0 Dy(0) = 20 )
y=
1/2 ∗ exp(t) ∗ t2 ∗ atan(t) + 3/2 ∗ exp(t) ∗ t − 1/2 ∗ exp(t) ∗ atan(t) − 1/2 ∗ exp(t) ∗ t ∗
log(t∧ 2 + 1) + exp(t)
>>
5
(I didn’t put ’t’ in the end of the command. Why?)
So the solution is
1 3 1 1
y = t2 et arctan t + tet − et arctan t − tet ln(t2 + 1) + et .
2 2 2 2
If you now type pretty(y) you will see
>> pretty(y)
>>
type
>> S = dsolve(0 Dx = 3 ∗ x − y0 ,0 Dy = x + y0 )
S=
x : [1x1 sym]
y : [1x1 sys]
>>
You can’t see the solution x, y directly!! To see the solution simply type
6
>> S.x, S, y
ans=
ans =
exp(2 t) (−C1 t + C2 + C2 t)
exp(2 t) (C1 − C1 t + C2 t)
>>
type
>> S = dsolve(0 Dx = x − y + 1/t0 ,0 Dy = x − y + 1/t0 ,0 x(1) = 20 ,0 y(1) = −10 );
>>
3 t − 1 + log(t)
−4 + 3 t + log(t)
7
2.7 How to plot a curve?
√
If you want to sketch the curve of function y = −x + 2 arctan(x + 2 − 1) on the interval
[0, 2], one easy way is to type
>> ezplot(0 −x + 2 ∗ atan(x + sqrt(2) − 1)0 , [0, 2])
another window opens and you can see the curve. In this way you can draw the curve of
a particular ODE solution. Just type a particular solution you have got by dsolve. For a
particular solution stored in y you can simply type ezplot(y,[0,2]). But if there are C1,
C2 in the formula you can not do it in this way. You have to replace them by some numbers
and type the whole formula.
Want to see the curve of the parametric function
t t
x = e 2 (3 cos t − sin t), y = e 2 (2 cos t + sin t); [0, 2π]?
Type
>> ezplot(0 exp(t/2) ∗ (3 ∗ cos(t) − sin(t))0 ,0 exp(t/2) ∗ (2 ∗ cos(t) + sin(t))0 , ...
[0, 2 ∗ pi])
(Here the command is too long. I can’t type it on a single line. So I typed ... and
then pressed ”Enter” and then typed the rest on the second line. ... is used to break the
command into two lines.)
Again another window opens and gives you the curve in the xy-plane.
You can use this command to draw the trajectory of of a system of equations. If you
have got a particular solution stored in S. You can simply type ezplot(S.x,S.y,[0,2∗pi])
To print the graph click the print icon on the par above the curve (in the graph window),
or simply type print on the Command Window. (Don’t close the graph window before you
print!)
To close the graph window you can simply click the ”×” at the upper-right corner of the
window.
More about the graphs. Another way to plot a curve is to use plot. Use help plot to
see the instructions.
you can use axis(Xmin Xmax Ymin Ymax) to re-scale the axis of a graph. Here Xmin,
Xmax, Ymin Ymax are real numbers, giving the interval [Xmin,Xmax] for x and [Ymin, Ymax]
for y.
If you want to plot several curves in a same graph, type hold on before or after having
plotted the first curve.
You can type title(’bla,bla,bla’) to change the title on the graph. The old one
usually is the functions the new one is bla,bla,bla.
8
2.8 How to save the text of Matlab session?
You may save the some commands and output showed up on the Command Window. Say
you want to save the displays for solving
Type diary myfile (or any name you want) and ”Enter”. Matlab will create a file called
myfile for you. After having done some Matlab work type diary off. Then everything has
ever displayed on the Command Window between diary myfile and diary off is saved in
the file myfile. You can type diary on to suspend the subsequent input and output to the
same file myfile.
To see the file myfile double-click the ”files” icon you will see a folder. Then double-click
”myfile” icon you will see file. To print it click ”File” on the top bar and click ”printer” and
then click ”print”.
You can also use ”Xterm” window. Then you just type lpr myfile.
• ↑ and ↓ keys can be used to scroll through your previous commands. An old command
can be recalled by typing the first few characters followed by ↑. This may help you to
re-use the old command.
• ← and → keys can be used to move the cursor along a line. This helps to do the
corrections in a command.