Work with the MATLAB windows

After clicking on the icon MATLAB 5.3, you are now aboard onto MATLAB window, the so-called command window, the Matlab workspace, where you will do all calculations, programming and editings. You may see now the following manual on the screen:

MATLAB Command Window

 

File


 

Edit

 

Options

 

Windows

 

Help

 

 

 

 

 

>>

 

 

 

 

Type ver to see the version. Type path to check the path setting. Save your work in disk. Anything you have typed in or any calculation you have run or any program you have written down in Matlab pages will disappear right away after your leaving the Matlab or log off the computer. Everething will be gone if you do not save them into your disk in A drive. Let us start by learning the M-files. Since only very limited number of functions are commercially built in Matlab software which are much less than we need to attack various problems, we have to create new functions, called M-functions. You have also to write programs, in the so-called M-files to meet the need of this course. That is why you must learn the following things.

1. Create a new m-file in A drive Example 1


1. Click on the buttom

File

on the tool bar to scrool down the manual.
2. Click the item ``New", then,

New M-file

. The Edtitor/Debugger page along with pad page for editing a m-file is available to you.. Then edit the file by entering a piece of text in the page, e.g., the following text:

Home Work 1    (Enter)

``I shall save all related formula, programs, and resulting data of computations in this m-file of hw1.m". You must place the .m at the end of the file name hw1 when saving the file so that it can be recogized, recalled and searched later on by the Matlab.
3. Save a file. Now you should save the file. Click the ``File" and then further at

Save as

in the scrool-down manual. A dialog box appears. The next thing is to click / type the drive a and enter the file mane hm1.m following the hints on the screen . Having done that, you click buttoms

ok

,

X

for exit and back to Matlab command window for editing other files.
4. Open an existing M-file for review or editing. To double check if the file you wanted to save did save, simply click the "File" (remenber, you are now at Matlab command window), and then go for ``Open M-file." Select the drive a: and you should find the ``hw1.m " already there. You may

open

to review the file.

2. To add formula and data to an existing M-file in your disk Example 2: Use of Diary Turn you computer on to the MATLAB command page. Then you should see the cursor: >> . Suppose you want to save the result of the calcultion of [((p/2)n)/n!] for n=1,2,¼, 20 which are needed for a problem in Home work 1. You can type first (recall that the built-in-function gamma(n) in Matlab means gamma(n+1)=n!): >> : diary a: hw1.m (Enter) >> : n=1:20;  E=((pi./2).Ùn)./gamma(n+1); (Enter) >> : format long (Enter) >> : [n¢,E¢] (Enter) (Here the command of transpose n¢ makes the vector of n=1,2, ¼,20 vertically displayed.) You should see by now the following result: ans =

 

n

E

 

 

1.00000000000000

1.57079632679490

2.00000000000000

1.23370055013617

3.00000000000000

0.64596409750625

4.00000000000000

0.25366950790105

5.00000000000000

oNormal align=center style='text-align:center'>

0.02086348076335

7.00000000000000

0.00468175413532

8.00000000000000

0.00091926027484

9.00000000000000

0.00016044118479

10.00000000000000

0.00002520204237

11.00000000000000

0.00000359884324

12.00000000000000

0.00000047108748

13.00000000000000

0.00000005692173

14.00000000000000

0.00000000638660

15.00000000000000

0.00000000066880

16.00000000000000

0.00000000006566

17.00000000000000

0.00000000000607

18.00000000000000

0.00000000000053

19.00000000000000

0.00000000000004

20.00000000000000

0.00000000000000

 

Following these data, if nothing else needs to collect, you must type >> : diary off to finish the data collecting. Otherwise some garbage message you don't need will invade later on into your file of hw1.m. The command ``diary" stands for open a file while ``diary off" for close. We used in the above the command ``format long " for 15-decimal digits accuracy. In contrast to the "long", a "format short" yields 5-digits output. By now these data have been saved into your disk under the file hw1.m.

3. To add a piece of text to an existing M-file in your disk See the step 4 in Example 1.

