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

Engineering Workshop 2 (Beee1313) : Lab 1: Matlab For Engineering Application On Arithmetic Operation

This document provides an overview of MATLAB for engineering applications and arithmetic operations. It introduces the MATLAB environment and components like the command window, workspace, and help documentation. It describes how to perform basic arithmetic operations in MATLAB like addition, subtraction, multiplication, and division. It also lists common mathematical functions in MATLAB like pi, power, exponential, logarithmic, trigonometric, and root functions. An example is provided to demonstrate solving a complex equation step-by-step using appropriate MATLAB commands according to mathematical order of operations. Finally, an activity asks the reader to write MATLAB code for various arithmetic expressions and equations.

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)
94 views

Engineering Workshop 2 (Beee1313) : Lab 1: Matlab For Engineering Application On Arithmetic Operation

This document provides an overview of MATLAB for engineering applications and arithmetic operations. It introduces the MATLAB environment and components like the command window, workspace, and help documentation. It describes how to perform basic arithmetic operations in MATLAB like addition, subtraction, multiplication, and division. It also lists common mathematical functions in MATLAB like pi, power, exponential, logarithmic, trigonometric, and root functions. An example is provided to demonstrate solving a complex equation step-by-step using appropriate MATLAB commands according to mathematical order of operations. Finally, an activity asks the reader to write MATLAB code for various arithmetic expressions and equations.

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/ 28

ENGINEERING WORKSHOP 2 (BEEE1313)

LAB 1: MATLAB FOR ENGINEERING APPLICATION ON


ARITHMETIC OPERATION

Mohammad Faizal Zulkifli1, Amar Faiz Zainal Abidin2


[email protected]

[email protected],
LESSON OUTCOMES
At the end of this lesson, you should be able to:
• Familiarize with MATLAB environment.
• Perform simple arithmetic operations using MATLAB.
• Define, use, express constant and assign values to variables using MATLAB as a
calculator.
• Write and execute simple MATLAB commands and functions.
• Write simple MATLAB commands for engineering applications.
INTRODUCTION TO MATLAB
1. MATLAB is acronym for MATrix LABoratory which is developed by MathWorks.
2. The software has been widely adopted by academicians in the engineering
field due to:
i. A load of built-in fuction for engineering application which reduce
programming time.
ii. The great visualisation capabilities that is suitable for presenting result
obtained.
MATLAB ENVIRONMENT
1. Start MATLAB by clicking double left clicking the MATLAB desktop icon.
2. The program will takes a while to load and during the process a splash screen
will be display.
3. By default, MATLAB displays a desktop interface with the following principal
components:
• The Command Window is to enter commands.
• The Workspace browser is to display the variables.
• The Current Folder browser is to display current files.
• The Command History is the record of previous works.
MATLAB ENVIRONMENT

Workspace:
display the
Current Folder: variables
display current Command Window: enter commands.
files
Command
History: record
previous works.

Fig 1. MATLAB Environment


MATLAB ENVIRONMENT
Semicolon: suppress
4. The MATLAB commands are entered in the command output
Command Window at the MATLAB prompt Prompt Percentage: Denotes comment
(>>).
5. Commands are executed once the ENTER key is
pressed.
6. The MATLAB command is case sensitive: a is
not the same as A. Command output: Display
7. A semicolon (;) at the end of a command result of the command
suppresses any output.
8. The percentage (%) denotes a comment.
Fig 2. MATLAB code
MATLAB ENVIRONMENT
9. Notice that the Workspace (refer Fig. 3) shows all variables
used with their current values.
10. Several keyboard shortcuts that are useful to assist in
entering commands: Fig 3. MATLAB Workspace
• Home, End: Move to the beginning, end of an expression.
• , : Move up, down the Command History.
• Esc: Deletes the expression at the prompt.
• Tab : Displays commands that complete a partial expression.
11. Other useful commands for managing the Command Window are:
• clc: Clears the Command Window.
• clear: Clear all the variables in the Workspace.
MATLAB ENVIRONMENT
12. There are two ways to access the Help
Documentation which are:
• Using helpdesk command (refer Fig. 4).
• Using help command. Example, to find
information about sqrt command (refer
Fig. 5). Fig 4. Using helpdesk command

Fig 5. Using sqrt command


ACTIVITY A: MATLAB ENVIRONMENT
1. Open MATLAB program.
2. Write the following command at your Command Window:
>> 10;
3. Then press ENTER. What do you see at your Command Window?
4. Next, try the following command:
>> 10
5. What is the difference between the two commands: (2) and (4)?
6. Now, try the following two commands and explain the difference:
>> 10 – 10
>> 10 % – 10
ACTIVITY A: MATLAB ENVIRONMENT
7. Try the following commands:
>> weight = 100
>> Weight
>> WEIGHT
>> wEiGhT
8. What do you observe? Why it happened?
9. Press  button. What do you see? How about if you press  button?
10. Try the clear command. What happen to the Workspace?
11. Lastly, try the clc command. What observation that you can make?
MATLAB ARITHMETICS
1. MATLAB can perform all basic mathematical arithmetic that you can think of.
Table 1 shows the list of the mathematical operation that we will try out later

Table 1. Basic MATLAB arithmetic commands


Symbol Operation Example Answer
+ Addition a = 12 + 1 a = 13
- Subtraction a = 12 - 1 a = 11
* Multiplication a = 12 * 2 a = 24
/ Right division a = 12 / 2 a = 6
\ Left division a = 3 \ 12 a = 4
^ Exponentiation a = 2 ^ 3 a = 8
MATLAB ARITHMETICS
2. In Table 2 listed mathematical functions that will be handy in your study:
Table 2. Additional MATLAB mathematical functions
Function Operation Example Math Expression Answer
pi π c = pi 𝑐=π c = 3.1416
power(a,b) Power (or 𝑎𝑏 in Math) c = power(2,5) 𝑐 = 25 c = 32
exp(a) Exponential (or 2.7183𝑎 in Math) c = exp(2) 𝑐 = 2.71832 c = 7.3891
log(a) Natural logarithm (or 𝑙𝑛 in Math) c = log(7) 𝑐 = 𝑙𝑛(7) c = 7.3891
log2(a) Base 2 logarithm c = log2(16) 𝑐 = 𝑙𝑜𝑔2 (16) c = 4
log10(a) Base 10 logarithm (or 𝑙𝑜𝑔 in Math) c = log10(2) 𝑐 = 𝑙𝑜𝑔(100) c = 2
2
sqrt(a) Square root ( 2 𝑎) c = sqrt(4) 𝑐= 4 c = 2
3
nthroot(a,b) n-th root ( 𝑏 𝑎) c = nthroot(8,3) 𝑐= 8 c = 2
cos(a) Cosine (in radian) c = cos(0) 𝑐 = cos(0°) c = 1
sin(a) Sine (in radian) c = sin(pi/2) 𝑐 = sin(90°) c = 1
tan(a) Tangent(in radian) c = tan(pi/4) 𝑐 = 𝑡𝑎𝑛(45°) c = 1.0000
MATLAB ARITHMETICS
3. A complex equation usually requires more than one function, lets take Eq. (1):
4
𝑎 = ln 2 + log 𝑒 81 (1)
4. The first step is to identify mathematical function that need to be use, in this case
there are four functions that need to be use:
• log(a) for solving ln 2 .
4
• nthroot(a,b) for solving 81.
4
81
• exp(a) for solving 𝑒
4
• log10(a) for solving log 𝑒 81
MATLAB ARITHMETICS
5. The second step is to determine the order of solving the mathematical operation.
Table 3 illustrates the order for solving the equation:
Table 3. Mathematical Order
Step Mathematical Equation Operation MATLAB Command
𝟒
1 𝑎 = ln 2 + log 𝑒 𝟖𝟏 → 𝑎 = ln 2 + log 𝑒 𝒃 n-th root b = nthroot(81,4)

2 𝑎 = ln 2 + log 𝑒 𝒃 → 𝑎 = ln 2 + log 𝒄 Exponential c = exp(b)


3 𝑎 = ln 2 + 𝒍𝒐𝒈 𝒄 → 𝑎 = ln 2 + 𝒅 Base 10 logarithm d = log10(c)
4 𝑎 = 𝒍𝒏 𝟐 + 𝑑 → 𝑎 = 𝒆 + 𝑑 Natural logarithm e = log(2)
5 𝒂= 𝒆+𝒅 Addition a = e + d
MATLAB ARITHMETICS
6. The last step is to write down the commands according to the mathematical
order:
>> b = nthroot(81,4)
>> c = exp(b)
>> d = log10(c)
>> e = log(2)
>> a = e + d
7. MATLAB will return 1.9960 as the final answer.
8. Of course, if you already get the hang of this method, you can write the entire
commands above into a single line:
>> a = log(2) + log10(exp(nthroot(81,4)))
ACTIVITY B: MATLAB ARITHMETICS
1. Write down the codes below and record your solution. Validate your answer using
your calculator.
>> a = 2, b = 4, c = 10
>> d = a + b, e = a – b, f = a * b
>> g = b / a, h = b \ c, i = a ^ c
>> j = a*pi
>> k = power(a, b), l = exp(c)
>> m = log(c), n = log2(c), o = log10(c)
>> p = sqrt(b), q = nthroot(b, a)
>> r = cos(pi/a), s = sin(pi/a), t = tan(pi/a)
ACTIVITY B: MATLAB ARITHMETICS
2. Based on Eq. (2) and Eq. (3), write the appropriate MATLAB command to solve
the mathematical equations:
−16+ 162 −4(1)(3)
𝑎= (2)
2(1)
𝜋
10 cos 90°+ 4
𝑧 = 4 (3)
ln 2 +log 𝑒 81

3. Validate your answer using your scientific calculator.


