You are on page 1of 12

METU, 2015 Prepared by

Derya Kaya

MATLAB TUTORIAL

1) Matlab Panels

2) Operations and Variables

3) Matrices

4) Scripts

5) Plotting

6) Simulink
METU, 2015 Prepared by
Derya Kaya

1) Matlab panels:

Command Window Command History

Workspace
Current
Folder

Current Folder: You can access your files.

Command Window: You can enter the commands after the >> sign.

Workspace: Data that you create or import are explored here.

Command History: Return the commands that you enter at the command line. To clear the Command History
click right on it then click “Clear Command History”.

To clear command window:

>> clc

To clear workspace

>> clear all

To close graphs

>> close all

Matlab has an online documentation facility. You can access it by help or doc command (e.g. >> help sin)

2) Operations and Variables:


>> (-1+5)*4/3 >> log(10) >> exp(0) >> sqrt(81) >> cos(pi) >> sin(30*pi/180)

ans = ans = ans = ans = ans = ans =

5.3333 2.3026 1 9 -1 0.5000


METU, 2015 Prepared by
Derya Kaya

>> 5^2 >> i^2 >> >> sqrt(2) >> >> sin(30*pi/180)
abs(i+1) angle(i+1)
ans = ans = ans = ans =
*180/pi
ans =
25 -1 1.4142 0.5000
ans =
1.4142
45

You can also use j to indicate the complex numbers. pi has the value 3.14159..

Inside the cos, sin, tan, atan, etc. commands, you should write the angle in terms of radian. So, convert the
degree to radian!

Y = ceil(X) rounds each element of X to the nearest integer greater than or equal to that element.

Y = floor(X) rounds each element of X to the nearest integer less than or equal to that element.

Y = round(X) rounds each element of X to the nearest integer. In the case of a tie, where an element
has a fractional part of exactly 0.5, the round function rounds away from zero to the integer with
larger magnitude.

Reference: http://www.mathworks.com/help/matlab/ref/round.html

In Matlab, you should define the variables. Otherwise it giver error!

>> a+1 a=

Undefined >> a+1


function or ans =
variable 'a'.
4

>> a=3;b=2;a+b
When you put the ; at the end of the
ans =
command it still define the variable
and write to the workspace. In order 5
to get a clear command window use ;
at the end of the commands.

If the variables that you want to use do not have a specific value, then you should define them as a symbolic
function by syms command.
METU, 2015 Prepared by
Derya Kaya

>> syms a b c

>> a^2+b^2+(c^2)/2

ans =

a^2 + b^2 + c^2/2

>> pretty(ans) (makes the result looks nicer)

𝑐2
𝑎2 + 𝑏 2 +
2

Cleaning up symbolic statements:

>> simplify (ans) simplifies ans syms a


X=(cos(a))^2+(sin(a))^
2
simplify(X)

ans =
1
3) Matrices:

Comma or space seperated values construct rows. You can create columns by using ; sign.

Constructing a matrix Size Eigenvalues Inverse of a matrix

>> A=[0 1 3;2 4 -6;3 1 7] >> size(A) >> eig(A) >> inv(A)

A= ans = ans = ans =

0 1 3 3 3 -1.4900 + 0.0000i -0.5484 0.0645 0.2903

2 4 -6 6.2450 + 1.6156i 0.5161 0.1452 -0.0968

3 1 7 6.2450 - 1.6156i 0.1613 -0.0484 0.0323

Transpose [V,D] = eig(A) : Eigenvalue decomposition >> eye(3) >> zeros(2) >> ones(3)

>> A' ans = ans = ans =

ans = [U,S,V]=svd(A) :Singularvalue 1 0 0 0 0 1 1 1


decomposition
0 2 3 0 1 0 0 0 1 1 1

1 4 1 0 0 1 1 1 1
To do elementwise[Q,R] = qr(A)
operations use: QR decomposition
dot (e.g. A./B or A.*B).
3 -6 7

>> det(A) gives


the determinant eye(n) zeros(n) ones(n)
>> transpose(A)
of A matrix creates an creates a creates a
gives the same
result identity zero matrix
>> rank(A) gives matrix matrix consisting
the rank of A with a size with a size of 1 with a
matrix nxn nxn size nxn
METU, 2015 Prepared by
Derya Kaya

>> a=[1 2];b=[3 4];c=[a;b] >> B=[ones(2);1 -4] C = rand(n) returns an n- >> A=[1 2;3 4];
by-n matrix of random
c= B= >> B=[5 6;7 8];
numbers.
1 2 1 1 >> A*B
>> C=rand(2)
3 4 1 1 ans =
C=
>> d=[a b] 1 -4 19 22

d= >> B(6) 0.8235 0.3171 43 50

1 2 3 4 ans = >> A.*B


0.6948 0.9502
-4 ans =

5 12

21 32

Elementwise operation

>> A=[1 2 3;4 5 6] A=

A= 1 2 3

1 2 3 4 5 6
First row of A
4 5 6 >> B=A(1,:)
matrix
>> A(1,3) B=

ans = 1 2 3

3 >> C=A(:,3) Third column


of A matrix
>> A(2,2) C=

ans = 3

5 6

4) Scripts