4. Complile a m-function as a M-file into your disk Since only limited number of functions have been built in MATLAB software, you need to create new functions to meet your need. ``M-function files are one of the fundamental strength of MATLAB. They allow you to encapsulate and create a sequence of useful formula and commands and apply them over and over."
Example 3. Suppose you need to compute a couple of times the function y=x2-ex+sin(3x+1). Say, you would like to evaluate this function at sequence of points x=5.62, x=1.20, x=-2.5. Repeat the steps of Example 1 for create a new M-file. Type the following text: function y=myfun(x) (This first line is called declaration line. do not place any punctuation mark at its end !) y=x.Ù2-exp(x)+sin(3.*x+1); (Be sure to type the semicolon

;

at the end.) NOTE

·         1. You must save this file with a name which is exactly identical to the name of your m-function, say, by this example, the file name has to be myfun.m.

·         2. You must place the semicolon

;

·         at the end of the mathematical expression. It suppresses the immediate numerical excution and printing of the function. This also prevents the printing from yielding any middle unfinished steps.

·         3. A M-function file name can have up to 31 charaters.

You have by now created a new m-file entitled myfun.m in your disk in A drive as a subroutine.

5. To execute a M-function in Matlab Type ``addpath c:\ matlab a:" in the Matlab command page. You are ready to execute the M-function file ``myfun.m" directly which is stored in your floppy disk in a: drive. This way, it created a temporary path from the Matlab to drive a. You are ready for execution: type in >> : y=myfun(5.62) It will give the answer: >> : ans =-245.1408. Similarly, you can find y=[-245.1408, -2.8738, 5.9528]; if you type first x=[5.62,1.20,-2.5]; (are you aware of the

;

?) and then enter y=myfun(x). This can be done as follows.

 

x=[5.62,  1.20,  -2.15]; ( Enter)

y=myfun(x)     ( Enter)    ( no ;, please)

 

6. Plotting graphs. Turn the computer to Matlab command page then do the following: >> : x=-1:0.1:1; (Enter) >> : y=myfun(x); (Enter) >> : plot(x,y) (Enter) The curve should appear on screen. The first line x=-1:0.1:1 produces 21 mesh points on the interval [-1,1] with the step=0.1. Now try plotting the graph y=x2 from x=-1 to x=1. Press >> y=x.Ù2; (Enter) >> plot(x,y,`b') (Enter) `b" stands for blue color. This time the plot is in blue. If you want to put the previous graph together with the new one, press any key to return to the text and enter >> hold on (Enter) >> y=myfun(x); (Enter) >> plot(x,y); (Enter) The command `hold on" tells Matlab not to erase old plots. Matlab will keep on drawing new plots on tops of old plots until you issu e the command ` hold off'. You may specify the graph with a pattern in terms of a line type or a point type selecting from the following array of symbols:

Colors

 

 

Line types

 

 

Point types

 

r

red

 

-

solid

 

·

point

w

white

 

- -

dashed

 

+

+

g

green

 

:

dotted

 

*

star

b

blue

 

-.

dash-dot

 

°

circle

i

invisible

 

 

 

 

x

x-mark.


Example: >> plot(x,y,'b *') will produce a blue * curve. If you want to have your graph drawn with a grid background, you may type in the following command: Example: >> grid (enter). Then the curve will be ploted out on the top of grid meshes.

B. Little Dictionary In Matlab

 

Display Formats

 

format long='padding:.75pt .75pt .75pt .75pt'>

35.83333333333334

16 digits

format short

35.8333

6 digits

format long e

3.583333333333334e+01=35.83333333333334

16 digits plus exponent

format short e

abs(x)

|x|

acos(x)

cos-1 x

acosh(x)

cosh-1 x

asin(x)

sin-1 x

atan(x)

tan-1 x

conj(x)

complex conjugate [`x]

cos(x)

cosx

sin(x)

sinx

cosh(x)

coshx

exp(x)

ex

gamma(x)

gamma(n)=(n-1)!

log(x)

lnx

log10(x)

log10 x

real(x)

complex real part

rem(x,y)

remainder of x/y

round(x)

round toward nearst integer. round(3.4)= 3, round(3.5)=4.

fix(x)

round downward toward zero at the nearst interger. fix(3.77)=3.

ceil(x)

round upward to nearest integer. ceil(6.02)=7. fix(6.02)=6

floor(x)

round toward at nearest integer. floor(-1.2)=-2, fix(-1.2)=-1.

