Professional Documents
Culture Documents
Derya Kaya
MATLAB TUTORIAL
1) Matlab Panels
3) Matrices
4) Scripts
5) Plotting
6) Simulink
METU, 2015 Prepared by
Derya Kaya
1) Matlab panels:
Workspace
Current
Folder
Command Window: You can enter the commands after the >> sign.
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”.
>> clc
To clear workspace
To close graphs
Matlab has an online documentation facility. You can access it by help or doc command (e.g. >> help sin)
>> 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
>> a+1 a=
>> 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 =
𝑐2
𝑎2 + 𝑏 2 +
2
ans =
1
3) Matrices:
Comma or space seperated values construct rows. You can create columns by using ; sign.
>> A=[0 1 3;2 4 -6;3 1 7] >> size(A) >> eig(A) >> inv(A)
Transpose [V,D] = eig(A) : Eigenvalue decomposition >> eye(3) >> zeros(2) >> ones(3)
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
>> 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
5 12
21 32
Elementwise operation
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
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.
To make changes on it
To save and use it later
METU, 2015 Prepared by
Derya Kaya
New Script
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.
1 2 3 4 5
2D Plotting:
1
>> x=linspace(1,5); 0.8
0.6
0.2
>> plot(x,y) 0
-0.2
-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.
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
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.
plot(x,y,'r.-') 0.8
A
0.6
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
-- 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
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
Drawing subplots
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
3D Plotting:
>> time=(0:0.4:10);
>> z=time;
>> x=sin(time);
>> y=cos(time);
>> plot3(x,y,z,'b','Linewidth',2)
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
Example: Mass-spring-damper
References: