0% found this document useful (0 votes)
102 views37 pages

Engineering Workshop 2 (Beee1313)

This document discusses MATLAB for engineering applications involving matrix operations, graph plotting, and Simulink. It covers topics such as creating and manipulating matrices, basic matrix algebra, using matrices to solve circuit problems, generating sequences for graph plotting, and an introduction to the Simulink environment. Examples are provided for performing operations on matrices, using matrices to solve a circuit analysis problem, and plotting a sine wave graph. The document is intended to help students understand and apply key MATLAB and Simulink skills for engineering applications.

Uploaded by

Ed Itr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views37 pages

Engineering Workshop 2 (Beee1313)

This document discusses MATLAB for engineering applications involving matrix operations, graph plotting, and Simulink. It covers topics such as creating and manipulating matrices, basic matrix algebra, using matrices to solve circuit problems, generating sequences for graph plotting, and an introduction to the Simulink environment. Examples are provided for performing operations on matrices, using matrices to solve a circuit analysis problem, and plotting a sine wave graph. The document is intended to help students understand and apply key MATLAB and Simulink skills for engineering applications.

Uploaded by

Ed Itr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

ENGINEERING WORKSHOP 2 (BEEE1313)

LAB 2: MATLAB FOR ENGINEERING APPLICATION ON MATRIX


OPERATIONS, GRAPH PLOTTING, AND SIMULINK

Mohammad Faizal Zulkifli1, Amar Faiz Zainal Abidin2


[email protected]