sign(x)

sign(positive)=1, sign(neg)=-1, sign(0)=0

sqrt(x)

sqrt(x)=Öx

 

Matlab numbers

 

exp(1)

exp(1)=e (=e1)

pi

p

eps

2.2×10-16. (the smallest Matlab number. Use this sometime to replace the zero)

i

Ö[(-1)]

 

Relation Operators

 

<

 

<

< =

 

£

>

 

>

> =

 

³

==

 

equal to, =

~  =

 

\not =

 

Logical Operator

 

&

 

and

|

 

or

~  

 

not

 

Operations

 

a.* b

 

a×b

a/ b, a./ b

 

a¸b

a Ùb, a .Ùb

 

ab

a±b

 

a±b

 

Array Construction

1. Let vectors x=[1,0,-2.5,5.7,-9.1,pi, 3.3,7.6,-2,4.7];    y=[1,2,3,4,5,6,7,8,9,10];

size(x) = 1 : 10

sum(x)=11.8416

x(3) = -2.5000, x(7) = 3.3000¼

x(3:7)=[-2.5, 5.7, -9.1, 3.1416, 3.3]. (Components run from 3rd to 7th)

x(5:-1:1)=[-9.1, 5.7, -2.5, 0, 1]; (Components run backward from 5th to 1st.)

x(2:2:7)=[0, 5.7, 3.1416]. (Take every other component from 2nd to 7th)

x([8, 2, 9, 1])=[7.6, 0, -2, 1] (The 8th,2nd,9th,and the 1st components.)

2. Let z=sin(x);w=log(y); The vertical transposes

[x¢,y¢]=

é
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ë

x

y

 

 

1.0000

1.0000

0

2.0000

-2.5000

3.0000

5.7000

4.0000

-9.1000

5.0000

3.1416

6.0000

3.3000

7.0000

7.6000

8.0000

-2.0000

9.0000

4.7000

10.0000

ù
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
û

,    [z¢,w¢]=

é
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ê
ë

sinx

log(y)

 

 

0.8415

0

0.6931

-0.5985

1.3863

-0.3191

1.6094

0.0000

1.7918

-0.1577

1.9459

0.9679

2.0794

-0.9093

2.1972

-0.9999

2.3026

ù
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
ú
û

.

3. Array operations Let a=[a1,a2,¼,an], b=[b1,b2,¼,bn], c=a scalar.

scalar addition

a+c=[a1+c,a2+c,¼,an+c]

scalar multiplication

a * c=[a1 *c,a2 *c,¼,an *c]

array addition

a+b=[a1+b1,a2+b2,¼,an+bn]

array multiplication

a. *b=[a1. *b1,a2. *b2,¼,an. *bn]

array division

a./b=[a1./b1.a2./b2,¼,an./bn]

array power

a.Ùc=[a1c,a2c,¼,anc]

 

c.Ùa=[ca1,ca2,¼,can]

 

a.Ùb=[a1b1,a2b2,¼,anbn]

4. Special commands

x=0:0.1:1

x = [ 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1].

 

 

x=(0:0.1:1). * pi

x = [0, 0.3142, 0.6283, 0.9425, 1.2566, 1.5708,

 

1.8850, 2.1991, 2.5133, 2.8274, 3.1416]

 

 

x=linspace(0,pi,11)

x = [0, 0.3142, 0.6283, 0.9425, 1.2566, 1.5708,

 

1.8850, 2.1991, 2.5133, 2.8274, 3.1416]

 

 

x=linesace(a,b,n)

implemnets a sequence of n numbers starts with a end up with b

 

Matrices



1. Create a matrix. >> A=[1,2,3; 4,5,6; 7,8,9] A=

 

1

2

3

4

5

6

7

8

9

 

2. Change or update an existing matrix >> A(3,2)=0 (Change the element in the 3rd row and 2nd column to 0) A=

 

1

2

3

4

5

6

7

0

9

 

3. Create a matrix B by taking the rows or columns of A in reverse order >> B=A(3:-1:1, :) B=

 

7

8

9

4

5

6

1

2

3

 

>> C=A(:, 3:-1:1) C=

 

td nowrap style='padding:.75pt .75pt .75pt .75pt'>

3

2

1

6

5

4