Ee 312 Mod 1
Ee 312 Mod 1
Ee 312 Mod 1
OVERVIEW
This module covers the concepts of numerical methods through the use of computer software tools particularly,
MATLAB application, to address engineering problems. Due to the highly mathematical nature of the
engineering field, we use analytical and numerical methods involving the writing of computer codes to solve
various types of engineering problems. This section helps incorporate certain programming methods to
successfully address problems such as differential equations and advanced engineering mathematics.
In this section, after being oriented with creating mathematical models, we will first introduce you with some
of the basic components of the MATLAB platform. This helps you to understand how to do programming in
the Command Window in a limited time and to compose and execute a file in MATLAB. This is followed by
the discussion of the basic building blocks in the construction of a computer program to solve mathematical
and engineering problems on the MATLAB platform.
LEARNING OUTCOMES
Develop mathematical models constructed on the basis of engineering concepts.
Integrate the basic MATLAB functions in developing mathematical models.
Write efficient code by creating plots programmatically using the graphics function in
MATLAB® platform.
LESSON
1
A mathematical model is a tool you can use to express real-world situations and solve problems or
analyse behaviour and predict future behaviour in real-world scenarios. In general, it can be represented in this
form:
Where the dependent variable (DV) is a feature that shows the state of the system; the independent
variable (IDV) are the measurements such as time and space in which the state of the system is identified; the
parameters are the properties of the system; and the forcing function is the external factor acting upon it.
EXAMPLE. In the physics world, Newton’s Second Law is one of the most important concept you need to
DO YOU master. This law tells us to calculate the acceleration of an object in a given net force:
STILL
REMEMBER? ∑𝐹
𝑎= (Eq. 1.2)
𝑚
Newton’s Second
Law of Motion Where F is the net force on the object, m is the mass of the body and a is its acceleration.
states that the
time rate of
change of Did you expect to see the formula of Newton’s Second
momentum of a Law as F=ma?
body is equal to Is it a YES or a NO? Why so?
the resultant
force acting on it.
LET’S DO SOME MATH!
You are given the task of estimating the speed of a bungee jumper as a function of time during the part of a
free-fall jump. Just imagine estimating the velocity of an apple falling into the ground, like how your physics
teacher used to ask.
Physics taught you that acceleration should be equal to the ratio of the force to the mass which is also known
as the Newton’s second law and if you still remember in fluid mechanics, you develop the following
mathematical model for the rate of change of velocity with respect to time;
1|P age
Numerical Met hods and Analysis | Module 1
DID YOU
KNOW?
Drag 𝑑𝑣 𝐶𝑑
coefficient is = 𝑔 − (𝑣 2 )
𝑑𝑡 𝑚
referred to as
“lumped” Where:
because its V downward vertical velocity (m/s)
magnitude is t time (s)
dependent on g acceleration due to gravity (≅ 9.81 m/s2)
factors such as m jumper’s mass (kg)
Cd lumped drag coefficient (kg/m)
jumper’s area
and the fluid Recall that calculus can be used to obtain a solution for v as a function of t.
density. However, in the following pages, you will be presented with an alternative
approach.
Let us use the Newton’s Second Law, (the more common form which is F=ma) to
identify the terminal velocity of our bungee jumper. By dividing both sides by the
mass:
Now, express F in terms of measurable variables. A body falling within the Earth’s proximity has net force
with two opposing forces:
F = FD + FU (Eq. 1.5)
Where,
FD = the downward pull of gravity
FU = the upward force of gravity
Assuming that air resistance is proportional to the square of the velocity (a good approximation in fluid
mechanics), we will have:
FU = – Cd v2 (Eq. 1.7)
Lumped drag coefficient (Cd) accounts for the properties of falling objects such as surface roughness, which
affects air resistance. Therefore,
Assume again that the bungee jumper is initially at rest (v=0 at t=0). Use calculus to solve the equation.
𝒈𝒎 𝒈𝑪𝒅
𝒗(𝒕) = √ 𝑪 𝐭𝐚𝐧𝐡 (√ 𝒕) (Eq. 1.9)
𝒅 𝒎
This is the format we used when using Newton's law in order to develop a balance of force for the bungee
jumper [Eq. (1.8)].
2|P age
Numerical Met hods and Analysis | Module 1
Eq. (1.11) represents one of the most significant ways where the laws of conservation are applied in engineering
and science — that is, predicting changes over time. Apart from calculating the change, a further way of
applying conservation laws is for instances where change is negligible. If change is zero, Eq. (1.11) becomes;
Therefore, when there is no change, the increases and decreases should be in balance. This scenario has many
functionalities in engineering and science.
mg = cd v2 (Eq. 1.13)
Thus, at steady state, the downward and upward forces are
in balance and Eq. (1.13) can be solved for the terminal
velocity:
MATLAB is a computer program that provides a convenient environment for the user to perform many
LESSON different types of calculations. It generates a very nice platform especially for implementing numerical
2 methods. In this section, you will be able to use this interactive or calculator mode to incorporate you to basic
functions such as computation and plot-making.
3|P age
Numerical Met hods and Analysis | Module 1
https://www.matlab.mathworks.com/
4|P age
Numerical Met hods and Analysis | Module 1
LET’S TRY!
Type…
>> 10 – 5 + (3 * 6)/ 2 ^ 4
2.2 ASSIGNMENT
Assignment refers to assigning values to variable names. This results in the storage of the values in the memory
location corresponding to the variable name.
2.2.1 Scalars
The assignment of values to scalar variables is similar to other computer languages. Type:
>> a = 4
Note how the assignment echo prints to display your results:
a=
4
Echo printing is a function of MATLAB. It can be terminated by the command line with the semicolon (;)
character. Try typing…
>> A = 6;
You can type several commands on the same line by separating them with commas or semicolons. If you
separate them with commas, they will be displayed, and if you use the semicolon, they will not. For example,
try typing…
>> a = 4, A = 6; x = 1;
MATLAB treats variable names in a case-sensitive manner—that is, the variable a is not the same as A. To
simulate, enter…
>> a
Then enter,
>> A
See how their values are distinct. They are distinct names.
We can assign complex values to variables, since MATLAB handles complex arithmetic automatically. The
unit imaginary number √−𝟏 is preassigned to the variable i. As a result, a complex value can be assigned
simply as in:
>> x = 2 + i*4
x=
2.0000 + 4.0000i
NOTE: MATLAB allows the symbol j to be used to represent the unit imaginary number for input. However,
it always uses an i for display. For example,
>> x = 2 + j*4
x=
2.0000 + 4.0000i
5|P age
Numerical Met hods and Analysis | Module 1
ans =
3.1416
Observe how MATLAB displays four decimal places. If you desire additional precision, enter the following:
>> format long
The following is a summary of the format commands you can use in engineering and scientific calculations.
They all have the syntax: format type.
TYPE RESULT
short Scaled fixed-point format with 5 digits
long Scaled fixed-point format with 15 digits for double and 7 digits for single
short e Floating point format with 5 digits
long e Floating point format with 15 digits for double and 7 digits for single
short eng Engineering format with atleast 5 digits and a power that is a multiple of 3
long eng Engineering format with exactly 16 significant digits and a power that is a
multiple of 3
Fixed dollars and cents
Brackets are used to enter arrays in the command mode. For example, a row vector can be assigned as follows:
>> a = [1 2 3 4 5]
a=
1 2 3 4 5
Row vectors are not often used to solve mathematical problems. When we speak of vectors, we routinely refer
to column vectors. A column vector can be entered in several ways. Try:
>> b = [2;4;6;8;10]
or
>> b = [2
4
6
8
10]
6|P age
Numerical Met hods and Analysis | Module 1
456
7 8 9]
Finally, we could construct the same matrix by concatenating or joining the vectors representing each column:
>> A = [[1 4 7]' [2 5 8]' [3 6 9]']
At any point in a session, a list of all current variables can be obtained by entering the who command:
>> who
Your variables are:
A a ans b x
NOTE: Subscript notation can be used to access an individual element of an array. For example, the fourth
element of the column vector b can be displayed as:
>> b(4)
ans =
8
For an array, A(m,n) selects the element in mth row and the nth column. For example,
>> A(2,3)
ans =
6
Some built-in functions can be used to create matrices. For example, the ones and zeros functions create vectors
or matrices filled with ones and zeros, respectively. To create a 2 × 3 matrix of zeros:
>> E = zeros(2,3)
E=
000
000
Similarly, the ones function can be used to create a row vector of ones:
>> u = ones(1,3)
u=
111
7|P age
Numerical Met hods and Analysis | Module 1
The linspace and logspace functions provide other handy tools to generate vectors of spaced points. The
linspace function generates a row vector of equally spaced points. It has the form:
linspace(x1, x2, n)
The logspace function generates a row vector that is logarithmically equally spaced. It has the form:
logspace(x1, x2, n)
which generates n logarithmically equally spaced points between decades 10x1 and 10x2. For example,
>> logspace(-1,2,4)
ans =
0.1000 1.0000 10.0000 100.0000
Aside from numbers, alphanumeric information or character strings can be represented by enclosing the strings
within single quotation marks. For example:
>> f = 'Miles ';
>> s = 'Davis';
Each character in a string is one element in an array. Thus, we can concatenate strings as:
>> x = [f s]
x=
Miles Davis
NOTE: Long lines can be continued by placing an ellipsis (three consecutive periods) at the end of the line to
be continued. For example, a row vector could be entered as:
>> a = [1 2 3 4 5 ...
6 7 8]
a=
1 2 3 4 5 6 7 8
However, you cannot use an ellipsis within single quotes to continue a string. To enter a string that extends
beyond a single line, piece together shorter strings as:
>> quote = ['Any fool can make a rule,' ...
' and any fool will mind it']
quote =
Any fool can make a rule, and any fool will mind it
8|P age
Numerical Met hods and Analysis | Module 1
ans =
0.5533
Calculations can also involve complex quantities. Here are some examples that use the values of x (2 + 4i) and
y (16) defined previously:
>> 3 * x
ans =
6.0000 + 12.0000i
>> 1 / x
ans =
0.1000 − 0.2000i
>> x ^ 2
ans =
−12.0000 + 16.0000i
>> x + y
ans =
18.0000 + 4.0000i
The real power of MATLAB is the capability to carry out vector-matrix calculations.
The inner product of two vectors (dot product) can be calculated using the * operator, for example:
>> a * b
ans =
110
and likewise, the outer product
>> b * a
ans =
2 4 6 8 10
4 8 12 16 20
6 12 18 24 30
8 16 24 32 40
10 20 30 40 50
Now, try
>> a * A
ans =
30 36 42
or
>> A * b
ans =
32
77
122
Matrices cannot be multiplied if the inner dimensions are unequal. Here is what happens when the dimensions
are not those required by the operations. Try:
>> A * a
MATLAB automatically displays the error message:
9|P age
Numerical Met hods and Analysis | Module 1
MATLAB will apply the simple arithmetic operators in vector-matrix if possible. You can also execute
calculations element by element in a matrix or vector. For example:
>> A^2
ans =
30 36 42
66 81 96
102 126 150
What if you want to square each element of A? That can be done with:
>> A.^2
ans =
1 4 9
16 25 36
DID YOU 49 64 81
KNOW?
Using the up- The period (.) preceding the ^ operator indicates that the operation is to be executed element by element. They
arrow key, you are also often referred to as element-by-element operations.
can go back to
any command that MATLAB accommodates a helpful shortcut for performing calculations that you’ve already done. Press the
you entered. Press up-arrow key. You can get back to the last line you typed in. Pressing Enter will perform the calculation
the up arrow until again. But you can also edit this line. For example, change it to the line below and then press Enter.
>> A.^3
you get back the
ans =
line. 1 8 27
64 125 216
343 512 729
Alternatively, you can type b and press the up-arrow once and it will automatically bring up the last command
beginning with the letter b. The up-arrow shortcut is a quick way to fix errors without having to retype the
entire line.
MATLAB and its Toolboxes have a collection of built-in functions. You can use online help to find out more
about them. You can go to: https://www.mathworks.com/help/matlab
For example, if you want to learn about the log function, type in:
>> help log
One of their important properties of MATLAB’s built-in functions is that they will operate directly on vector
and matrix quantities. For example, try:
>> log(A)
ans =
0 0.6931 1.0986
1.3863 1.6094 1.7918
1.9459 2.0794 2.1972
10 | P a g e
Numerical Met hods and Analysis | Module 1
Notice that the natural logarithm function is element by element to the Matrix A, in array style
There are also functions that perform special actions on the elements of matrices and arrays. For example, the
sum function returns the sum of the elements:
>> F = [3 5 4 6 1];
>> sum(F)
ans =
19
Likewise, with the following commands:
>> min(F), max(F),mean(F),prod(F),sort(F)
A frequent use of functions is to evaluate a formula for a series of arguments. Recall that the velocity of a free-
falling bungee jumper can be determined with [Eq. (1.9), Unit 1]
𝒈𝒎 𝒈𝑪𝒅
𝒗 ( 𝒕) = √ 𝐭𝐚𝐧𝐡 (√ 𝒕)
𝑪𝒅 𝒎
where 𝒗 is velocity (m/s), 𝒈 is the acceleration due to gravity (9.81 m/s2), 𝒎 is mass (kg), 𝑪𝒅 is the drag
coefficient (kg/m), and 𝒕 is time (s).
Check the number of items in the t array with the length function:
>> length(t)
ans = 11
Assign values to the parameters:
>> g = 9.81; m = 68.1; cd = 0.25;
MATLAB allows you to evaluate a formula such as 𝒗 = 𝒇(𝒕), where the formula is computed for each value
of the t array, and the result is set to a corresponding position in the 𝒗 array. For our case,
>> v = sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t)
v=
0
18.7292
33.1118
42.0762
46.9575
49.4214
50.6175
51.1871
51.4560
51.5823
51.6416
2.5 GRAPHICS
MATLAB allows graphs to be created quickly and conveniently. For example, to create a graph of the t and
v arrays from the data above, enter:
>> plot(t, v)
The graph appears in the graphics window and can be printed or transferred via the clipboard to other
programs.
11 | P a g e
Numerical Met hods and Analysis | Module 1
Table 2.2 Common Specifiers for Colors, Symbols, and Line Types
MATLAB can display more than one data set on the same plot. For example, another possible way to connect
each data marker with a straight line would be:
>> plot(t, v, t, v, 'o')
By default, previous plots are erased every time the plot command is implemented. Subplot, which allows you
to split the graph window into sub windows or panes using the syntax:
subplot( m , n , p )
This command breaks the graph window into an m-by-n matrix of small axes, and selects the p-th axes for the
current plot.
The simplest simulation of this capability is the plot3 command which has the syntax:
plot3( x , y , z )
Where x , y , and z are three vectors of the same length. The result is a line in three-dimensional space through
the points whose coordinates are the elements x, y, and z.
Let’s graph a circle with the two-dimensional plot function using the parametric representation: x = sin(t) and y
= cos(t).
>> t = 0:pi/50:10*pi;
>> subplot(1,2,1);plot(sin(t),cos(t))
>> axis square
>> title('(a)')
Note that the circle will be distorted if we had not used the axis
square command.
12 | P a g e
Numerical Met hods and Analysis | Module 1
KEY CONCEPTS/SUMMARY
We also discussed with you some of the basic components of the MATLAB platform:
Variable Assignment
Mathematical Operations
Built-in Functions
Graphical Functions
SUGGESTED READINGS
Ian H. Hutchinson, A Student’s Guide to Numerical Methods, Kindle Edition © Cambridge University Press
2015
Visit: https://www.mathworks.com/help/matlab
REFERENCES
Dr. Arti Kaushik, Numerical Methods for Engineers (with programs in MATLAB) 1st Ed. Kindle Edition
©2018
William Bober, MATLAB® Essentials A First Course for Engineers and Scientists © 2018 by Taylor &
Francis Group, LLC
https://www.mathworks.com/help/matlab
Hello human!
13 | P a g e
Numerical Met hods and Analysis | Module 1
CONCEPT CHECK
Create the program of the following items given. Screen capture all the files and send via
email. Write the program code in your journal. (10 points each)
A. Imagine you stand on a distance, say 10m away, watching someone throwing a ball
upwards. A straight line from you to the ball will then make an angle with the
horizontal that increases and decreases as the ball goes up and down. Let us consider
the ball at a particular moment in time, at which it has a height of 10m. If the ball is
x m away and y m up in the air, it makes an angle 𝜃 with the ground, where tan𝜃 =
𝑦/𝑥. What is the angle of the line then?
B. Say we are interested in y at every millisecond for the first second of the flight.
(Refer from the previous problem). This requires repeating the calculation of 𝑦 =
𝑣0 𝑡 − 0.5𝑔𝑡 2 one thousand times. If the ball’s initial velocity is 5m/s, create a
program that produces a plot of the vertical position with time.
Filename: ball_plot.m
C. Save a copy of the previous program and confirm that the copy runs as the original.
You are now supposed to introduce errors in the code, one by one. For each error
introduced, save and run the program, and comment how well Matlab’s response
corresponds to the actual error. When you are finished with one error, re-set the
program to correct behaviour (and check that it works!) before moving on to the
next error.
I. Insert the word hello on the empty line above the assignment to v0.
II. Remove the % sign in front of the comment initial velocity.
III. Remove the = sign in the assignment to v0.
IV. Change the symbol ^ into **.
V. Change the calculation of y to y = v0*t.
VI. Write x on the line just above where y is calculated.
VII. Change the statement y = v0*t - 0.5*g*t^2 into y = v0*t - 0.5*g*t^2; That
is, insert a semicolon at the end.
Filename: testing_ball.m
D. Write a program that computes the volume V of a cube with sides of length L D
4cm and prints the result to the screen. Both V and L should be defined as separate
variables in the program. Run the program and confirm that the correct result is
printed.
Filename: cube_volume.m
E. Write a program that computes both the circumference C and the area A of a circle
with radius r = 2cm. Let the results be printed to the screen on a single line with an
appropriate text. The variables C, A and r should all be defined as separate variables
in the program. Run the program and confirm that the correct results are printed.
Filename: circumference_and_area.m
14 | P a g e