CHE 306 Lesson Note 3
CHE 306 Lesson Note 3
objective of this
LESSON 3: section is to
learn how to
MATLAB write M-file
programs to
PROGRAMMING implement
numerical
methods.
M-files
Input-Output
Structured Programming
CONTENTS Nesting and Indentation
Passing Functions to M-
files
Case Study: Bungee
Jumper Velocity
The most common way to operate
MATLAB is by entering commands
one at a time in the command
window.
M-files provide an alternative way
of performing operations that
M-FILES greatly expand MATLAB’s
problem-solving capabilities.
An M-file consists of a series of
statements that can be run all at
once. Note that the nomenclature
“M-file” comes from the fact that
such files are stored with a .m
A script file is merely a series of
MATLAB commands that are
saved on a file.
They are useful for retaining a
M-FILES
series of commands that you
want to execute on more than
(A) SCRIPT one occasion.
FILES The script can be executed by
typing the file name in the
command window or by invoking
the menu selections in the edit
window: Debug, Run.
Example 3.1: Develop a script
file to compute the velocity of the
free-falling bungee jumper for the
case where the initial velocity is
zero.
M-FILES
Solution: Open the editor with
the menu selection: File, New, M-
(A) SCRIPT file. Type in the following
FILES statements to compute the
velocity of the free-falling bungee
jumper at a specific time [recall
Eq. (1.9)]:
g = 9.81; m = 68.1; t = 12; cd = 0.25;
v = sqrt(g * m / cd) * tanh(sqrt(g * cd / m)
>>scriptdemo
The result will be displayed as
v =
50.6175
M-FILES Thus, the script executes just as
if you had typed each of its lines
(A) SCRIPT in the command window.
FILES As a final step, determine the
value of g by typing
>> g
g =
9.8100
So you can see that even though
g was defined within the script, it
retains its value back in the
command workspace.
M-FILES As we will see in the following
section, this is an important
(A) SCRIPT distinction between scripts and
functions.
FILES
Function files are M-files that
start with the word function.
In contrast to script files, they
can accept input arguments and
M-FILES return outputs.
Hence, they are analogous to
(B) FUNCTION user-defined functions in
FILES programming languages such as
Fortran, Visual Basic or C.
The syntax for the function file
can be represented generally as
function outvar = funcname(arglist)
% helpcomments
statements
outvar = value;
S
As in the previous Section,
information is passed into the
function via the argument list and
is output via the function’s name.
Two other functions provide ways
3.2 to enter and display information
directly using the command
INPUT-OUTPUT
window.
The input Function
This function allows you to prompt
the user for values directly from the
command window. Its syntax is:
n = input('promptstring')
The function displays the
promptstring, waits for keyboard
input, and then returns the value
from the keyboard.
For example,
m = input('Mass (kg): ')
3.2
When this line is executed, the
INPUT-OUTPUT
user is prompted with the
message
Mass (kg):
3.3.1
DECISIONS
The if...else Structure.
This structure allows you to
execute a set of statements if a
logical condition is true and to
3.3.1 execute a second set if the
condition is false.
DECISIONS
Its general syntax is
if condition
statements1
else
statements2
end
The if...elseif Structure.
It often happens that the false
option of an if...else structure is
another decision.
3.3.1 This type of structure often
DECISIONS occurs when we have more than
two options for a particular
problem setting.
For such cases, a special form of
decision structure, the if...elseif
has been developed.
if condition1
statements1
elseif condition2
statements2
elseif condition3
3.3.1 statements3
DECISIONS . . .
else
statements
else
end
The switch Structure.
The switch structure is similar in
spirit to the if...elseif structure.
However, rather than testing
individual conditions, the
3.3.1 branching is based on the value
DECISIONS of a single test expression.
Depending on its value, different
blocks of code are implemented.
In addition, an optional block is
implemented if the expression
takes on none of the prescribed
switch testexpression
case value1
statements1
case value2
statements2
. . .
3.3.1 otherwise
statements
DECISIONS otherwise
end
>> factor(5)
ans =
120
This loop will execute 5 times
(from 1 to 5).
At the end of the process, x will
hold a value of 5! (meaning 5
factorial or 1 × 2 × 3 × 4 × 5 =
3.3.2 120).
Notice what happens if n = 0.
LOOPS For this case, the for loop would
not execute, and we would get
the desired result, 0! = 1.
Vectorization.
The for loop is easy to implement
and understand.
However, for MATLAB, it is not
necessarily the most efficient means
3.3.2 to repeat statements a specific
number of times.
LOOPS Because of MATLAB’s ability to
operate directly on arrays,
vectorization provides a much more
efficient option.
For example, the following for loop
structure:
i = 0;
for t = 0:0.02:50
i = i + 1;
y(i) = cos(t);
end