MATLAB APPLICATION
1. Let us look at the problem statement below:
When a current of 4.8 amperes is run through the coiled heating element of an electrical
kettle, the resistance of the element is 50 ohms. What is the voltage of the electrical kettle?

2. The first step is to solve the problem is to identify the suitable equation. In this
case, the equation required is undoubtedly the most well known among the
Electronic Engineering students, the Ohm’s Law as expressed in Eq. (3):
𝑉 = 𝐼𝑅 (3)
where:
𝑉 is Voltage
𝐼 is Current
𝑅 is Resistance
MATLAB APPLICATION
3. The second step is to determine suitable name to represents all the variables. In
this case:
• V will be for Voltage
• I will be for Current, and
• R will be for Resistance
4. The third step is to determine the input and output variables. Input variables are
variables which values are known, while output variables are which value need to
be find. Thus:
• 𝐼 and 𝑅 are input variables
• 𝑉 is the output variable
MATLAB APPLICATION
6. The fourth step is to write the command to represent the input variables and
assign the respective values. Therefore:
>> I = 4.8, R = 50
7. The last step is to write the command that represent the mathematical equation.
In this case, the Ohm’s law can expressed as follows:
>> V = I * R
8. You should obtain 240 V as the final answer.
MATLAB APPLICATION
9. Let us look at another electrical problem as shown below:
Given a formula that express the relationship of the electromagnetic force of a solenoid coil
as shown below:
𝑚𝑐 × 𝐴(𝑛 × 𝑖)2
𝐹=
2𝑙 2
where:
𝐹is Force,
𝑖 is Current,
𝑙 is Length of the gap between the solenoid and a piece of metal,
𝐴 is Area,
𝑛 is Number of turns,
𝑚𝑐 is the magnetic constant of 4𝜋x 10-7.

Find the electromagnetic force of the solenoid coil for the given parameters: 𝑖 = 10A, 𝑙 =
0.01m, 𝐴 = 0.25𝑚2 , and 𝑛 = 1000 turns.
MATLAB APPLICATION
10. Applying the same method as mentioned earlier:
Given a formula that express the relationship of the
Step 1: Identify suitable formula electromagnetic force of a solenoid coil as shown below:
Step 2: Determine suitable name for all variables 𝑚𝑐 × 𝐴(𝑛 × 𝑖)2
𝐹=
Step 3: Identify input and output variables 2𝑙 2
where:
Variable names: output variables 𝐹is Force,
• F is Force 𝑖 is Current,
• i is Current 𝑙 is Length of the gap between the solenoid and a piece of metal,
• l is Length input variables 𝐴 is Area,
• A is Area 𝑛 is Number of turns,
• n is Number of turns 𝑚𝑐 is the magnetic constant of 4𝜋x 10-7.
• mc is Magnetic constant
Step 4: Write commands for inputs with the values Find the electromagnetic force of the solenoid coil for the given
parameters: 𝑖 = 10A, 𝑙 = 0.01m, 𝐴 = 0.25𝑚2 , and 𝑛 = 1000
>> i = 10, l = 0.01, A = 0.25, n = 1000
turns.
>> mc = 4 * pi * power(10,-7)
Step 5: Write commands for the formula >> F = (mc * A * (n * i)^2)/(2 * l^2)
MATLAB APPLICATION
11. Most of the time, we want the user to input the input variables. This can be
achieve by using the input command.
12. For example, i = input(‘Please insert the current:‘) where the
Command Window will display Please insert the current and once
the user insert the value and press enter, the value will be stored in variable i.
13. Let us takes the previous problem and rewrite the commands to allow user to
insert the input variables:
>> i = input(‘Enter the current value: ‘)
>> l = input(‘Enter the length value: ‘)
>> A = input(‘Enter the Area value: ‘)
>> n = input(‘Enter the value of the Number of turns: ‘)
>> mc = 4 * pi * power(10,-7)
>> F = (mc * A * (n * i)^2) / (2 * l^2)
MATLAB APPLICATION
14. Figure 9 shows the expected outcome of the Command Window:

Fig 6. Expected outcome


ACTIVITY C: MATLAB APPLICATION
1. Given the following engineering problem, write suitable command to solve the
problem:
Based on the circuit connection shown in Figure A, 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 1 ms.

Figure A: RC Charging Circuit


ACTIVITY C: MATLAB APPLICATION
2. Following is the last problem for us to solve:

Write a program that accepts values of Resistor, 𝑟 and Voltage, 𝑉𝑠 from user. Based on the
values of 𝑟 and 𝑉𝑠 obtained, the program returns the value of Current, 𝑖 according to the
formula derives from Ohm’s Law.
SUMMARY
What we have learned?
• MATLAB environment: MATLAB layout, important short cuts,
and accessing Help Documentation
• Define, use, express constant and assign values to variables
Write and execute simple MATLAB commands and functions
for solving mathematical equation
• Write MATLAB commands for solving Ohm’s Law, and RC
circuit.
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