[email protected],
LESSON OUTCOMES
At the end of this lesson, you should be able to:
1. Understand the concept of a matrix and array
2. Understand with the basic matrix manipulation and notation in MATLAB
3. Perform basic matrix algebra
4. Apply matrix for solving engineering application.
5. Understand the concept of graph plotting in MATLAB
6. Apply graph plotting to display graph related to engineering application.
7. Familiarize the SIMULINK environment
8. Perform basic SIMULINK simulation related to engineering application
9. Apply SIMULINK for solving engineering application.
MATLAB MATRIX
1. In Mathematics, a matrix is collection of numerical values
that are organized in terms of rows and columns.
2. A matrix can be express in MATLAB by by typing each
element of a row, row by row, with a space or a comma
separating the consecutive elements in a row, and
semicolons to separate consecutive rows of a matrix.
7
3. Example, 𝑥 = [1 9 −10ሿ and y = 4 can expressed in
2
MATLAB using the following MATLAB command (refer Fig. 1):
>> x = [1 9 -10] % Or x = [1, 9, -10]
>> y = [7; 4; 2] Fig 1. Example matrix in MATLAB
MATLAB MATRIX
4. A few more example to help you get the hang of the concept,
given 𝑎 = 1 2 3 and 𝑏 = 1 2 can be written (refer Fig. 2):
4 5 6 3 4
>> a = [1 2 3; 4 5 6]
>> b = [1 2; 3 4]
5. There also two commands to help you out in manipulating
the matrix that you created in MATLAB. The commands are:
• size()command will return the size of the matrix
• length()command will return the largest dimension
of the matrix
Fig 2. Example matrix in MATLAB
MATLAB MATRIX
6. By taking the earlier example where 𝑎 = 1 2 3 and 𝑏 = 1 2 ,
4 5 6 3 4
writing the command size(a) will return two numerical values:
2 and 3 which means 2 is the number of rows and 3 is the number
of columns in matrix a (refer Fig. 3).
7. Then, if you try writing down command size(b), MATLAB will
return the values of 2 and 2 which means matrix b has 2 columns
and rows (refer Fig. 3).
8. As stated earlier the command length(a) will return the
largest dimension of the matrix. For matrix a, the value will be 3
because the largest dimension of matrix a is its column which is 3.
While length(b) will return 2 as both column and row of
matrix b is 2 (refer Fig. 3). Fig 3. Example function
size and length
MATLAB ARITHMETICS
9. MATLAB can perform all basic mathematical arithmetic for matrix that you can
think of. Table 1 shows the list of the mathematical operation that we will try
out later. Let say we have wrote z = [1 2; 3 4];
Table 1. Basic MATLAB arithmetic commands
Symbol Operation Example Answer
A =
+ Addition A = Z + Z 2 4
6 8
A =
- Subtraction A = Z - Z 0 0
0 0
A =
A = Z * Z
* Multiplication 7 10
15 22
A =
.’ Transpose A = Z.’ 1 3
2 4
MATLAB ARITHMETICS
10. On top of that, Table 2 listed additional functions that are commonly used.

Table 2. Additional MATLAB mathematical functions


Math
Function Operation Example Answer
Expression
B =
−1
inv(a) Inverse B = inv([1 2; 3 4]) 1 2
𝐵= -2.0000 1.0000
3 4
1.5000 -0.5000

𝑇
B =
transpose(a) Transpose B = transpose([1 2; 3 4]) 1 2 1 3
𝐵=
3 4 2 4
𝐵 B =
det(a) Determinant B = det([1 2; 3 4]) 1 2 𝑇
= det( ) -2
3 4
MATLAB MATRIX
11. Now, let see how we can apply what we had learned in solving electrical circuit.
Hopefully, you still can recall what you have learned in ECF & Technical Mathematics.

Figure A shows an electrical circuit consists of several resistors. Find 𝐼1 , 𝐼2 and 𝐼3 for the given
electrical circuit

Figure A. Electrical circuit


MATLAB MATRIX
12. Applying KCL, you should be able to obtain the following three equations:
Loop 1: 10 𝑉 = 76 𝐼1 − 25 𝐼2 − 50 𝐼3
Loop 2: 0 𝑉 = −25 𝐼1 + 56 𝐼2 − 1 𝐼3
Loop 3: 0 𝑉 = −50 𝐼1 − 1 𝐼2 + 106 𝐼3
13. And if we translate this into matrix form, it should become:
76 −25 −50 𝐼1 10
−25 56 −1 𝐼2 = 0
−50 −1 106 𝐼3 0
14. Rearrange the matrix
𝐼1 −1
76 −25 −50 10
𝐼 = 𝐼2 = −25 56 −1 0
𝐼3 −50 −1 106 0
MATLAB MATRIX
15. And now, let’s translate it into MATLAB command:
>> R = [76 -25 -50; -25 56 -1; -50 -1 106];
>> V = [10; 0; 0];
>> I = inv(R) * V
16. You should get 𝐼1 = 0.2449 𝐴 ,
𝐼2 = 0.1114 𝐴 and 𝐼3 =
0.1166 𝐴 at your Command
Window (refer Fig. 4).

Fig 4. Solving electrical circuit problem using MATLAB


ACTIVITY A: MATLAB MATRIX
1. Open MATLAB program.
2. Write the following command at your Command Window:
>> A = [-10 2; 7 15]; B = eye(2);
>> C = A + B, D = A – B, E = A * B
>> F = A.’, G = transpose(A)
>> H = A.^(-1), I = inv(A)
>> J = det(A)
3. Compare the MATLAB result with manual calculation.
ACTIVITY A: MATLAB MATRIX
4. Another question to test your understanding on applying what you have learned in
engineering application. No manual calculation are required (Yeah! )
Consider the electric circuit shown in Figure B. Determine the currents 𝐼1 , 𝐼2 and 𝐼3 .

Figure B. BEEU1013 Test 1 Question 3 Sem 1 2018/2019


MATLAB GRAPH PLOTTING
1. As mentioned in the first workshop, MATLAB has the capability to present result
in numerous graphical representation.
2. In this topic, we will explore the basic MATLAB graph plotting capability.
3. The easiest way to generate a sequence of number is by using the following
command: n = [initial:increment:final] where initial is the
starting value, final is the ending value, and increment is the value it will
increment from starting value to the ending value. For example:
>> a = [1:1:5] % Output will be a = 1 2 3 4 5
>> b = [5:-1:1] % b = 5 4 3 2 1
>> c = [-1:0.5:0] % c = -1.0000 -0.5000 0
MATLAB GRAPH PLOTTING
4. So where do apply this sequence number? Let just say you want to plot a sine wave
from 0° to 360° with an interval value of 1° . This can be done using MATLAB using the
following commands:
>> a = [0:pi/180:2*pi] % Inputs to sine() are in radian
>> y = sin(a)
>> plot(a,y)
5. The first line of the command is to generate a sequence of value from 0° , 1° , 2° ,
3° ,…, 358° , 359° , 360° . The second line will generate values of y based on the
sine function and input a. The last line will plot the graph where the first input of the
function reresent x-axis values and second input is for the y-axis values.
MATLAB GRAPH PLOTTING
6. A new window will pop up to display the result
(as in Fig. 4). Notice that the graph is a bit dull and
boring 
7. In order to enhance the readability of the graph,
there a few command that we can try out! Now
extend the commands with the following
commands:
>> grid ON;
>> legend(‘Sine’);
>> title(‘Sine Waveform’);
>> xlabel(‘Angle (radian)’); Fig 4. Plotting sine wave on MATLAB
>> ylabel(‘Output y’);
MATLAB GRAPH PLOTTING
8. Notice that the graph becomes more visually appealing (refer Fig. 5).

title(‘Sine Waveform’):
display tittle with given sentences
legend(‘Sine’):
display legend with
given sentences

ylabel(‘Output’: display
grid ON: turn ON the
the label for y-axis with given
sentences grid for the graph

xlabel(‘Angle(radian)’:
display the label for x-axis with
given sentences
Fig 5. Plotting sine wave on MATLAB
MATLAB GRAPH PLOTTING
9. Another cool feature of MATLAB graph plotting is you can plot more than one output
in a single graph. Let just say we want to plot cosine and sine wave together in one
graph, the command as follows
>> a = [0:pi/180:2*pi];
>> y = sin(a); z = cos(a);
>> plot(a,y,’red’); hold;
>> plot(a,z,’blue’); grid ON
>> legend(‘Sine’, ‘Cosine’);
>> title(‘Trigonometry Waveforms’);
>> xlabel(‘Angle (radian)’);
>> ylabel(‘Output’);
Fig 5. Plotting sine and cosine wave on
a single figure
MATLAB GRAPH PLOTTING
10. Let us try out what we had learned earlier in the following engineering application:

Based on the circuit connection shown in Figure A, Ir. Ts. Syahrin decides to create his own
MATLAB program that plot the values of Voltage across capacitor, 𝑉𝑐 for defined time (𝑡)
during the charging phase. Values of Voltage Supplied (𝑉𝑠 ) is 240 V, Resistance (𝑟) is 5 kΩ,
Capacitance (C) is 0.6 µF, and time (𝑡) from 0 µs to 20,000 µs with interval time of 1 µs.

Figure C: RC Discharging Circuit


MATLAB GRAPH PLOTTING
10. Let us try out what we had learned earlier in the following engineering application:
𝑡
−𝑟𝐶
Step 1: Identify suitable formula. In this case: 𝑉𝑐 = 𝑉𝑠 𝑒
Step 2: Determine suitable name for all variables
Based on the circuit connection shown in Figure C,
Variable names:
Ir. Ts. Syahrin decides to create his own MATLAB
• Vc is Capacitor Voltage • r is Resistance value
program that plot the values of Voltage across
• Vs is Supply Voltage • C is Capacitance value
capacitor, 𝑉𝑐 for defined time ( 𝑡 ) during the
• t is time
charging phase. Values of Voltage Supplied (𝑉𝑠 ) is
Step 3: Write command for the sequence variable: 240 V, Resistance (𝑟) is 5 kΩ, Capacitance (C) is 0.6
>> t = [0:1e-6:20000e-6];
µF, and time (𝑡) from 0 µs to 20,000 µs with
Step 4: Write commands for the remaining variables: interval time of 1 µs.
>> r = 5e3; Vs = 240; C = 0.6e-6;
Step 5: Write command for the formula:
>> Vc = Vs * exp(-t/(r*C));
Step 6: Write command to plot the graph:
>> plot(t,Vc);
Step 7: Write commands to plot the enhance the graph:
>> grid ON; xlabel(‘time (s)’);
Figure C: RC Discharging Circuit
>> ylabel(‘Capacitor voltage, Vc (V)’);
>> title(‘RC Discharging circuit’);
MATLAB GRAPH PLOTTING
11. The entire command in one chunk as shown below. You should obtain graph as
illustrated in Fig. 6.
>> t = [0:1e-6:20000e-6];
>> r = 5e3; Vs = 240; C = 0.6e-6;
>> Vc = Vs * exp(-t/(r*C));
>> plot(t,Vc);
>> grid ON; xlabel(‘time (s)’);
>> ylabel(‘Capacitor voltage, Vc (V)’);
>> title(‘RC Discharging circuit’);

Fig 6. Plotting RC discharging circuit


equation in MATLAB
ACTIVITY B: MATLAB GRAPH PLOTTING
1. Write the following commands at your Command Window:
>> m = [0:pi/180:2*pi]; y = sin(m); plot(m,y);
2. Explain what you understand from the command written.
3. Now, change pi/180 to pi/4. Observe the difference and explain why it occur.
4. Given to you equation: 𝑦 = 2 + (𝑥 − 1) 2 . Fill in the Table given in the laboratory
sheet. Then, draw the graph on the provided area inside the laboratory sheet. Label
the graph appropriately.
5. Once you done with (4), write a suitable MATLAB command to display the graph.
Label the graph appropriately. Does graph (5) is the same with graph (4)? Why?
ACTIVITY B: MATLAB GRAPH PLOTTING
1. Let us revisit the previous engineering problem, write suitable command to solve
the problem:
Based on the circuit connection shown in Figure D, Ir. Ts. Syahrin decides to create his own
MATLAB program that calculate values of Voltage across capacitor, 𝑉𝑐 for defined time (𝑡)
during the charging phase. Values of Voltage Supplied (𝑉𝑠 ) is 240 V, Resistance (𝑟) is 5 kΩ,
Capacitance (C) is 0.6 µF, and time (𝑡) is from 0 ms to 100 ms with interval of 0.01 ms.

Figure D: RC Charging Circuit


MATLAB SIMULINK
1. Another tool that makes MATLAB standout
from other software is the Simulink
Toolbox.
Fig 7. Icon New
2. This toolbox allow user to perform
mathematical and simulation by connecting
blocks, instead of writing lengthy
commands.
3. To access MATLAB Toolbox, click at the New
icon (refer Fig. 7), Then click Simulink
Model (refer Fig. 8). A new window as Fig. 9
Fig 8. Icon
will pop up. Simulink
Fig 9. New Simulink Model file
Model
MATLAB SIMULINK
4. Next, we will need to open the library to access
all the block, at the main window, click at
Simulink Library icon as shown in Fig. 10. A new
window called Simulink Library Browser will pop Fig 10. Simulink Library icon
up.
5. Notice there a number of libraries and each
library contains sub-libraries, and each sub-
libraries contains a function block.
6. For this laboratory session, we will focus only on
the Simulink library.

Fig 11. New Simulink Model file


MATLAB SIMULINK Output Input
7. To insert a block into the Simulink Model file. Click at the
desire block and drag the block into the Simulink Model block.
8. You can rearrange the location of the block by clicking the Fig 12. Input and Output
block and drag to the new location.
9. To connect the blocks, you need to click and hold the left
mouse button of the output arrow (refer Fig. 12) of a block Fig 13. Successful connection
then move the mouse cursor to the input arrow of another
block (refer Fig. 12) and release the button
10. A successful connection between two blocks will give you a Fig 14. Failed connection
black line (refer Fig. 13) while a failed connection will return
you a red line (refer Fig. 14).
11. To edit the parameter value of a block, double click the block:
it will pop up the Function Block Parameters window. Figure
15 shows for Math Function Parameters window.
Fig 15. Function Block
Parameters window
MATLAB SIMULINK
12. Table 3 shows the commonly used Simulink block:
Table 3. Commonly used Simulink block
Icon Block Name Sub-library Icon Block Name Sub-library
Math Trigonometric
Math Operation Math Operation
Function Function
XY Graph Sink
Abs Math Operation

Product Math Operation Display Sink

Divide Math Operation Scope Sink

Add Math Operation Step Source

Subtract Math Operation Constant Source


MATLAB SIMULINK
13. Figure 15 shows the connection of
several blocks. In order to see the output
of the block diagram connections, you
can click at the Play icon (refer Fig. 16).
14. The output for each block should be as
follows (refer Fig. 17): Fig. 15. Example
• Math function: 𝑒 −4 = 0.01832 Simulink block
diagrams
• Abs: −4 = 4
• Product: −4 × 20 = −80
• Divide:−4 ÷ 20 = −0.2
Fig. 16. Play icon Fig. 17. Result of the
• Add:−4 + 20 = 16 Simulink block diagrams
• Subtract:−4 − 20 = −24
MATLAB SIMULINK
15. Sometimes you want to plot a graph, let say, you want to plot the equation:
𝑦 𝑡 = −4 + (𝑡 − 1) 2 from 𝑡 starts at 0 second to 20 second with interval time of
1 second. The first step is to identify suitable Simulink block to be use. In this case:
• Three Constant blocks for −4 , 1 and power of 2
• One Add block for addition operation
• One Subtract block for subtraction operation
• One Math Function block for power operation
• One Clock block to represent 𝑡
• One Scope block to display the output graph
MATLAB SIMULINK
16. The second step is to solve connect the blocks according to the mathematical order.
For 𝑦 𝑡 = −4 + (𝑡 − 1) 2 , you can solve within four orders. Your Simulink Model
should be similar to Fig. 18.

4th Order: 𝑦 𝑡 = −4 + (𝑡 − 1) 2
2nd Order:(𝑡 − 1) 2

1st Order: 𝑡 − 1

3rd Order:−4 + (𝑡 − 1) 2

Fig 18. Simulink Model for solving quadratic equation


MATLAB SIMULINK
17. The third step is to update the simulation
time you need to click Simulation menu
(refer Fig. 18), then click Model Fig 18. Simulation menu
Configuration Parameters menu (refer Fig. 2
1
19). The Configuration Parameters window 3
will pop up (refer Fig. 20). Change the start 4
time and stop time according to needs, At
Solver Type, choose Fixed-Step and set the
Fixed-Step size according to question.

Fig 19. Model Configuration Fig 20. Model Configuration Parameters


Parameters
MATLAB SIMULINK
18. Once you already edit the properties, click Apply. Then Click Play icon to simulate
the circuit. Once the simulation completed, double click the Scope block. A new
window that shows the result will pop up (refer Fig. 21).

Fig 21. Output graph of the quadratic equation


MATLAB GRAPH PLOTTING
10. Let us try out what we had learned earlier in the following engineering application:
𝑡
−𝑟𝐶
Step 1: Identify suitable formula. In this case: 𝑉𝑐 = 𝑉𝑠 𝑒
Based on the circuit connection shown in Figure E,
Step 2: Determine suitable block to represent the variables Ir. Ts. Syahrin decides to create his own MATLAB
• Three Constant blocks for 𝑉𝑠 , 𝑟 and 𝐶. program that plot the values of Voltage across
• One Divide block for division operation capacitor, 𝑉𝑐 for defined time ( 𝑡 ) during the
• Two Product blocks for subtraction operation charging phase. Values of Voltage Supplied (𝑉𝑠 ) is
• One Math Function block for exponential 240 V, Resistance (𝑟) is 5 kΩ, Capacitance (C) is 0.6
• One Clock block to represent 𝑡
µF, and time (𝑡) from 0 µs to 20,000 µs with
• One Scope block to display the output graph
interval time of 100 µs.
Step 3: Connect the block diagram according to the
mathematical order (refer Fig. 22)

Figure E: RC Discharging Circuit


Step 5: Change the simulation parameters
Fig. 22. Simulink Model of RC discharging circuit
MATLAB GRAPH PLOTTING
10. Let us try out what we had learned earlier in the following engineering application:
Step 4: Run the simulation & Voila! (refer Fig. 23). Right click Based on the circuit connection shown in Figure E,
and click Autoscale to automatically scale the graph. Ir. Ts. Syahrin decides to create his own MATLAB
program that plot the values of Voltage across
capacitor, 𝑉𝑐 for defined time ( 𝑡 ) during the
charging phase. Values of Voltage Supplied (𝑉𝑠 ) is
240 V, Resistance (𝑟) is 5 kΩ, Capacitance (C) is 0.6
µF, and time (𝑡) from 0 µs to 20,000 µs with
interval time of 100 µs.

Figure E: RC Discharging Circuit

Fig. 23. Graph of RC discharging circuit


ACTIVITY C: MATLAB APPLICATION
1. Open the Simulink Model. Draw the following block diagram in Figure F. & run.
Note down the result in your laboratory sheet and translate the equation that
represents the block diagram. Validate your solution with manual calculation.

Figure F: Block diagram


ACTIVITY C: MATLAB APPLICATION
2. Let us revisit the same problem again, now try using Simulink to solve the problem.
Compare your solution with solution in Activity B. Discuss your observation.
Based on the circuit connection shown in Figure G, Ir. Ts. Syahrin decides to create his own
MATLAB program that calculate values of Voltage across capacitor, 𝑉𝑐 for defined time (𝑡)
during the charging phase. Values of Voltage Supplied (𝑉𝑠 ) is 240 V, Resistance (𝑟) is 5 kΩ,
Capacitance (C) is 0.6 µF, and time (𝑡) is from 0 ms to 100 ms with interval of 0.01 ms.

Figure G: RC Charging Circuit


SUMMARY
What we have learned?
• MATLAB matrix: Commands of basic arithmetic manipulation
in matrices. Solved electronic circuit problem.
• MATLAB graph plotting: Commands of basic graph plotting.
Solved RC circuit problem
• MATLAB Simulink: Commonly used blocks and solved RC
circuit problem.
REFERENCES
1. K. Bennett, MATLAB: Applications for the Practical Engineer, London: InTechOpen
Limited, 2014.
2. H. Moore, MATLAB for Engineers (5th Edition), London: Pearson, 2017.
3. M. Sadiku, C. Alexander, Fundamentals of Electrical Circuits (5th Edition), Columbus:
McGraw-Hill Education, 2012.

You might also like