0% found this document useful (0 votes)
16 views

Matlab Tutorial Ae383

This document provides an introduction and overview of MATLAB including its panels, operations and variables, matrices, scripts, plotting, curve fitting and Simulink. It explains the MATLAB command window, workspace, current folder and command history. It demonstrates basic operations, defining and using variables, creating and manipulating matrices, and generating random matrices. It also introduces common matrix operations like transpose, inverse, determinant, rank, eigenvalues and singular value decomposition.

Uploaded by

Muhammet Demir
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Matlab Tutorial Ae383

This document provides an introduction and overview of MATLAB including its panels, operations and variables, matrices, scripts, plotting, curve fitting and Simulink. It explains the MATLAB command window, workspace, current folder and command history. It demonstrates basic operations, defining and using variables, creating and manipulating matrices, and generating random matrices. It also introduces common matrix operations like transpose, inverse, determinant, rank, eigenvalues and singular value decomposition.

Uploaded by

Muhammet Demir
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

INTRODUCTION TO MATLAB

Department of Aerospace Engineering, METU

System Dynamics - AE 383

October 17 1
OUTLINE
• Matlab Panels
• Operations and Variables
• Matrices
• Scripts
• Plotting
• Curve Fitting
• Simulink
October 17 2
MATLAB PANELS Current Folder: You can access
your files.
Command Window: You can enter
Command Window Command History the commands after the >> sign.
Workspace: Data that you create
or import are explored here.
Command History: Return the
Workspace
commands that you enter at the
Current
Folder command line.
To clear command window: >> clc
To clear workspace >> clear all
To clear the command history click
right on it then click “Clear
Command History”.
To close graphs >> close all

October 17 3
MATLAB PANELS
>> who  list all variables that exist in the workspace.

>>whos  detailed list of all variables in the workspace with name,


dimension, data type and size (memory).

October 17 4
This is natural logarithm, i.e., ln( ). In Matlab log( ) is used for that. If you need
10 base logarithm, you should use log10( ). log10(10) returns 1 as expected.

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

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

October 17 5
OPERATIONS AND VARIABLES
• 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.

October 17 6
OPERATIONS AND VARIABLES
• You should define the variables. Otherwise YOU GET ERROR.
>> a+1 a=
3

Undefined function or >> a+1


variable 'a'. ans =
4

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

October 17 7
OPERATIONS AND VARIABLES
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.

Cleaning up symbolic statements, >> simplify (X)

>> syms a

>> X=(cos(a))^2+(sin(a))^2
>> pretty(ans) makes the result looks nicer
>> simplify(X)

ans =
1

October 17 8
OPERATIONS AND VARIABLES
Consider a polynomial: 𝑠 2 + 5𝑠 + 6. You can evaluate the polynomial for a specified value by using Matlab:
>>P=[1 5 6];
>>polyval(P,1)
ans =
12
It evaluates 𝑠 2 + 5𝑠 + 6 for 𝑠 = 1
>> r=roots(P)
r=
-3.0000
-2.0000
It evaluates the roots of P, which are -3 and -2

October 17 9
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

October 17 10
MATRICES
Transpose [V,D] = eig(A) : Eigenvalue decomposition >> eye(3) >> zeros(2) >> ones(3)
>> A' ans = ans = ans =
ans = [U,S,V]=svd(A) :Singularvalue decomposition 1 0 0 0 0 1 1 1
0 2 3 0 1 0 0 0 1 1 1
1 4 1 [Q,R] = qr(A) : QR decomposition 0 0 1 1 1 1
3 -6 7

>> det(A) gives the


determinant of A matrix eye(n) creates zeros(n) ones(n)
an identity creates a zero creates a
>> transpose(A) gives the matrix with a matrix with a matrix
same result >> rank(A) gives the rank of A
size nxn size nxn consisting of 1
matrix
with a size nxn

October 17 11
MATRICES

M = magic(n) returns an n-by-n matrix constructed from the integers 1 through n^2 with equal
row and column sums. The order n must be a scalar greater than or equal to 3.

October 17 12
MATRICES
>> 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];
c= B= by-n matrix of random >> 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) 43 50
0.8235 0.3171
1 2 3 4 ans = >> A.*B Elementwise
0.6948 0.9502 operation (.*),
-4 ans = mostly used in
data analysis. It
5 12 operates on
array element by
element.
21 32
October 17 13
MATRICES
>> A=[1 2 3;4 5 6] A=
A= 1 2 3
1 2 3 4 5 6
4 5 6 >> B=A(1,:) First row of A 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

October 17 14
EXERCISE I
1. Create a 4x3 matrix of random numbers.
2. Create a diagonal matrix of size 6x6 with 2 on the diagonal.
3. Solve Ax = b for A = magic(4) and b = (1 2 3 4), and compute eigenvalues,
eigenvectors, inverse, rank, and transpose of A.
4. Consider a function 𝐺 𝑠 = 𝑠 3 +𝑠 + 6. Find G(-3) and roots of 𝐺 𝑠 .

