You are on page 1of 22

Republic of the Philippines

PALOMPON INSTITUTE OF TECHNOLOGY


Palompon, Leyte
COLLEGE OF TECHNOLOGY AND ENGINEERING
Mechanical Engineering Department

COMPUTER APPLICATION FOR ME


(MATLAB)
Lecture I: Topic Outline
• Desktop Basics
• Common Commands
• Matrices and Arrays
• Workspace Variables
• Character String
• Solving System of Equations
Solving Systems of Equation
Solving Systems of Equation
Solving Systems of Equation
Solving Systems of Equation
Solving Systems of Equation using MATLAB
MATLAB Sample: Using inv: Using linsolve:
4x − 2y = 20
−5x − 5y = −10
Solving Systems of Equation using MATLAB
MATLAB Sample: Using inv: Using linsolve:
x+y+z=2
6x + 4y + 5z = 31
5x + 2y + 2z = 13
Solving Systems of Equation using MATLAB
Sample Exercise:
Solve these systems of linear equations using solvers: linsolve or inv
1. 2. 3.

2x + 3y − z = 12 3x − 9z = 33 2x + 5y + 2z = −38
3x − 2y + 2z = −5 7x − 4y − z = 15 3x − 2y + 4z = 17
x + 4y − 3z = 7 4x + 6y + 5z = −6 −6x + y − 7z = −12
Finding Solutions using MATLAB
Where: a, b, c, d = coefficients
𝑛 𝑛−1 𝑛−2 𝑛−𝑛 z = constant
a𝑥 + b𝑥 + c𝑥 … d𝑥 =z
x = unknown variable

𝑀𝑎𝑡ℎ𝑒𝑚𝑎𝑡𝑖𝑐𝑎𝑙 𝑂𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛𝑠 𝑈𝑠𝑒𝑑: Sample Solution:


Roots of Equation 𝑥 2 − 10x + 25 = 0
1. Translate into a row vector 𝑥−5 𝑥−5 = 0
matrix:
2. Use the solver “roots(x)” 𝑥=5
Using MATLAB:
𝐸𝑞1 = [1 − 10 25]
𝑅 = roots(Eq1)
Finding Solutions using MATLAB
Sample Exercise:
Solve for the solutions using roots of equation in MATLAB:
1. 2. 3.

2𝑥 2 + 3x − 9 = 0 𝑥 2 − 18x + 81 = 0 𝑥 2 − 6x − 16 = 0
Plotting graphs in MATLAB
1. Create a vector for the independent variable.
2. Assign a function.
3. Plot the function using “plot(x,y)” command.

In order to create a plot, their row vectors must be the same.

Using MATLAB:

𝑥 = [0: 1: 10]
𝑓 𝑥 = sin(𝑥)
𝑝𝑙𝑜𝑡 (𝑥, 𝑦)
Plotting graphs in MATLAB
Sample Exercise:

𝑥 = [0: 0.1∗ 𝑝𝑖: 10]

y= 𝑐𝑜𝑠(𝑥)
Writing a function on MATLAB
1. Prepare/solve for the mathematical equation.
2. Click on New Script at the command window.
3. Write the code function [output var.] = function_name(input var.)
4. Enter, write the equation with the respective output and input variables, with semicolon at the end.
5. Enter, end.
6. Save the function as a file using the current folder location.
7. Work with the function and find the solution.

Example Using MATLAB:

Cylinder Volume Function,


Function of h & r ℎ
𝑉= 𝜋𝑟 2
3
Writing a function on MATLAB
1. Prepare/solve for the mathematical
equation.
2. Click on New Script at the command
window.
3. Write the code function [output var.] =
function_name(input var.)
4. Enter, write the equation with the
respective output and input variables, with
semicolon at the end.
5. Enter, end.
6. Save the function as a file using the current
folder location.
7. Work with the function and find the
solution.
Writing a function on MATLAB
1. Define a mathematical equation of a cone.
Let r=radius, V=volume, h=height. ℎ
𝑉 = 𝜋𝑟 2
3
After defining code for the mathematical function, find V if radius is 8.
Also, express the variables as a comment before writing the code and express the
results as comment afterwards.

2. Define a mathematical equation of a sphere. 4 3


