Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ee 312 Mod 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Numerical Met hods and Analysis | Module 1

Numerical Methods and Problem solving


with MATLAB

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.

1.1 MATHEMATICAL MODEL

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:

DV = f (IDV, Parameters, Forcing function) (Eq. 1.1)

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:

a=F/m (Eq. 1.3) FIGURE 1.1


Forces acting
Then express the acceleration (a) as the time rate of change of the velocity; on a free-falling
bungee jumper.
dv / dt = F / m
(Eq. 1.4)

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

Also, the second law can be used to formulate force as;


FD = mg, where g is the acceleration due to gravity (Eq. 1.6)

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,

dv / dt = g – (Cd / m)v2 (Eq. 1.8)

Assume again that the bungee jumper is initially at rest (v=0 at t=0). Use calculus to solve the equation.
𝒈𝒎 𝒈𝑪𝒅
𝒗(𝒕) = √ 𝑪 𝐭𝐚𝐧𝐡 (√ 𝒕) (Eq. 1.9)
𝒅 𝒎

Where tanh as elementary exponential function:


𝒆𝒙 −𝒆−𝒙
𝐭𝐚𝐧𝐡 𝒙 = (Eq. 1.10)
𝒆𝒙 +𝒆−𝒙

1.2 CONSERVATION LAWS IN ENGINEERING AND SCIENCE


Apart from Newton's second law, science and engineering have other major guiding concepts. The
Conservation laws are among the most important among these.

Change = increases − decreases (Eq. 1.11)

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;

Increases = decreases (Eq. 1.12)

Therefore, when there is no change, the increases and decreases should be in balance. This scenario has many
functionalities in engineering and science.

For example, for steady-state incompressible fluid flow in


pipes, the flow into a junction must be balanced by flow
going out. The junction in Fig. 1.2, the balance that can be
used to compute that the flow out of the Pipe 4 must be
60.

For the bungee jumper, the steady-state condition would


correspond to the case where the net force was zero or
(Eq.1.8) with dv/dt = 0,

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:

OH NO! FIGURE 1.2


𝒈𝒎 A flow balance for steady incompressible fluid flow at
𝒗= √
Did you find it 𝑪𝒅 the junction of pipes.
difficult to While Equations (1.11) and (1.12) may likely to be
answer the relatively easy; they represent the two significant ways in
question? which the laws of conservation are applied in engineering and science. Such that they will form an essential
Which part
part of our initiatives to demonstrate the relation between numerical methods and engineering and science in
was unclear to
next modules.
you?
Electrical engineering experiments use current as well as energy balance to simulate electrical circuits. The
Tell me about balance of current from conversation of charge is conceptually similar to the flow balance shown in Fig. 1.2.
it. As the flow at the junction of pipes must be balanced, electrical current at the junction of electrical wires must
balance.

CONCEPT CHECK (A)


A bungee jumper with a mass of 68.1 kg fly from a hot air balloon.
Use the previous equations to compute velocity for the first 12s of free
fall. Also, determine the terminal velocity that will be attained for an
infinitely long cord. Use a drag coefficient of 0.25 kg/m. NOTE: After
a sufficiently long time, a constant velocity, called 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.

2.1 THE MATLAB ENVIRONMENT

MATLAB is a multi-paradigm numerical computing environment and MathWorks developed proprietary


programming language. MATLAB allows to manipulate matrixes, to plot functions and data, to implement
algorithms, to create user interfaces and to interface with high - level programming languages.

MATLAB uses three primary windows:


 Command window – Used to enter commands and data.
 Graphics window – Used to display plots and graphs.
 Edit window – Used to create and edit M-files.

3|P age
Numerical Met hods and Analysis | Module 1

LABORATORY EXERCISE M1 – P1.1

Using your smart phone, go to your App Store and


download the ‘MATLAB mobile’ application.

After installing, open the app and create an account.

The application is available in Google Play Store and


Apple Store for free.

You can also use the browser/desktop site and go to:

https://www.matlab.mathworks.com/

Make sure to use an active email address.

After filling up this form, verify first your email before


you click the CONTINUE button.

When the system asks if you have your MATLAB


License, just answer NO. The application is free.

Finish your profile with a strong password.

After logging in, your will proceed to


COMMAND WINDOW.

4|P age
Numerical Met hods and Analysis | Module 1

LET’S TRY!
Type…

>> 10 – 5 + (3 * 6)/ 2 ^ 4

>> ans = ______________

To enter the EDIT WINDOW:


In the upper left side corner, click the menu button
and choose FILES.

Next, on the upper right side corner, click the plus


sign (+) the choose NEW FILE. Name your file as
‘test’ then click CREATE.

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

There are several predefined variables like pi.


>> pi

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

Now when pi is entered the result is displayed to 15 significant figures:


>> pi
ans =
3.14159265358979

To return to the four decimal version, type:


>> format short

The following is a summary of the format commands you can use in engineering and scientific calculations.
They all have the syntax: format type.

Table 2.1 Format Commands for Type Function

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

2.2.2 Arrays, Vectors, and Matrices


An array is a set of values identified by a single name for the variable. One-dimensional arrays are called
vectors, and two-dimensional arrays are called matrices.

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]

or, by transposing a row vector with the ' operator,