October 17 15
SCRIPTS
A script file is a file that contains a series of MATLAB commands
New Script

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

October 17 16
SCRIPTS
* means that it is not saved. Save the file and
then press the Run button

October 17 17
SCRIPTS
• 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.
• We will use editor in plotting part.

October 17 18
PLOTTING
2D 3D
• Line graphs • Line graphs
• Bar graphs • Surface graphs
• Area graphs • Mesh graphs
• Scatter graphs • Bar graphs
• Radial graphs • Volumetric graphs
etc. etc.

October 17 19
Generating data in plotting
PLOTTING
y = linspace(n,m) returns a row vector of 100 evenly spaced points between n and m.
y = linspace(n,m,k) generates k 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


t= 1 increment

1 2 3 4 5
>> t=(1:0.5:5) t(n:k:m) creates a vector from n to m
with k increment
t=
1.0000 1.5000 2.0000 2.5000
3.0000 3.5000 4.0000 4.5000
5.0000

October 17 20
PLOTTING
1
2D Line graph
0.8
>> x=linspace(1,5);
0.6

>> y=sin(x); 0.4

>> plot(x,y) 0.2

0
x and y vectors must be same size. Otherwise you get error.
-0.2

-0.4

-0.6

-0.8

-1
1 1.5 2 2.5 3 3.5 4 4.5 5

October 17 21
PLOTTING
You need to write axes names and put a title and/or legend.
Angular velocity versus thrust
1
A
0.8

0.6
>> x=linspace(1,5);
0.4
>> y=sin(x);
0.2
>> plot(x,y)

Thrust
0
>> xlabel('RPM')
-0.2
>> ylabel('Thrust')
-0.4
>> title('Angular velocity versus thrust')
-0.6
>> legend('A')
-0.8

-1
1 1.5 2 2.5 3 3.5 4 4.5 5
RPM

October 17 22
PLOTTING
>>hold on retain current graph in figure.
x=linspace(1,5); 1
Omega versus thrust

y=sin(x); 0.8
plot(x,y,'g*-') 0.6
hold on 0.4
z=cos(x) 0.2
plot(x,z,'r*')

Thrust
0
xlabel('RPM')
-0.2
ylabel('Thrust')
-0.4
title('Omega versus thrust')
-0.6
legend('y=sin(x)','z=cos(x)','Location','southwest')
-0.8 y=sin(x)
z=cos(x)
-1
1 1.5 2 2.5 3 3.5 4 4.5 5
RPM
October 17 23
PLOTTING
• 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.

October 17 24
PLOTTING
Angular velocity versus thrust
1
A
0.8

plot(x,y,'r.-') 0.6

0.4

0.2

Marker Line Style

Thrust
Color 0

-0.2

-0.4

-0.6

-0.8

-1
1 1.5 2 2.5 3 3.5 4 4.5 5
RPM

October 17 25
PLOTTING
Specifier Line Style
- Solid line (default) Specifier Color
-- Dashed line
: Dotted line
-. Dash-dot line y yellow
Specifier Marker
m magenta
o Circle
+ Plus sign c cyan
* Asterisk
. Point r red
x Cross
s Square g green
d Diamond
^ Upward-pointing triangle b blue
v Downward-pointing triangle
> Right-pointing triangle w white
< Left-pointing triangle
p Pentagram k black
h Hexagram
Reference: http://www.mathworks.com/help/matlab/ref/plot.html October 17 26
PLOTTING
subplot(x,y,z) e.g. subplot(2,1,2)means the figure
Activates zth
consists of 2 rows, one column, and x = linspace(0,10);
Makes x Makes y
row column
one activates second one. y1 = sin(x); 1

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

0.5
figure -1
0 1 2 3 4 5 6 7 8 9 10

sin(x);y2 = 0
subplot(3,1,1); 1
-0.5

sin(5*x);figuresubplot(2,1, -1
0 1 2 3 4 5 6 7 8 9 10
plot(x,y1) 0

1);plot(x,y1)subplot(2,1,2) 1

0.5
-1

;plot(x,y2) 0
subplot(3,1,2); 0 1 2 3 4 5 6 7 8 9 10
1
-0.5
plot(x,y2)
-1
0 1 2 3 4 5 6 7 8 9 10
0

subplot(3,1,3) -1
0 1 2 3 4 5 6 7 8 9 10
>> close all command closes all figures. plot(x,y3)
subplot(2,1,2);
plot(x,y2)
>>close[n m] command closes nth and mth figures.
October 17 27
PLOTTING

3D line graphs
>> time=(0:0.4:10);
3D mesh graphs
>> z=time; 10

6
>> x=-pi:0.1:pi;
>> x=sin(time); 4 >> y=-pi:0.1:pi; 1
2

