Biomedical Engineering Reference
In-Depth Information
A function is a sequence of instructions like a script,
but the function may accept input arguments and return
output arguments. The function may also have MATLAB
variables that can only be accessed inside the function.
Like all block-structured programming tools, sub-
routines and functions each have only one control flow
entry point and one exit.
while ~isempty (F)
C ¼ Far2Cel (F);
display(C);
F ¼ input ('The value of the temperature
in degree Fahrenheit ¼ 0 );
end
which is called from the MATLAB command line. In the
sample execution below, note that the execution stops
when there is no numeric input.
>> F2C
The value of the temperature in degree
Fahrenheit ¼ 32
C ¼ 0
The value of the temperature in degree
Fahrenheit ¼ 100
C ¼ 37.7778
The value of the temperature in degree
Fahrenheit ¼ 212
C ¼ 100
The value of the temperature in degree
Fahrenheit ¼ 77
C ¼ 25
The value of the temperature is in degree
Fahrenheit. ¼
Example 2.1b.6 Using scripts and functions.
Write a script that asks for a temperature (in degrees
Fahrenheit) and calls a function to compute the equiva-
lent temperature in degrees Celsius. The script should
keep running until no number is provided to convert
(Note: The command isempty will be useful here).
Solution
The solution to this problem has two parts. The first is
the function that converts a temperature in Fahrenheit
into its equivalent temperature in Celsius. The second
part of the solution is the script that calls this function.
The best strategy is to write and test the function first,
and only after it is determined to be correct, write the
script that calls the function.
The MATLAB function needs to compute the tem-
perature in Celsius using the following formula:
C ¼ 5
9 ðF 32 Þ
2.1b.4 Fundamentals of data
structures for MATLAB
The temperature in Fahrenheit is the argument to the
function, which returns the temperature in degrees
Celsius. The MATLAB function is:
MATLAB programs have two parts: a sequence of in-
structions for the computation to be performed and the
representation of the data that are being operated on.
The previous section is an overview of the basic pro-
gramming language constructs that underlie all MATLAB
programs. This section is an overview of the techniques
available in MATLAB for representing data.
Although the emphasis of this text is on numerical
methods and quantitative results, there is still the need
to include other information, such as names of variables,
the units of the results, and information on the in-
terpretation. The format of the numerical output is im-
portant as well.
All modern programming languages include a facility
for creating and using different data types. There are
fundamental data types and mechanisms for forming
aggregates of the data. This section is an overview of the
six fundamental data types in MATLAB: double, char,
sparse, uint8, cell, and struct.
% Far2Cel.m
function C ¼ Far2Cel (F)
C ¼ (5/9) * (F-32)
end
which should be tested before going on to complete the
exercise. The script that uses this function should take as
input from the keyboard a temperature in degrees
Fahrenheit, display the temperature converted to de-
grees Celsius and then wait for the next input.
The description should suggest that there is a while
loop, not a for loop in this script since the number of
iterations is uncertain and cannot be computed a priori.
First, get the keyboard input of temperature in Fahr-
enheit and check to see if the input is empty. If the input
is empty, stop execution; otherwise, convert the tem-
perature to Celsius and then get another input temper-
ature and once again check to see if the input is empty.
Iterate until there is no more keyboard input. The script
F2C.m is:
% F2C.m
F ¼ input ('The value of the temperature
in degree Fahrenheit ¼ ');
2.1b.4.1 Number representation
In calculus and courses on differential equations as well,
the real numbers were used, whether this was made
Search WWH ::




Custom Search