>> b = [2 4 6 8 10]'
The result in all three cases will be:
b=
2
4
6
8
10
A matrix of values can be assigned as follows:
>> A = [1 2 3; 4 5 6; 7 8 9]
A=
123
456
789
In addition, the Enter key (carriage return) can be used to separate the rows. For example, the Enter key would
be struck after the 3, the 6, and the ] to assign the matrix:
>> A = [1 2 3

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

or, with more detail, enter the whos command:


>> whos

Name Size Bytes Class


A 3x3 72 double array
a 1x5 40 double array
ans 1x1 8 double array
b 5x1 40 double array
x 1x1 16 double array (complex)
Grand total is 21 elements using 176 bytes

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

2.2.3 Colon Operators


Colon operator is a powerful tool for creating and manipulating arrays. If a colon is used to separate two
numbers, MATLAB generates the numbers between them using an increment of one:
>> t = 1:5
t= 1 2 3 4 5
If colons are used to separate three numbers, MATLAB generates the numbers between the first and third
numbers using an increment equal to the second number:
>> t = 1:0.5:3
t=
1.0000 1.5000 2.0000 2.5000 3.0000

Note that negative increments can also be used


>> t = 10:−1:5
t= 10 9 8 7 6 5
Aside from creating series of numbers, the colon can also be used as a wildcard to select the individual rows
and columns of a matrix. When a colon is used in place of a specific subscript, the colon represents the entire
row or column. For example, the second row of the matrix A can be selected as:
>> A(2,:)
ans = 4 5 6
Also, the colon notation can be used to selectively take out a series of elements from within an array. For
example, based on the previous definition of the vector t:
>> t(2:4)
ans = 9 8 7
Thus, the second through the fourth elements are returned.

7|P age
Numerical Met hods and Analysis | Module 1

2.2.4 linspace and logspace Functions

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)

which generates n points between x1 and x2. For example;


>> linspace(0,1,6)
ans =
0 0.2000 0 .4000 0.6000 0.8000 1.0000
If the n is omitted, the function automatically generates 100 points.

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

If n is omitted, it automatically generates 50 points.

2.2.5 Character Strings

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

2.3 MATHEMATICAL OPERATIONS


Operations with scalar quantities are handled in a straightforward manner, similar to other computer languages.
The common operators, in order of priority, are:
^ Exponentiation
− Negation
*/ Multiplication and division
\ Left division
+− Addition and subtraction

These operators will work in calculator fashion. Try:


>> 2*pi
ans =
6.2832

Also, scalar real variables can be included:


>> y = pi/4;
>> y ^ 2.45

8|P age
Numerical Met hods and Analysis | Module 1

ans =
0.5533

Results of calculations can be assign to a variable or simply displayed.


As with other computer calculation, the priority order can be overridden with parentheses. For example,
because exponentiation has higher priority than negation, the following result would be obtained:
>> y = −4 ^ 2
y= −16
Thus, 4 is first squared and then negated. Parentheses can be used to override the priorities as in
>> y = (−4) ^ 2
y=
16
Within each precedence level, operators have equal precedence and are evaluated from left to right. As an
example:
>> 4^2^3
>> 4^2^3)
>> (4^2^3

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

To further demonstrate vector-matrix multiplication, first redefine a and b:


>> a = [1 2 3];
and
>> b = [4 5 6]';

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

??? Error using ==> mtimes

Matrix-matrix multiplication is carried out in the same way:


>> A * A
ans =
30 36 42
66 81 96
102 126 150

Mixed operations with scalars are also possible:


>> A/pi
ans =
0.3183 0.6366 0.9549
1.2732 1.5915 1.9099
2.2282 2.5465 2.8648

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.

2.4 USE OF BUILT-IN FUNCTIONS

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

For a list of all the elementary functions, type:


>> help elfun

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).

To create a column vector t that has values from 0 to 20 in steps of 2:


>> t = [0:2:20]'
t=
0
2
4
6
8
10
12
14
16
18
20

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.

Modify the graph with commands such as the following:

11 | P a g e
Numerical Met hods and Analysis | Module 1

>> title('Plot of v versus t')


>> xlabel('Values of t')
>> ylabel('Values of v')
>> grid

The plot command displays a solid thin line by default.


In order to plot each point with a symbol, include
a specifier bound in single quotes in the plot
function.

Table 2.2 Common Specifiers for Colors, Symbols, and Line Types

COLORS SYMBOLS LINE TYPE


Blue (b) Point . Solid -
Green (g) Circle o Dotted :
Red (r) X-mark x Dash dot -.
Cyan (c) Plus + Dashed --
Magenta (m) Star *
Yellow (y) Square s
Black (k) Diamond d
White (w) Triangle down v
Triangle up ^
Triangle left/right < or >

For example, if you want to use open circles enter


>> plot(t, v, 'o')

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

Presented in this module is the general form of creating mathematical models:


 Dependent Var = f (Independent Var, Parameters, Forcing function)

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

Svein Linge, Programming for Computations - MATLAB/Octave: A Gentle Introduction to Numerical


Simulations with MATLAB/Octave (Texts in Computational Science and Engineering) Springer. Kindle
Edition. Copyright © 2016

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!

Do you prefer using the MATLAB mobile application


while performing the laboratory exercises or would you
like to try using the desktop platform?

I would love to know why.

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?

Create a Matlab program for doing these calculations. Filename: ball_angle.m

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

You might also like