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

Matlab ABC

Embed Size (px)

DESCRIPTION

Buen tutorial que abarca:\Solución simbólica de ecuaciones diferenciales de primer orden, de segundo orden, y sistemas de ecuaciones diferenciales.\Idioma: Ingles Paginas:113 Valoración: Excelente Formato: pdf (Adobe Acrobat)

Citation preview

Page 1: Matlab ABC

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 typeyour 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 yournew 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 solvesystem of algebraic equations, compute the eigenvalues and eingenvectors of a matrix, findthe 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 differentialequations! That is what we are supposed to do. Moreover, you can use its wonderful graphicsand visualization facilities to generate the curves, surfaces, motion pictures, etc. CertainlyMatlab 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 theCommand Window.

1

Page 2: Matlab ABC

Now you have entered the Matlab world. All you need is to type a command after theprompt, >>, 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/4a =

4.7500>>

That means that you are supposed to calculate a = 2 · (−2) + 32− 1/4 and Matlab givesyou the answer 4.7500. The output is stored in a. If you type a = 2 ∗ (−2) + 32 − 1/4; (endup 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 valueof 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 storedin ans. You will see

2

Page 3: Matlab ABC

>> 2 ∗ (−2) + 3∧2− 1/4ans =

4.7500>>

You can type two or more commands simultaneously. But you have to separate them byeither 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 abuild-in function. Certainly it an approximation of π. Type help elfun to see other basicfunctions.

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

Page 4: Matlab ABC

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′+2y =3x, type

>> dsolve(′Dy + 2 ∗ y = 3 ∗ x′,′ x′)ans =3/2 ∗ x− 3/4 + exp(−2 ∗ x) ∗ C1>>

Matlab gives you the solution 32x− 3

4+ C1e

−2x. In your command D stands for the firstderivative. (Note it is capital D!!!) The equation is included in ’ ’. ’x’ after the commaindicates that x is the independent variable. If you don’t type ’x’ Matlab will automaticallytake t as the independent variable and treats x in the equation as some constant. Then youwill get a wrong answer. If you want the solution to be stored in a variable y type

>> y = dsolve(′Dy + 2 ∗ y = 3 ∗ x′,′ x′)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 thefunction (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

Page 5: Matlab ABC

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

>>

Want to solve the initial value problem

y′ = cos(x+ y), y(0) =π

4?

Just type

>> y = dsolve(′Dy = cos(x + y)′,′ y(0) = pi/4′,′ x′); 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 2ndorder derivative. Want to solve the Cauchy-Euler equation

x2y′′ − xy′ + y = 0?

Type

>> y = dsolve(′x∧2 ∗ D2y− x ∗ Dy + y = 0′,′ x′); pretty(y)C1 x + C2 x log(x)

>>

So the solution is y = C1x+ C2x lnx.Let us solve the problem

y′′ − 2y′ + y = et arctan t, y(0) = 1, y′(0) = 2.

Type

>> y = dsolve(′D2y− 2 ∗ Dy + y = exp(t) ∗ atan(t)′,′ y(0) = 1′,′ Dy(0) = 2′)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

Page 6: Matlab ABC

(I didn’t put ’t’ in the end of the command. Why?)So the solution is

y =1

2t2et arctan t+

3

2tet − 1

2et arctan t− 1

2tet ln(t2 + 1) + et.

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

dt= 3x− y

dy

dt= x+ y

type

>> S = dsolve(′Dx = 3 ∗ x− y′,′ Dy = x + y′)S =

x : [1x1 sym]

y : [1x1 sys]

>>

You can’t see the solution x, y directly!! To see the solution simply type

6

Page 7: Matlab ABC

>> S.x, S, yans=

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(C1t+ C2 + C2t), y = e2t(C1 − C1t+ C2t).To solve the initial value problem

X′ =

[1 −11 −1

]X +

[1/t1/t

], X(1) =

[2−1

],

type

>> S = dsolve(′Dx = x− y + 1/t′,′ Dy = x− y + 1/t′,′ x(1) = 2′,′ y(1) = −1′);>>

(Why S doesn’t appear?) Then type

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

3 t − 1 + log(t)

−4 + 3 t + log(t)

7

Page 8: Matlab ABC

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(′−x + 2 ∗ atan(x + sqrt(2)− 1)′, [0, 2])

another window opens and you can see the curve. In this way you can draw the curve ofa particular ODE solution. Just type a particular solution you have got by dsolve. For aparticular 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 numbersand type the whole formula.

Want to see the curve of the parametric function

x = et2 (3 cos t− sin t), y = e

t2 (2 cos t+ sin t); [0, 2π]?

Type

>> ezplot(′exp(t/2) ∗ (3 ∗ cos(t)− sin(t))′,′ exp(t/2) ∗ (2 ∗ cos(t) + sin(t))′, ...[0, 2 ∗ pi])

(Here the command is too long. I can’t type it on a single line. So I typed ... andthen pressed ”Enter” and then typed the rest on the second line. ... is used to break thecommand 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 youprint!)

To close the graph window you can simply click the ”×” at the upper-right corner of thewindow.

More about the graphs. Another way to plot a curve is to use plot. Use help plot tosee 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 havingplotted the first curve.

You can type title(’bla,bla,bla’) to change the title on the graph. The old oneusually is the functions the new one is bla,bla,bla.

8

Page 9: Matlab ABC

2.8 How to save the text of Matlab session?

You may save the some commands and output showed up on the Command Window. Sayyou want to save the displays for solving

Type diary myfile (or any name you want) and ”Enter”. Matlab will create a file calledmyfile for you. After having done some Matlab work type diary off. Then everything hasever displayed on the Command Window between diary myfile and diary off is saved inthe file myfile. You can type diary on to suspend the subsequent input and output to thesame 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” andthen 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 commandcan be recalled by typing the first few characters followed by ↑. This may help you tore-use the old command.

• ← and → keys can be used to move the cursor along a line. This helps to do thecorrections 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 orexit. The window will be closed.

9