𝑉= 𝜋𝑟
Let r=radius, V=volume, SA=surface area. 3
After defining code for the mathematical function, find SA and V if radius is 10. 𝑆𝐴 = 4𝜋𝑟 2
Also, express the variables as a comment before writing the code and express the
results as comment afterwards.
MIDTERM
Solve the following engineering problems by creating functions.

Filename & variable designation:


A triangle has sides of length x, y, and z. Find its area
if the sides are 195, 157 and 210, respectively. Heron’s Average area of triangle x, y and z
s A x,y,z
𝑥+𝑦+𝑧
𝑠= 𝐴= 𝑠(𝑠 − 𝑥)(𝑠 − 𝑦)(𝑠 − 𝑧) herons_average area_triangle ---
2

A ball is dropped from a height of 60 meters above Time of fall gravity x, y and z
ground. How long does it take to hit the ground?
t ---- x,y,z
1 2 time ---- ---
ℎ = 𝑔𝑡
2

Note: Don’t forget to create and use assigned Function folder in the MATLAB folder
as a current folder. For the square root command, use sqrt (X).
Differentiation and Integration in MATLAB
Differentiation Integration
1. Assign a variable using 1. Assign a variable
“syms” command using “syms”
2. Write the equation of command
the function of the 2. Write the equation
assigned command of the function of
3. Write and assign the the assigned
derivative of the command
function using “diff” 3. Write and assign the
command. integral of the
function using “int”
command.
Differentiation and Integration in MATLAB
Sample Problem
Plot the curve of the displacement, velocity
and acceleration.
Integration in MATLAB
Sample Problem
A force 𝐹 in Newtons is applied to move an object along a straight line from 𝑥 = 2 meters to 𝑥 = 5
meters. The force at any point x meters is given by 𝐹(𝑥) = 2𝑥 + 1 Newtons. Find the work done in
moving the object from 𝑥 = 2 𝑡𝑜 𝑥 = 5.
𝐹(𝑥) = 2𝑥 + 1

0 1 2 3 4 5
Integration in MATLAB
Sample Problem
A force 𝐹 in Newtons is applied to move an object along a straight line from 𝑥 = 2 meters to 𝑥 = 5
meters. The force at any point x meters is given by 𝐹(𝑥) = 2𝑥 + 1 Newtons. Find the work done in
moving the object from 𝑥 = 2 𝑡𝑜 𝑥 = 5.
𝐹(𝑥) = 2𝑥 + 1 𝑏
𝑊 = න 𝐹 𝑥 𝑑𝑥
𝑎

𝑎=2 𝑏=5
5
𝑊 = න 2𝑥 + 1 𝑑𝑥
2
0 1 2 3 4 5 5
𝑊 = 𝑥2 + 𝑥 2

𝑊 = 24 𝐽𝑜𝑢𝑙𝑒𝑠
Integration in MATLAB
Sample Problem
A force 𝐹 in Newtons is
applied to move an object along
a straight line from 𝑥 = 2
meters to 𝑥 = 5 meters. The
force at any point x meters is
given by 𝐹(𝑥) = 2𝑥 +
1 Newtons. Find the work done
in moving the object from 𝑥 =
2 𝑡𝑜 𝑥 = 5.
Final Exam (Activity)
Solve the following using MATLAB.
1. A particle is moving along a straight line, and the force acting on it is given by 𝐹(𝑥) = 3𝑥^2 for 1 ≤ 𝑥 ≤ 2.
Find the work done in moving the particle from 𝑥 = 1 to 𝑥 = 2.
2. The rate at which rain is falling into a container is given by the function 𝑅′(𝑡) = 0.2𝑡 − 0.1, where 𝑅(𝑡)
represents the total amount of rainfall accumulated up to time t in hours. Find the total amount of rainfall
accumulated after 4 hours.
3. The rate at which a substance is cooling is given by the function 𝑇′(𝑡) = −0.02𝑡 + 0.3, where 𝑇(𝑡)
represents the temperature of the substance at time 𝑡 in minutes. Find the total change in temperature
after 10 minutes.
4. A tank initially contains 500 liters of water. The rate at which water is draining from the tank is given by
𝑉′(𝑡) = −2𝑡 liters per minute, where 𝑉(𝑡) is the volume of water in the tank at time 𝑡 in minutes. Find the
volume of water in the tank after 3 minutes.

You might also like