To create a new editor document, write >> edit at the command line or click to the “New Script” as shown in the
following figure.

Why do we use editor (m file)?

 To make changes on it
 To save and use it later
METU, 2015 Prepared by
Derya Kaya

New Script

* means that it is not saved. Save the file and


then press the Run button

Anything following percent sign (%) is a comment! Comments helps you to understand the code when you use it
later. Hence, you can save your time.

You cannot run the commands that you write to the editor unless you don’t save it!
METU, 2015 Prepared by
Derya Kaya

5) Plotting

y = linspace(n,m) returns a row vector of 100 evenly spaced points between n and m.

y = linspace(n,m,k) generates n points. The spacing between the points is (m-n)/(k-1).

>> t=(1:5) t(n:m) creates a vector from n to m


with 1 increment
t=

1 2 3 4 5

>> t=(1:0.5:5) t(n:k:m) creates a vector from n to m


t= with k increment

1.0000 1.5000 2.0000 2.5000


3.0000 3.5000 4.0000 4.5000
5.0000

2D Plotting:

1
>> x=linspace(1,5); 0.8

0.6

>> y=sin(x); 0.4

0.2

>> plot(x,y) 0

-0.2

x and y vectors must be same size. -0.4

-0.6
Otherwise you get an error! -0.8

-1
1 1.5 2 2.5 3 3.5 4 4.5 5

You need to write axes names and put a title and/or legend.

Angular velocity versus thrust


>> x=linspace(1,5); 1
A
>> y=sin(x); 0.8

>> plot(x,y) 0.6

0.4
>> xlabel('RPM')
0.2
>> ylabel('Thrust')
Thrust

0
>> title('Angular velocity versus thrust')
-0.2
>> legend('A') -0.4

-0.6

-0.8

-1
1 1.5 2 2.5 3 3.5 4 4.5 5
RPM
METU, 2015 Prepared by
Derya Kaya

Omega versus thrust


x=linspace(1,5); 1

y=sin(x); 0.8

plot(x,y,'g*-') 0.6

hold on 0.4

z=cos(x) 0.2

Thrust
plot(x,z,'r*') 0

-0.2
xlabel('RPM')
-0.4
ylabel('Thrust')
-0.6
title('Omega versus thrust')
-0.8 y=sin(x)
legend('y=sin(x)','z=cos(x)','Location','southwes z=cos(x)
-1
t') 1 1.5 2 2.5 3
RPM
3.5 4 4.5 5

You can also insert x label, y label, title or legend by clicking on the insert part on the graph.

Insert x label, y label, title, legend, etc.

Angular velocity versus thrust


1

plot(x,y,'r.-') 0.8
A

0.6

Color Marker Line 0.4

Style 0.2
Thrust

-0.2

-0.4

-0.6

-0.8

-1
1 1.5 2 2.5 3 3.5 4 4.5 5
RPM
METU, 2015 Prepared by
Derya Kaya

Specifier Line Style

- Solid line (default)

-- Dashed line

: Dotted line

-. Dash-dot line

Specifier Marker

o Circle

+ Plus sign

* Asterisk

. Point

x Cross

s Square

d Diamond

^ Upward-pointing triangle

v Downward-pointing triangle

> Right-pointing triangle

< Left-pointing triangle

p Pentagram

h Hexagram

Specifier Color

y yellow
m magenta
c cyan
r red
g green
b blue
w white
k black
METU, 2015 Prepared by
Derya Kaya

Table Reference: http://www.mathworks.com/help/matlab/ref/plot.html

Figure Reference: MIT Introduction to Matlab, Lecture 2

Drawing subplots

subplot(x,y,z) e.g. subplot(2,1,2)means the figure consists


of 2 rows, one column, and activates
Makes x Activates zth second one.
Makes y
row one
column

x = linspace(0,10);
y1 = sin(x);
y2 = sin(5*x);
figure
subplot(2,1,1);
plot(x,y1)
subplot(2,1,2);
plot(x,y2)

x = linspace(0,10);
y1 = sin(x);
y2 = sin(5*x);
y3=sin(7*x)
figure
subplot(3,1,1);
plot(x,y1)

subplot(3,1,2);
plot(x,y2)

subplot(3,1,3)
plot(x,y3)
subplot(2,1,2);
METU, 2015 Prepared by
Derya Kaya

>> close all command closes all figures.

>>close[n m] command closes nth and mth figures.

3D Plotting:

>> time=(0:0.4:10);

>> z=time;

>> x=sin(time);

>> y=cos(time);

>> plot3(x,y,z,'b','Linewidth',2)

Surface and mesh plots

x=-pi:0.1:pi;
y=-pi:0.1:pi;
[a,b]=meshgrid(x,y); % to make
matrices
Z=sin(a).*cos(b);
surf(x,y,Z)

x=-pi:0.1:pi;
y=-pi:0.1:pi;
[a,b]=meshgrid(x,y); % to make
matrices
c=sin(a).*cos(b);
contour(a,b,c)
METU, 2015 Prepared by
Derya Kaya

6) Simulink

Open new document

Simulink library browser includes various blocks that you need.

Example: Mass-spring-damper

References:

Introduction to Programing in Matlab, MIT opencourseware


http://www.mathworks.com/help/matlab/ref/plot.html

You might also like