Matlab Programming f001, Informatyka, Metody numeryczne - Matlab

[ Pobierz całość w formacie PDF ]
Division of Engineering Fundamentals , Copyright 1999 by J.C. Malzahn Kampe 1 / 21
Virginia Polytechnic Institute & State University EF1015 Revised Fall 2000
_____________________________________________________
MATLAB Programming
When we use the phrase “computer solution,” it should be understood that a computer
will only follow directions; the “solution” part of that phrase still resides with the person
directing the machine. Developing an algorithm to solve a problem is the first step in a
“computer solution.” The computer must then be given every procedural step, in the
proper order, to accomplish the task that we require of it. With that in mind, an
appreciation of how explicit we must be with our directions is helpful. Consider a young,
barefoot child. If you tell this child to “put on the shoes” that you have set out, the child
is very likely to come back to you with the shoes on, carrying socks in hand. In
retrospect, the child has accomplished the task you had put before him. Had you also
wanted socks on his feet, surely you would have told him.
In an effort to understand the three aspects of programming that we will cover
(sequential, selection or decision, and repetition) it is useful to have an example to
follow. In the following sections, we will consider these four problem statements.
1.
Prepare a flowchart and MATLAB program that will calculate the area and
circumference of a circle, allowing the radius to be an input variable, and output
radius, area, and circumference.
2.
Prepare a flowchart and MATLAB program that will calculate the area and
circumference of a circle, allowing the radius to be an input variable, and output
radius, area, and circumference only if the area of the circle is greater than 20.
3.
Prepare a flowchart and MATLAB program that will calculate the area and
circumference of ten circles, allowing radius to be an input variable, and output
radius, area, and circumference only if the area of the circle is greater than 20. The
number of circles that do not have an area greater than 20 should also be output.
4.
Prepare a flowchart and MATLAB program that will calculate the area and
circumference of any number of circles, allowing radius to be an input variable, and
output radius, area, and circumference only if the area of the circle is greater than 20.
The number of circles examined that do not have an area greater than 20 should also
be output.
Problem 1: Sequential
The solution algorithm for Problem 1 involves a
sequence
of procedures that follows a
single logic flow path from “START” to “STOP”. The flowchart and program are shown
in Figure 1.
Division of Engineering Fundamentals , Copyright 1999 by J.C. Malzahn Kampe 2 / 21
Virginia Polytechnic Institute & State University EF1015 Revised Fall 2000
_____________________________________________________
START
% This program will calculate the
% area and circumference of a circle,
% allowing the radius as an input.
INPUT
R
R = input('Please enter the radius: ');
AREA = pi * R^2;
AREA = p R
2
CIRC = 2.0 * pi * R;
fprintf('\n Radius = %f units',R)
fprintf('\n Area = %f square units', AREA)
fprintf('\n Circumference = %f units\n', CIRC)
CIRC = 2.0 p R
OUTPUT
R, AREA, CIRC
STOP
Figure 1. A MATLAB program (a script m-file named Prob1) and the corresponding
flowchart for Problem 1.
________________________________________________________________________
At this point you may have many questions about how we get to the contents of Figure 1,
so let’s go through that first.
After reading Problem 1 above, we must first develop an approach (algorithm) to obtain
what the problem statement requires. Our solution algorithm is documented with the
flowchart that appears in Figure 1. The flowchart is produced using the drawing features
available in MS Word under “Flowchart” in the “AutoShapes” menu of the lower toolbar.
Next we translate the flowcharted algorithm to a computer program (code) that
MATLAB can interpret and execute. This program appears on the left in Figure 1. In
order to produce this code, we open MATLAB, and go to the “File” menu in the upper
toolbar, and select “New.” Of the options that come up, we then select “M-File;” this
opens the MATLAB “Editor/Debugger” window. Once this window is open, we can
type in our program, edit the program, and save it to our “Work” folder.
Now take a look at the correspondence between the flowchart and the code in Figure 1.
The first thing to note is that there are no MATLAB commands in the code to “START”
or “STOP” the program. The script file begins with statements that have been
commented out
of the executable statements by placing “%” in the first position on those
lines. The “%” tells MATLAB that what follows on that line can be ignored during
program run. We use the comment lines to describe what the program does. Also, if, in
the command window, we type
Division of Engineering Fundamentals , Copyright 1999 by J.C. Malzahn Kampe 3 / 21
Virginia Polytechnic Institute & State University EF1015 Revised Fall 2000
_____________________________________________________
» help Prob1
area and circumference of a circle,
allowing the radius as an input.
we see that MATLAB responds with those comments that begin the script file named
Prob1.
Below the comments, the first executable statement in the file identifies “R” as an input
variable. In the statement
R = input('Please enter the radius: ');
we tell MATLAB to print “Please enter the radius:” to the screen as a prompt to the user
that the radius value must be entered. MATLAB accepts the entered value, and stores it
under the variable named “R.” The “;” at the end of the statement suppresses the
MATLAB echo of the value entered. This
input
value assignment statement corresponds
to the “INPUT” symbol in the flowchart.
Beneath the input statement of the code are two value assignment statements.
AREA = pi * R^2;
CIRC = 2.0 * pi * R;
These correspond to the two process symbols in the flowchart; they use the value stored
as “R” in calculations and assign the calculation results to “AREA” and “CIRC.” It is
important to remember the form that value assignment statements require. Only a single
variable with no operators may appear on the left of the equal sign, and all variables on
the right of the equal sign must have a current assigned value.
Looking at the flowchart, our last order of business is to output the values for “R,”
“AREA,” and “CIRC.” In the code shown in Figure 1, we use
fprintf
statements to
accomplish this.
fprintf('\n Radius = %f units',R)
fprintf('\n Area = %f square units', AREA)
fprintf('\n Circumference = %f units\n', CIRC)
Using these statements allows us to format our output. In other words, these commands
let us tell MATLAB exactly how we want our values for “R,” “AREA,” and “CIRC”
printed to the screen. Within the parentheses of the first
fprintf
statement, the “\n” is a
carriage return command that tells MATLAB to start printing on a new line. MATLAB
interprets what follows in that statement as a character string that contains a format field
for a number (the “%f”), and that the number to be placed at the position designated by
“%f” is stored under the variable named “R.” Note that the carriage return, the text, and
This program will calculate the
Division of Engineering Fundamentals , Copyright 1999 by J.C. Malzahn Kampe 4 / 21
Virginia Polytechnic Institute & State University EF1015 Revised Fall 2000
_____________________________________________________
the format field are enclosed within single quote marks, and the variable name (which
stores the value) specified to fill the number format field is listed outside the quotes, but
before the closing parenthesis. Note, also, that a comma separates the format string (i.e.,
what is contained within the single quotes) and the variable name that stores the value to
be placed in the number format field. It was not necessary to use three different
fprintf
statements because you can output more than one argument with
fprintf
statements.
Three were used to keep the length of the code statements short enough to fit within
Figure 1. The number field was specified as a fixed point formatted field by use of
“%f”. Use “%e” for exponential format, and “%g” when either fixed point or
exponential format (which ever is shorter) is acceptable. We can also tell MATLAB how
many places past the decimal point we would like, as in “%.3f”, which specifies that
three places after the decimal be displayed in fixed point format. Now, if you do not
wish to format your output, you can use MATLAB’s
disp
command, which has the
following syntax
disp(argument)
where “argument” is replaced with a single variable name, or with a character string (that
cannot contain any format fields) enclosed within single quotes. The
disp
statement will
accept only one argument, so for our flowchart in Figure 1, three
disp
statements would
be required just to output the variables, with no annotating text.
Now that we understand the translation of the flowchart to code, how is the code
executed? If you are in the “Editor/Debugger” window with the script m-file open, you
can choose “Run” from the options in the “Tools” pull-down menu, and the command
window will show execution of the program.
» Please enter the radius: 32
Radius = 32.000000 units
Area = 3216.990877 square units
Circumference = 201.061930 units
If you are in the command window, you can enter the script m-file name at the prompt.
» Prob1
Please enter the radius: 32
Radius = 32.000000 units
Area = 3216.990877 square units
Circumference = 201.061930 units
»
Division of Engineering Fundamentals , Copyright 1999 by J.C. Malzahn Kampe 5 / 21
Virginia Polytechnic Institute & State University EF1015 Revised Fall 2000
_____________________________________________________
» Prob1
Please enter the radius: 5
Radius = 5.000000 units
Area = 78.539816 square units
Circumference = 31.415927 units
»
Problem 2: Selection / Decision
The solution to Problem 2 involves a decision on whether the radius, area and
circumference values should be printed. This decision to output is based on whether the
area value exceeds 20. Figure 2 shows a MATLAB program and flowcharted algorithm
that produces the results requested in Problem 2.
START
% This program will calculate the
% area and circumference of a circle,
% allowing the radius as an input,
% but will only provide output if the
% area exceeds 20.
INPUT
R
AREA = p R
2
R = input('Please enter the radius: ');
AREA = pi * R^2;
CIRC = 2.0 p R
CIRC = 2.0 * pi * R;
if AREA > 20.0
if
AREA > 20.0
T
RUE
fprintf('\n Radius = %f units',R)
fprintf('\n Area = %f square units', AREA)
fprintf('\n Circumference = %f units\n', CIRC)
OUTPUT
R, AREA, CIRC
end
FALSE
STOP
Figure 2. A MATLAB program (a script m-file named Prob2) and the corresponding
flowchart for Problem 2.
________________________________________________________________________
[ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • mement.xlx.pl