Académique Documents
Professionnel Documents
Culture Documents
1
4.2 Exchanging data between the SIMULINK and MATLAB workspace. ......................................... 49
4.3 The SimPowerSystems library. ...................................................................................................... 52
4.4 Modeling simple electric circuits using the SimPowerSystems library. ........................................ 53
Day 5: Exam .................................................................................................................................................... 57
2
Day 1: Getting Started with MATLAB
More details about what is MATLAB for the students can be found on the following link
http://www.mathworks.com/academia/student_version/details.html
The official website for the MATLAB software is http://www.mathworks.com/. In this website,
you can find anything you need to know about the MATLAB.
You can do with the MATLAB more than you can count or imagine. Things that can be done
using the MATLAB may be grouped under the following items
‐ Mathematical computation and matrix manipulations.
‐ Implementing algorithms (writing MATLAB programs that perform specific tasks).
‐ Based on the previous two items, Mathematical modeling (creating models) and
optimization can be done using MATLAB.
‐ Statistical data analysis and data management (examples of data management includes data
sorting and data filtering).
‐ Visualizing data (plotting of functions and data).
‐ Creating user interfaces.
‐ Interfacing with other software and programming languages such as excel, C++, and JAVA.
‐ Symbolic computation.
‐ Report generation.
‐ Hardware Interface and control.
3
‐ Numerical Calculations.
‐ Calculus and Differential Equations.
‐ Statistics.
‐ Integration.
‐ Transforms.
MATLAB has many built-in functions (Many toolboxes) that make the programming easy and
friendly. Examples of toolboxes in MATLAB are
‐ Control System Toolbox: Design and analyze control systems
‐ Data Acquisition Toolbox: Connect to data acquisition cards, devices, and modules
‐ DSP System Toolbox: Design and simulate signal processing systems
‐ Image Processing Toolbox: Perform image processing, analysis, and algorithm development
‐ Instrument Control Toolbox: Control and communicate with test and measurement
instruments
‐ Optimization Toolbox: Solve standard and large-scale optimization problems
‐ Signal Processing Toolbox: Perform signal processing, analysis, and algorithm development
‐ Simulink Control Design: Compute PID gains, linearize models, and design control systems
‐ Statistics and Machine Learning Toolbox: Perform statistical modeling and analysis
‐ Symbolic Math Toolbox: Perform symbolic math computations
Complete List of the toolboxes in MATLAB can be found in the following link
http://www.mathworks.com/products/
After installing the MATLAB on your computer, the MATLAB is started by double clicking the
following icon
You can find this icon on the desktop and/or in the start menu\programs and/or in the following
path
C:\Program Files\MATLAB\R2007b\bin
Once the MATLAB is opened, you will see the following interface
4
Fig.1 MATLAB interface
Changing the working folder can be done by clicking the button in the following figure. The
working folder is the folder where you save your work such as MATLAB code or workspace
variables. After clicking this button; browse for the folder you need, choose it and click Ok.
5
Fig.2 changing the working folder
Then press “enter” in your keyboard. MATLAB will write the following
x=
2
MATLAB has created a 1 by 1 matrix with a name x and stored in it a value of 2. Now, you can
observe this variable in the workspace window.
Now, execute the following commands and see what will happen
>> sqrt(3)
Since you did not create a variable, the MATLAB created one for you with a name “ans” and it
stored the results in the variable “ans”. Now, Try
>> 2+3
When you define a variable and press enter, the MATLAB rewrite it on the command window. To
prevent this, add a semicolon as follow
6
>> z=3;
After writing the above command and pressing enter, the MATLAB will not display anything;
however, observe that a variable with name “z” has been created in the workspace window and has
been assigned a value of 3. Now, defined more than one variable and perform a simple
computation as follow
>> a=3;
>> b=25*2;
>> c= a^3+b
MATLAB evaluates an expression according to the precedence levels of the operators. The
precedence levels of the main operators from the highest to the lowest is as follows
1. Parentheses “( )”
2. Power “^”
3. Multiplication “*” and division “/”
4. Addition “+” and subtraction “-“
Operators have the same precedence level are evaluated from the left to the right. Now, what do
you expect to be the result of the following computation?
>> a=1;
>> b=2;
>> c= 4;
>> d=a*c^2/b+a-b*(c-b)
If you forget the variable you have defined in MATLAB, use the command who or whos to get a
list of the variables defined in the work space
>> who
7
MATLAB answer will give you the variables defined in your workspace
Your variables are:
a b c d
a 1x1 8 double
b 1x1 8 double
c 1x1 8 double
d 1x1 8 double
If you need to clear the command window, use the command “clc” which will delete all the
commands and answers written in the command window. Please, note that the previous executed
commands are still available in the command history window.
>> clc % to clear the command window
To delete the variables you have defined in the workspace, use the command “clear”
>> clear % to clear the workspace
Now, let us define more basic data elements such as vectors and matrices. To define a row vector,
write for example
>> row=[1 2 3 4]
8
2 3 4
a 5 6 7
8 9 10
Write the following command and press enter
>> a=[2 3 4;5 6 7;8 9 10]
The MATLAB will display the matrix “a” since you did not put a semicolon at the end of the
command. You will observe also that MATLAB has added a matrix in the workspace with a name
“a”.
a=
2 3 4
5 6 7
8 9 10
Now, define a second matrix “b” and multiply “a” by “b” as follows
>> b=[5 1 2;3 4 6;6 5 7];
>> c=a*b
MATLAB answer is
ans =
Inf
MATLAB answer is
ans =
Inf
9
Type the following
>> 0/0
MATLAB answer is
ans =
NaN
MATLAB answer is
ans =
NaN
MATLAB answer is
ans =
3.1416
MATLAB answer is
ans =
1.7977e+308
MATLAB answer is
ans =
2.2251e-308
10
MATLAB answer will provide you with all information you need about the command “clear”.
You can also use the command “doc” to get help about a certain MATLAB function such as
“clear”
>> doc clear
The following help window will open, in which you can browse for the topic you need help about
or search for this topic
Example 1: to calculate sin (90) in MATLAB, write the following command. Please, observe that
the sin argument should be in radians
>> sin(pi/2)
11
1
Table 2 lists some MATLAB functions used to perform simple matrices operations
Example 1: to create a 5 by 5 random matrix, and get the inverse of this matrix; use the following
commands
>> y=rand(5,5)
>>x=inv(y)
12
1 2 3
a 4 5 6
7 8 9
To recall the element in the second row and third column, write the following in MATLAB
>> a(2,3)
13
You can use the colon “:” to create vectors as follows
>> x=1:4
1 2 3 4
Or you can create a vector with a certain step between the elements as follows
>> y=1:3:10
14
Day 2: Writing simple MATLAB programs
>>a=2+i*3
2.0000 + 3.0000i
You can also use the following two commands to define the same complex number
>>a=complex(2,3)
Or
>>a=2+j*3
The following table lists MATLAB functions used in complex number computations.
2.2 MATLAB commands used for 2-D plotting and 3-D plotting.
2-D plotting in MATLAB can be done using plot command as in the following examples
>>x=1:1:20;
>>y=x;
>>plot(x,y)
15
MATLAB will plot the following figure
20
18
16
14
12
10
0
0 2 4 6 8 10 12 14 16 18 20
Another example
>>wt=0.01:0.01:4*pi;
>> x=sin(wt);
>>y=cos(wt);
>>plot(wt,x,wt,y)
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 2 4 6 8 10 12 14
16
Table 4 presents some MATLAB commands used in plotting.
Example
>>wt=0.01:0.01:4*pi;
>> x=sin(wt);
>>y=cos(wt);
>>plot(wt,x,wt,y)
>> grid on
>> legend('sin wave','cos wave')
>> axis([0 2*pi -1.5 1.5])
>> xlabel('wt')
>> ylabel('sin and cos values')
>> title(' sin and cos waves')
0.5
sin and cos values
-0.5
-1
-1.5
0 1 2 3 4 5 6
wt
17
To plot a 3-d plot in MATLAB, use the command plot3 as in the following example
>>t = 0:pi/50:10*pi;
>>x = sin(t);
>>y=cos(t);
>>plot3(t,x,y)
0.5
-0.5
-1
1
0.5 40
0 30
20
-0.5 10
-1 0
18
Fig.8 Opening an M-file
A third method to open an M-file is to write the command edit in the command window
>>edit
As a result for any of the above mentioned methods, an M-file editor window will open as shown
in the following file. The M-file editor is used to create and manage an M-files.
19
Fig.10 M-file editor window
In the M-file editor window, write the following set of commands as shown in the following figure
20
Before running the above script file, you need to save it first. You can save the file by clicking on
the save button as shown in the following figure
Clicking on the save button will open the browse window shown in the next figure
The above rules are also valid for naming variables in MATLAB. If you need to check if a variable
name or M-file name is correct or not, use the MATLAB function isvarname. An example will
follow
>>isvarname 7_program
Zero value means that the above name is incorrect in MATLAB. If the name is correct, MATLAB
will respond by a value of one.
After saving the M-file with a correct name, you can run this file by clicking the run button as
shown in the following figure
22
The result of running an M-file will be shown in the MATLAB command window. The result of
running the above M-file will be as follows
c=
40.1711
z=
80.3421
y=
24
In the above figure, “T_P_converter” is the function name. This function receives two inputs
(celsius and pascal) and returns two outputs (kelvin and bar).
To save the above function, you should save the M-file with the same name as the function name
which is “T_P_converter”.
To use the above function after saving it, write the following code in the command window
>> celsius=15;
>> pascal=250000;
>> [temperature,pressure] = T_P_converter(celsius, pascal)
23
MATLAB will give you the following answer
temperature =
288.1500
pressure =
2.5000
pressure =
0.2176
MATLAB evaluates an expression, and as long as this expression is true, MATLAB will
repeatedly execute a group of statements. An example will follow
Save the M-file and run it, MATLAB will give the following answer in the command window
hello world
hello world
hello world
hello world
24
for index = values
execute statements
end
For loops are used to repeatedly execute a set of statements for a specified number of times
25
Save the M-file and run it, MATLAB will give the following answer in the command window
hello world
hello world
hello world
hello world
hello world
Open an M-file and write the code shown in the following figure
Save the M-file and run it, MATLAB will give the following answer in the command window
26
x is between 5 and 10
value of x is : 7
Observe the use of the MATLAB function fprintf in the previous M-file. fprintf can be used to
display a numerical value of a certain variable within a sentence on the command window. For
more information on using the fprintf function or any function in MATLAB, you can use the
MATLAB command doc as explained at section (1.7) of this course.
One of the wonderful toolboxes in MATLAB is the symbolic math toolbox. This section provides
few examples on symbolic computations using MATLAB.
The following is an example of calculating the derivative of a certain function. Assume the
following function
f (t ) 2 t 3 sin(t )
To calculate the derivative of the above function, write the following commands in MATLAB
>>syms t
>>f=2*t^3+sin(t);
>>diff(f)
6*t^2+cos(t)
60*x^2
In order to differentiate a function with respect to a certain variable, see the following example
>> syms t x
>>f=3*x*t+x;
>>a=diff(f,'t')
27
MATLAB answer will be
a=
3*x
If you need to differentiate a certain function (f) n times with respect to a certain variable x, use
the following expression diff(f,'x',n).
2/3*x^3
16/3
To perform an indefinite integral with respect to a certain variable, say ‘x’, use the command
int(f,’x’,a,b).
>> syms x t
>>f=3*x*t;
>>int(f,'x',0,2)
28
MATLAB will plot the following
x3
200
150
100
50
-50
-100
-150
-200
-6 -4 -2 0 2 4 6
x
>> syms x
>>f=3*x^3+exp(x);
>>subs(f, 5)
523.4132
You can solve equations in MATLAB using the command solve. Write in MATLAB the following
>> [x,y] = solve('x + 5*y = 7','x - 5*y = 5')
29
Day 3: Getting Started with SIMULINK
>> simulink
30
You can also start the SIMULINK by clicking on the button shown in the following figure
To open a graphical model editor, in which you can build the model you need to simulate, from the
SIMULINK library browser window click on file > new > model as shown in the following figure
31
A SIMULINK model editor will open as shown in the following figure
32
To open demos for SIMULINK models, click on the word Simulink as shown in the following
figure. A list of all the Simulink Demos will open in the left pane of the window.
To open any Simulink Demo, click on the name of the model which will open a page that
describes the model as shown in Fig. 26. After that, click on “Open this model” link to see the
Simulink model as shown in Fig. 27.
33
Fig.27 opening a Simulink
Once you click on “Open this model” link, the Simulink model will open as shown in Fig. 28.
You may choose to run the model, close the model, or save the model in order to modify it as
shown in Fig. 28.
You can press the back button as shown in Fig. 27 to return back to the main list of Simulink
demos in order to see more examples of systems that you can simulate using the Simulink.
34
3.5 Opening important libraries.
Open the SIMULINK library browser by writing Simulink in the MATLAB command window,
and then press enter. Click on the commonly used blocks link as shown in the following figure
To open the math operations library click on Math operations link as shown in the following figure
35
Blocks of libraries such as Sinks, Sources, Continuous, Signal Routing, and User-Defined
functions library can be shown by the same way. See the following figures
36
Fig. 33 blocks of the Continuous library
37
Fig. 35 blocks of the User-defined Functions library
38
A new model will open as shown in the following figure
You will build your model in the new model window by dragging the blocks you need from the
library browser window to the new model window. (Or Right-click on the block and select “add to
…”. See the following figure.
39
Now add the blocks in the following figure to the model window
Now double click on the constant block to open the following window in which you will change
the Constant value from the default value of “1” to “5” and then press Ok.
Now you need to connect the blocks together to build your model. You do that as shown in the
following figure.
40
Fig. 41 connecting two blocks
After connecting all the blocks you will have the model shown in the following figure. In this
figure, click on save followed by run as shown.
After saving and running the model, you will get the results shown in the display block. See the
following figure.
41
Fig. 43 viewing the results after running the model
Save the above model and run it. To see the results, double click on the scope model to open the
scope window in which you will see the output.
42
Fig. 45 the scope window
After opening the scope window, press on the Autoscale button as shown in the following figure
43
Day 4: Building simple models using SIMULINK
The following example will explain the use of the Fcn block. Assume you have an input for which
you need to perform the following computation
x (1 x)
f (x ) x
5
e
In the above expression x is the input and f(x) is the output. The Fcn block will be used to perform
the above computation.
44
In the Fcn block, the input has to be given the symbol “u”. So the mathematical expression will be
as follows
u (1 u )
f (u ) u
5
e
Let u=0.5, the following Simulink block will evaluate the above expression for an input value
equal to 0.5.
The SIMULINK model shown in Fig. 48 consists of a constant block, Fcn block, and display
block. After adding these blocks to a new SIMULINK model, connect them. After that, double
click on the constant block and write 0.5 in the constant value field, and then click on Ok. Double
click on the Fcn block and write the mathematical expression in the expression field as shown in
the next figure, and then press Ok. Run the model, and you will see the output on the display
block. As shown in Fig. 48, the result will equal to 0.2262.
45
Fig. 49 adding an expression in the Fcn block.
In order to embed a MATLAB function inside a SIMULINK model, use the Embedded MATLAB
Function block (shown in the following Figure).
46
The following example will explain how to use the Embedded MATLAB Function block. Add
five blocks to a new SIMULINK model as shown in Fig. 51.
Double click on the Embedded MATLAB Function. As a result, an embedded MATLAB editor
window will open as shown in Fig.52.
47
Change the code written in Fig.52 to that written in Fig. 53, and press on the disk button to save.
Close the embedded MATLAB editor window, and observe the changes in the SIMULINK model
which is shown in Fig. 54.
Fig. 53 changing the code inside the embedded MATLAB editor window
48
Connect the blocks and change the constant value of each constant block as shown in Fig. 55. Run
the model and the results are displayed in the display blocks as shown in Fig. 55.
Build a new SIMULINK model as shown in Fig. 56. Double click on the “From Workspace”
block, and enter the array [t, x, y] in the data entry. The SIMULINK model will be as shown in
Fig. 56. Run the model; double click on the scope; and press on the autoscale button. You will see
the output as shown in Fig. 57.
49
Fig. 56 using the From Workspace block
50
In order to extract a signal from the SIMULINK into MATLAB, use the “To Workspace” block.
You can find this block in the Sinks library. Build a new model in the SIMULINK as shown in
Fig. 58. You should expect finding the Sine wave block in the sources library.
Double click on the “To Workspace” block. Change the variable name to x, and change the Save
format to Array as shown in Fig. 59. Press ok, and run the model.
51
Go to the MATLAB main window, you will observe that two variables have been added to the
workspace. These two variables are tout and x. Now, write the following command in the
MATLAB command window.
>>plot(tout, x)
52
Fig. 61 the SimPowerSystems library
To see the blocks inside any sub-library or library, click on the “+” sign.
53
Ammeter 10 Ohm
A
Open a new SIMULINK model. Add to this model the following blocks
‐ Powergui
‐ DC Voltage Source
‐ Series RLC Branch (add two of this block)
‐ Voltage Measurement
‐ Current Measurement
‐ Display (add two of this block)
The Powergui block is a necessary block to simulate any electric system in the SIMULINK.
You can find the above blocks in the libraries/sub-libraries presented in the following table
Arrange the blocks as shown in Fig. 63. To rotate a block, right-click on this block and choose
Format, then Rotate block.
54
Fig. 63 Building a simple electric circuit in the SIMULINK
Double click on the DC Voltage Source, and change the amplitude value to 10. Double click on
the Series RLC Branch. Change the branch type to R, and change the resistance value to 10 as
shown in Fig. 64.
55
Connect the blocks as shown in Fig. 65. Run the model, and you can see the results on the display
blocks as shown in Fig. 65.
56
Day 5: Exam
Q1: What is the MATLAB function used for calculating the square root of a given number?
Q2: Evaluate the following expression in MATLAB when x=5, and y =7?
( y 5) 2
z
ex
Q3: State the MATLAB functions used to clear the command window and to clear the workspace?
Q4: Mention two MATLAB functions that can be used to recall the variables defined in your
workspace?
5 2 3 0
5 1 3 4
a
3 4 8 9
9 2 3 4
Q6: Using MATLAB, calculate the inverse of the matrix a defined in Q4?
Q7: Recall the second column of the matrix a defined in Q4 using MATLAB?
Q8: Recall the third row of the matrix a defined in Q4 using MATLAB?
Q9: Recall the element in the second row and third column of the matrix a defined in Q4 using
MATLAB?
y
Q11: How do you open the help page of the MATLAB function abs?
y cos (45 )
57
ln (10)
y
5!
Q14: Using MATLAB, calculate the transpose of the matrix a defined in Q4?
Q15: Using a suitable MATLAB function, create a matrix of 5 rows and 4 columns and all the
elements are equal to zero?
Q16: Using a suitable MATLAB function, generate a random matrix of 3 rows and 3 columns
where all the elements are generated randomly between zero and one?
Q17: using a suitable MATLAB command, create a vector which starts by 0 and ends by 200 with
a step of 2?
Q18: Find the conjugate number of the following complex number z in MATLAB?
z 5 6i
Q19: Create a vector t that starts from 0 to 2π, then plot x=cos(t) versus t using MATLAB.
Q20: For the figure obtained in Q19, add a label to the x-axis, a label to the y-axis, and a title to
the graph?
Q21: How do you verify if “myfirst_program” is a correct variable or file name in MATLAB?
Q22: Write a MATLAB script in which you define the following variables x=5, y=9, and z=10;
after that, use the script file to perform the following computations.
f (x z y ) 5
x
g
z y
Q24: Using a While statement, write a MATLAB script that displays the word “hello” on the
screen three times?
Q25: Using a For loop, write a MATLAB script that displays the word “hello” on the screen three
times?
58
f (t ) 2 t 3 cos(t )
Q27: Using MATLAB, find the forth derivative of the following function
f (t ) 2 t 6
x
7
dx
x y 6
3* x 4* y 16
Q31: What is the MATLAB command used to open the MATLAB Demos?
Q32: in which library do the following blocks exist? (Mention only one library that contains all
the blocks)
59
Q35: in which library do the following blocks exist?
x
sin( x ) e 5
f (x )
x 2 1
Q38: Using the embedded MATLAB function, convert from Kelvin to Celsius and from Bar to
Pascal if the input temperature is 300.6 Kelvin and the input pressure is 0.21758 Bar?
Q39: Using suitable blocks, simulate a cosine wave in the SIMULINK and export it to MATLAB,
and do the opposite which is creating a cosine wave in the MATLAB and adding it to a
SIMULINK model.
Q40: Using SIMULINK, find the readings of the ammeter and voltmeter in the following circuit
Ammeter 2.5 Ohm
A
60