This document provides an introduction to MATLAB and Simulink. It discusses built-in functions, vectors and matrices, modeling examples, M-files for scripts and functions. It also explores the MATLAB desktop, command window, and workspace. Examples are provided on arithmetic operations on matrices, plotting graphs, writing functions, and modeling an RLC circuit in Simulink.
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
1.1Introduction to matlab.pptx
1.
2. Contents
Built in functions
Getting Started
Vectors and Matrices
Introduction
Simulink
Modeling examples
MATLAB
SIMULINK
M–files : script and functions
4. Don’t have to declare type
Don’t even have to initialise
Just assign in command window
>>
>> a=12; % variable a is assigned 12
4
Matlab
prompt
assign
operator
suppress
command
output
comment
operator
Matlab
prompt
assign
operator
suppress
command
output
comment
operator
Try the same line without the
semicolon and comments
5. View variable contents by simply typing the
variable name at the command prompt
>> a
a =
12
>>
>> a*2
a =
24
>>
5
6. The workspace is Matlab’s memory
Can manipulate variables stored in the
workspace
>> b=10;
>> c=a+b
c =
22
>>
6
7. Display contents of workspace
>> whos
Name Size Bytes Class
a 1x1 8 double array
b 1x1 8 double array
c 1x1 8 double array
Grand total is 3 elements using 24 bytes
>>
Delete variable(s) from workspace
>> clear a b; % delete a and b from workspace
>> whos
>> clear all; % delete all variables from workspace
>> whos
7
8. Don’t need to initialise type, or dimensions
>>A = [3 2 1; 5 1 0; 2 1 7]
A =
3 2 1
5 1 0
2 1 7
>>
8
square brackets to define matrices
semicolon for next row in matrix
9. Access elements of a matrix
>>A(1,2)
ans=
2
Remember Matrix(row, column)
Naming convention Matrix variables start with a capital
letter while vectors or scalar variables start with a
simple letter
9
A =
3 2 1
5 1 0
2 1 7
indices of matrix element(s)
10. VERY important operator in Matlab
Means ‘to’
>> 1:10
ans =
1 2 3 4 5 6 7 8 9 10
>> 1:2:10
ans =
1 3 5 7 9
10
Try the following
>> x=0:pi/12:2*pi;
>> y=sin(x)
12. >> A ' % transpose
>> B*A % matrix multiplication
>> B.*A % element by element
multiplication
>> B/A % matrix division
>> B./A % element by element division
>> [B A] % Join matrices (horizontally)
>> [B; A] % Join matrices (vertically)
12
A =
3 2 1
5 1 0
2 1 7
B =
1 3 1
4 9 5
2 7 2
Create matrices A and B and try out the matrix operators in this slide
Enter matrix B
into the Matlab
workspace
13. Arithmetic operations – Matrices
Performing operations to every entry in a matrix
Add and subtract
>>> A=[1 2 3;4 5 6;7 8
9]
A =
1 2 3
4 5 6
7 8 9
>>>
>>> A+3
ans =
4 5 6
7 8 9
10 11 12
>>> A-2
ans =
-1 0 1
2 3 4
5 6 7
14. Arithmetic operations – Matrices
Performing operations to every entry in a matrix
Multiply and divide
>>> A=[1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9
>>>
>>> A*2
ans =
2 4 6
8 10 12
14 16 18
>>> A/3
ans =
0.3333 0.6667 1.0000
1.3333 1.6667 2.0000
2.3333 2.6667 3.0000
15. Arithmetic operations – Matrices
Performing operations to every entry in a matrix
Power
>>> A=[1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9
>>>
A^2 = A * A
To square every element in A, use
the element–wise operator .^
>>> A.^2
ans =
1 4 9
16 25 36
49 64 81
>>> A^2
ans =
30 36 42
66 81 96
102 126 150
19. Scalar functions – used for scalars and operate
element-wise when applied to a matrix or vector
e.g. sin cos tan atan asin log
abs angle sqrt round floor
At any time you can use the command
help to get help
e.g. >>>help sin
20. Matlab editor
Use scripts to execute a series of Matlab commands
20
Matlab Desktop
Press to create new m-
file in the matlab editor
21. Scripts will manipulate and
store variables and matrices
in the Matlab Workspace
(memory).
They can be called from the
Matlab command line by
typing the (case sensitive!)
filename of the script file.
>> myscript
Scripts can be opened in the
editor by the following
>> open myscript
21
Highlight a few lines of your
script by left- clicking and
dragging the mouse over the
lines. Right-click the
highlighted lines and select
Evaluate Selection.
Will be slightly
different in
Linux
22. >> I=iterate(5)
I =
1 4 9 16 25
22
output
input
function name
for statement block
function keyword
help lines for function
Access the comments of
your Matlab functions
>> help iterate Make sure you save changes to the
m-file before you call the function!
23. >> [i j]=sort2(2,4)
i =
4
j =
2
>>
23
Functions can have many
outputs contained in a matrix
Remember to use the
Matlab help command for
syntax
>> help if
if statement
block
24. 24
Method is linear
>>
i =
4
i =
16
i =
256
While statement block Switch statement block
Without ; to
print output
25. >> figure % create new figure
>> t=0:pi/12:8*pi;
>> y=cos(t);
>> plot(t,y,‘b.-')
25
Investigate the function
>> y=A*cos(w*t+phi);
for different values of phi (eg: 0, pi/4, pi/3,
pi/2), w (eg: 1, 2, 3, 4) and A (eg: 1, 0.5, 2). Use
the hold on Matlab command to display your
plots in the same figure. Remember to type
hold off to go back to normal plotting mode.
Try using different plot styles (help plot)
A = amplitude
phi = phase
w = angular frequency = 2*pi*frequency
Plot style
26. Data visualisation – plotting graphs
Example on plot – 2 dimensional plot
Example on plot – 2 dimensional plot
>>> x=linspace(0,(2*pi),100);
>>> y1=sin(x);
>>> y2=cos(x);
>>> plot(x,y1,'r-')
>>> hold
Current plot held
>>> plot(x,y2,'g--')
>>>
Add title, labels and legend
title xlabel ylabel legend
Use ‘copy’ and ‘paste’ to add to your window–
based document, e.g. MSword
27. Data visualisation – plotting graphs
0 1 2 3 4 5 6 7
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
angular frequency (rad/s)
y1
and
y2
Example on plot
sin(x)
cos(x)
Example on plot – 2 dimensional plot
eg1_plt.m
28. Example – RLC circuit
Exercise 1:
Write an m–file to plot Z, Xc and XLversus
frequency for R =10, C = 100 uF, L = 0.01 H.
+
V
–
R = 10 C
L
29. Example – RLC circuit
Total impedance is given by:
L
C X
X
When
LC
1
o
31. For a given values of C and L, plot the following versus the frequency
a) the total impedance ,
b) Xc and XL
c) phase angle of the total impedance
for 100 < < 2000
Example – RLC circuit
+
V
–
R = 10 C
L
32. < ¦ save !
> rand load guide
~= zeros … get
== min ' ' set
>= max { }
<= repmat try
& axis catch
32
Remember to use the Matlab help command if you get stuck
string
cell
error handling
Operating
system
command
Graphical
user interface
Continue in next line
33. Every function must begin with a header:
function output=function_name(inputs)
Output variable
Must match the file
name
input variable
34. Used to model, analyze and simulate dynamic
systems using block diagrams.
Provides a graphical user interface for constructing
block diagram of a system – therefore is easy to use.
However modeling a system is not necessarily easy !
35. Model – simplified representation of a system – e.g. using
mathematical equation
We simulate a model to study the behavior of a system –
need to verify that our model is correct – expect results
Knowing how to use Simulink or MATLAB does not
mean that you know how to model a system
36. Problem: We need to simulate the resonant circuit
and display the current waveform as we change the
frequency dynamically.
+
v(t) = 5 sin t
–
i 10 100 uF
0.01 H
Varies from 0
to 2000 rad/s
Observe the current. What do we expect ?
The amplitude of the current waveform will become
maximum at resonant frequency, i.e. at = 1000 rad/s
37. How to model our resonant circuit ?
+
v(t) = 5 sin t
–
i 10 100 uF
0.01 H
idt
C
1
dt
di
L
iR
v
Writing KVL around the loop,
39. Thus the current can be obtained from the voltage:
LC
1
s
L
R
s
)
L
/
1
(
s
V
I
2
LC
1
s
L
R
s
)
L
/
1
(
s
2
V I
40. Start Simulink by typing simulink at Matlab prompt
Simulink library and untitled windows appear
It is here where we
construct our model.
It is where we obtain
the blocks to
construct our model
41. Constructing the model using Simulink:
‘Drag and drop’ block from the Simulink library
window to the untitled window
1
s+1
Transfer Fcn
simout
To Workspace
Sine Wave
42. Constructing the model using Simulink:
LC
1
s
L
R
s
)
L
/
1
(
s
2
6
2
10
1
s
1000
s
)
100
(
s
100s
s +1000s+1e6
2
Transfer Fcn
v
To Workspace1
i
To Workspace
Sine Wave
43. We need to vary the frequency and observe the current
100s
s +1000s+1e6
2
Transfer Fcn1
v
To Workspace3
w
To Workspace2
i
To Workspace
Ramp
s
1
Integrator
sin
Elementary
Math
Dot Product3
Dot Product2
1000
Constant
5
Amplitude
…From initial problem definition, the input is 5sin(ωt).
You should be able to decipher why the input works, but you
do not need to create your own input subsystems of this form.
45. The waveform can be displayed using scope – similar
to the scope in the lab
100s
s +1000s+1e6
2
Transfer Fcn
0.802
Slider
Gain
Scope
s
1
Integrator
sin
Elementary
Math
Dot Product2
5
Constant1
2000
Constant