You are on page 1of 26

Matlab

Default layout (page 1)


Workspace

Command window

Command
history

Layout & help (page 2)


To restore the default layout
desktop >> desktop layout >> default
To clear the workspace
clear
To find information (help) about MATLAB
commands
1- use help command for example
help clear
2- use the help icon from the toolbar

Notes page 2
The answer of any line is printed bellow it
except lines ended by ;
a=3-2
b=5;
c=a+b
MATLAB is case sensitive so x is not X
a=3
b=A+3
wrong
b=a+3

Notes and operators (page 2&3)


You can write more than a command in the
same line. Each is separated by , (answer
printed) or ; (answer not printed)
a=3, b=7 ; c=a+b ; d=c/5
Operator with numbers
1- ^ (power) , *, / , + , (arithmetic)
2- < , <= , > , >= , == , ~= (not equal) , |
(or), & (and)
Look to examples in the end of page 3

Matrix (page 4)
To make a row vector called a as1 5 3 in
MATLAB, we write
a = [ 1 5 3]
OR
a = [ 1, 5 ,3]
1

To make a column vector called a as 10


4

in MATLAB, we write
a=[1 ; -10 ; 4]
Note that ; means new line in the matrix
What are the dimensions of the above
matrices ?

Matrix (page 4)
9
3
as 5 10
6 30

To make a Matrix called a


in
MATLAB, we write
a = [3 9 ; 5 -10; -6 30]
OR
a = [3 , 9 ; 5 , -10; -6 , 30]
Matrix operators ^ ,* , / , + , - . All refers to
matrix rules
Element by Element operators .^ , .* , ./ , .
+ , . + is the same as .+ and is the same as .

Matrix (page 4)

3 2

3 5
3 2

3 5

1 3
* 4 2
1 3
.* 4 2

11 5
= 23 1
6
3
= 12 10

To test the above in MATLAB write


a= [3 2; 3 5]
b= [1 3;4 -2]
a*b
a.*b

Sub matrix (page 4)


Index in MATLAB start with 1 not 0 as in C+
+
9
3

So to index the element 5 in the


5 10
following matrix (named a) we write 6 30
a(2,1)
Try the following examples
a(1:2,2)
a(2:3,1:2)
OR
a(2:3, : )
What is the output of

Sub matrix (page 4)


For row or column vector you can use only
one index or two indices
a 2 13 4 6
a=[2 13 4 6]
3

b=[3 ; 45 ; 34 ; 44]
45
b
The second item in vector a is
34

44
m = a(2)
OR m = a(1,2)

The second item in vector b is
m = b(2)
OR m = b(2,1)

Transpose (page 4)
Rotate each column to a row
OR
Rotate each row to a column
Its symbol is

10
30

5 6
3

10
30

a=[ 3 9; 5 -10; -6 30]


b=a

Constants and
formats( page 2)
1- pi the number = 3.141592653589793
format short : 4 digits after decimal point
format long : 15 digits after decimal point
pi
format long
pi
format short
pi

Dont
forget
a
report
of
examples of page
4

Constants ( page 2)

i , j indicates the imaginary unit sqrt(-1)


a= 1+4i
b= 3+6j
c=a*b

Inf : infinity
S=1/0

NaN : Not a number


S =0/0

M file ( page 4&5)


Write a MATLAB program
1- in command window as before
2- in M file
A. open a new M file
B. Write the program
a=3;
b=5;
c=(a+b) /2
C. Save it with a name
D. Run it (F5)
. Which is better 1 or 2 ?

If (page 7)
if condition
.
.
end
if condition
.
else
.
end

if condition
.
elseif condition
.
else
.
end
See example in
page 7

for (page 7&8)


for variable = start : step :end
.
.
end
for i=1:0.1:2

i
end

for i=2:-0.1:1
i
end

while(page 8)
while condition
.
.
end
t=0;

while t<5
t=t+1
end

Functions( page 5&6)


1- open a M file and save it with the same
name as the function
function y = myfun1(x)
y=1+x-x.^2/4;
end
then save it with name myfun1
function [ out1,out2, ] =
FunctionName( in1,in2, )
.
.
end

Functions( page 6&7)

You can run the function in command


window or in another M file
b = myfun1(3)

2- using inline ( one input called x and one


output
myfun2=inline(1+x-x.^2/4)
3- using @ (one output and one or more
inputs
myfun2=@(x) (1+x-x.^2/4)

Calling function (6)


1- C++ like
b = myfun1(3)

2-using feval
b = feval(myfun1,3)
Look at page 5 to the build in MATLAB functions

Write

a function that takes


p and p_hat and return the
absolute error and the
relative error using the
three methods( you may
write two functions instead
of one)

Plotting (page 7)
x= [ 1 , 2 , 3 , 4, 5]
% x data
y= [ 4 , 6 , 8 , 10 , 12]
% y data
plot (x,y)
%plotting
title(my first graph)
%plot title
xlabel(x data)
% x axis name
ylabel(y data)
% y axis name
Note that % is followed by comments
The first line can be replaced with
x=1:1:5
% start : step : end

Plotting (page 7)
You can choose the color of the graph
b blue
ggreen r red
y yellow k black
You can choose the line shape of the graph
-
solid
. point
-. dashdot
-- dashed
You can choose the points shape
*
star
+ plus
plot(x,y,r-);
plot(x,y,g.)
plot(x,y,b-.); plot(x,y,y--)
plot(x,y,k*);plot(x,y,b+)
plot(x,y,r-*); plot(x,y,b-+)

Multi Plotting (page 7&8)

Plotting sin and cos in the same graph


x=0:0.01:1;
y1=sin(3*x);
plot(x,y1) ;
hold on % all plotting will be over the same graph
y2=cos(3*x);
plot(x,y2) ;
hold off
% disable holding

OR
plot(x,y1,r-, x,y2,b--)

Dont forget to solve the


assignment in the last
page and deliver it in
papers the next section

You might also like