>> y=cos(time); 0
>> [a,b]=meshgrid(x,y); % to 0.5

1
0.5 1 make matrices 0

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

>> Z=sin(a).*cos(b); -0.5


-1 -1

>> surf(x,y,Z) -1
4
2 4
0 2
0
-2 -2
-4 -4

October 17 28
PLOTTING
3D Contour graph

x=-pi:0.1:pi; 3

y=-pi:0.1:pi; 2

[a,b]=meshgrid(x,y); % to 1

make matrices
-1

c=sin(a).*cos(b); -2

contour(a,b,c) -3
-3 -2 -1 0 1 2 3

October 17 29
MATLAB has an online documentation facility. You can access it by help or
doc commands (e.g. >> help sin)

October 17 30
CURVE FITTING
When we work with data such as experimental, statistical etc., the data does not
has a symmetrical and smooth pattern when it is plotted. To obtain a better and
suitable plots and even to eliminate the measurement errors existing in the data
we try to find the best fit curve of the data. Matlab has very useful and smart
tools for this purpose.

October 17 31
CURVE FITTING
For example, the plot has 10 specific
values taken from a sine function which
are shown with the blue circles. If we
just combine the points piecewise, we
cannot obtain the real or an approximate
curve.

October 17 32
CURVE FITTING
Then, how can we obtain a more
approximate curve?

October 17 33
October 17 34
October 17 35
CURVE FITTING
• Fist we choose the data sets that we want to fit. Then we need to choose a
proper fitting type such as interpolant, polynomial and etc. Each method has
advantages/disadvantages according to the studied data type. In this course
we usually need to use polynomial curve fitting. It is significant to note that a
suitable/proper degree of polynomial must be chosen for the fitting.

October 17 36
CURVE FITTING
Alternatively we can use command to make curve fitting.

x = linspace(0,4*pi,10); Creates 10 points from 0 to 4*pi


y=sin(x);
Creates a fitting curve for the points on
p = polyfit(x,y,7); sine function corresponding to 10 points
by using 7th order polynomial
x1 = linspace(0,4*pi,100);
y1 = polyval(p,x1,100); Creates a new plot by calculating the
value of the created polynomial

October 17 37
CURVE FITTING

figure;
plot(x,y,'o')
hold on
plot(x,y,'r')
plot(x1,y1,'b')

October 17 38
SIMULINK
Simulink is
• a block diagram environment that is used in simulation, automatic code generation, and continuous
test and verification of embedded systems.
• provides a graphical editor, customizable block libraries, and solvers for modeling and simulating
dynamic systems.
• is an interactive environment for modeling, simulating and analyzing dynamic systems.
• uses graphical user interface of block diagram representation instead of differential and difference
equations.
• consists of blocks (representing elementary dynamic systems) interconnected by lines (time-domain
signals).

October 17 39
SIMULINK
Click on the Simulink
Library
or write >> simulink
on the command
window.

D. Kaya October 17 40
SIMULINK

Open new
Simulink
document

October 17 41
SIMULINK

Move the
blocks, that
you need, to
the new
document,
then run the
simulation by
clicking on the
run button.

October 17 42
SIMULINK
• To open Simulink click on the Simulink icon on the MATLAB toolbar.
• To open a new model click on the corresponding icon on the Simulink
toolbar.
• To introduce blocks in your model, choose the block from the library, click
on it and drag it in your model.
• Interconnect the output of a block to the input of the required block by a
line using mouse.
• Double-click each block to set its parameters.

October 17 43
SIMULINK
Simulink provides a library browser that allows
you to select blocks from libraries of standard
blocks:
• Sources - blocks that generate signals
• Sinks - display or output blocks.
• Discrete - discrete-time components blocks.
• Continuous - linear continuous-time blocks.
• Math - general mathematical functions blocks.
October 17 44
SIMULINK
The Simulink block diagram model depicts a time-dependent
mathematical relationship between Inputs, States, and Outputs in
the form of a set of Ordinary Differential Equations “ODE”.
In this phase Simulink successively solves the ODE to compute
the states and the outputs of the system at intervals from the
simulation start time to the stop time.
To set the parameters of the simulation select simulation from
Simulink’s toolbar, then select simulation parameters or model
configuration parameters.
October 17 45
Simulink Example

• Build a Simulink model that solves the


differential equation x(0)  1

x  3 sin 2t 
x 1 x
• With initial condition of 3sin(2t)
(input) s
x(t)
(output)

x(0)  1. integrator

October 17 46
x  3 sin 2t 

x(0)  1.

October 17 47
EXERCISE II
• Simulate a mass-spring-damper system using MATLAB-Simulink.

October 17 48
SIMULINK
• For block libraries see the following references:
https://www.mathworks.com/help/simulink/block-libraries.html
https://www.mathworks.com/help/simulink/blocklist.html

October 17 49

You might also like