You are on page 1of 26

Command window

Command
history
Workspace
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

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



To make a row vector called a as in
MATLAB, we write
a = [ 1 5 3]
OR
a = [ 1, 5 ,3]
To make a column vector called a as in
MATLAB, we write
a=[1 ; -10 ; 4]
Note that ; means new line in the matrix
What are the dimensions of the above
matrices ?
( ) 3 5 1
|
|
|
.
|

\
|

4
10
1
To make a Matrix called a as 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 .-

|
|
|
.
|

\
|

30
10
9
6
5
3

* =

.* =

To test the above in MATLAB write
a= [3 2; 3 5]
b= [1 3;4 -2]
a*b
a.*b


|
|
.
|

\
|
5
2
3
3
|
|
.
|

\
|
2
3
4
1
|
|
.
|

\
|
1
5
23
11
|
|
.
|

\
|
5
2
3
3
|
|
.
|

\
|
2
3
4
1
|
|
.
|

\
|
10
6
12
3
Index in MATLAB start with 1 not 0 as in C++
So to index the element 5 in the
following matrix (named a) we write
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
a(:,2)
|
|
|
.
|

\
|

30
10
9
6
5
3
For row or column vector you can use only
one index or two indices
a=[2 13 4 6]
b=[3 ; 45 ; 34 ; 44]
The second item in vector a is
m = a(2) OR m = a(1,2)
The second item in vector b is
m = b(2) OR m = b(2,1)

|
|
|
|
|
.
|

\
|
=
44
34
45
3
b
( ) 6 4 13 2 = a
Rotate each column to a row
OR
Rotate each row to a column
Its symbol is



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


|
|
.
|

\
|


30 10 9
6 5 3
|
|
|
.
|

\
|

30
10
9
6
5
3
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
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
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 condition
.
.
end
if condition
.
else
.
end


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

for variable = start : step :end
.
.
end
for i=1:0.1:2
i
end
for i=2:-0.1:1
i
end
while condition
.
.
end
t=0;
while t<5
t=t+1
end
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

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)
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 too functions instead
of one)

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

You can choose the color of the graph
b blue g green 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-+)
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=sin(3*x);
plot(x,y2) ;
hold off % disable holding
OR
plot(x,y1,r-, x,y2,b--)

You might also like