Você está na página 1de 9

Math 320: Matlab ABC.

0. How to login and logout a computer?


Type your user name and press ”Enter”. Then type your password and press ”Enter”.
After you have logged in, you can change your password (if you want). Click the ”termi-
nal” icon. Type passwd, then type your current password after ”Old password”, then type
your new password (usually a combination of 8 digits and letters) after ”New password”,
then repeat your new one after ”Retype new password”. Now you need to memorize your
new password and use it next time.
After you having done your work, click the ’logout’ icon, then click ’exit’.

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/

and click Matlab Primer or Matlab introduction.


You can also type tour or demo on the Command Window to have a brief tour. Certainly
you can go to the library to borrow a guide book. (There are many these kind of books!!)

2 How to use Matlab?


2.1 Open the Matlab Command Window.
Double-click the Matlab icon. Wait for couple seconds. A Command Window will jump out,
which looks like
>>

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.

2.2 Basic arithmetic.


• + — addition

• − — subtraction

• ∗ — multiplication

• / — division (right division for matrices)

• \ — division (left division for matrices)

• ∧ — 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!

2.3 Build-in functions


Matlab has many build-in functions. Certainly it includes the following basic functions

• sin — sine function

• cos — cosine function

• log — natural log function

• exp — natural exponential function

• sqrt — square root function

• tan — tangent function

• asin —arcsine function

and so on. If you want to calculate sin π2 , you type


>> sin(pi/2)
ans =
1
>>

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
>>

You can also type two commands on the same line

4
>> y = dsolve(0 Dy + 2 ∗ y = 3 ∗ x0 ,0 x0 ); pretty(y)
3/2 x - 3/4 + exp(-2 x) C1
>>

Want to solve the initial value problem


π
y 0 = cos(x + y), y(0) = ?
4
Just type
>> y = dsolve(0 Dy = cos(x + y)0 ,0 y(0) = pi/40 ,0 x0 ); pretty(y)
−x + 2 atan(x + 21/2 − 1)
>>

Found the initial condition in the command?

2.5 How to solve a 2nd-order differential equation?


Same as the first-order equations. The only thing we need to know is that D2 means the 2nd
order derivative. Want to solve the Cauchy-Euler equation
x2 y 00 − xy 0 + y = 0?
Type
>> y = dsolve(0 x∧ 2 ∗ D2y − x ∗ Dy + y = 00 ,0 x0 ); pretty(y)
C1 x + C2 x log(x)
>>

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)

1/2 exp(t) t2 atan(t) + 3/2 exp(t) t − 1/2 exp(t) atan(t)


−1/2 exp(t) t log(t2 + 1) + exp(t)

>>

2.6 How to solve a system of differential equation?


Just input the equations simultaneously, separated by ’,’.
To solve
dx
= 3x − y
dt
dy
= x+y
dt

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=

exp(2 ∗ t) ∗ (−t ∗ C1 + C2 + t ∗ C2)

ans =

exp(2 ∗ t) ∗ (C1 − t ∗ C1 + t ∗ C2)


>>

If you want a good looking solution type


>> pretty(S.x), pretty(S.y)

exp(2 t) (−C1 t + C2 + C2 t)

exp(2 t) (C1 − C1 t + C2 t)
>>

So the solution is x = e2t (C1 t + C2 + C2 t), y = e2t (C1 − C1 t + C2 t).


To solve the initial value problem
     
0 1 −1 1/t 2
X = X+ , X(1) = ,
1 −1 1/t −1

type
>> S = dsolve(0 Dx = x − y + 1/t0 ,0 Dy = x − y + 1/t0 ,0 x(1) = 20 ,0 y(1) = −10 );
>>

(Why S doesn’t appear?) Then type


>> pretty(S.x), pretty(S.y)

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.

2.9 Some points


• In Matlab upper and lower letter are not the same

• Parentheses () and square brackets [] are not interchangeable

• ↑ 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.

2.10 How to Close the Command Window?


Simply click the ”×” at the upper-right corner of the Command Window, or type quit or
exit. The window will be closed.

Você também pode gostar