3 Topic 2 Fundamentals of Computer Aided Mathematical Calculations
3 Topic 2 Fundamentals of Computer Aided Mathematical Calculations
SERRANO
PROGRAMMING AND SOFTWARE
OBJECTIVE
•To learn how to create well
documented program files by employing
structured programming to implement
logic and repetition
PROGRAMMING
Programming Topics:
1. Simple information representation (constants, variables & type
declarations)
2. Advanced information rep. (data structure, arrays and records)
3. Mathematical formulas (assignment, priority rules and intrinsic functions)
4. Input/Output
5. Logical representation (sequence, selection & repetition)
6. Modular programming (functions and subroutines)
PROGRAMMING
LOGICAL REPRESENTATION:
Sequence Structure: expresses the trivial idea that unless you direct
it otherwise, the computer code is to be implemented one instruction
at a time. As in Fig. 2.2, the structure can be expressed generically as
a flowchart or as pseudocode.
PROGRAMMING
LOGICAL REPRESENTATION:
Selection: In contrast to the step-by-step
sequence structure, selection provides a
means to split the program’s flow into
branches based on the outcome of a
logical condition. Fig. 2.3 shows the two
most fundamental ways for doing this.
LOGICAL REPRESENTATION:
Selection:
Although the IF/THEN and the IF/THEN/ELSE
constructs are sufficient to construct any
numerical algorithm, two other variants are
commonly used. Suppose that the ELSE clause of
an IF/THEN/ELSE contains another IF/THEN. For
such cases, the ELSE and the IF can be combined
in the IF/THEN /ELSEIF structure shown in Fig.
2.4a. Notice in Fig 2.4a there is a chain or
“cascade” of decisions.
LOGICAL REPRESENTATION:
Repetition: provides a means to implement instructions repeatedly. The resulting
constructs, called loops, come in two “flavors” distinguished by how they are
terminated.
The first and most fundamental type is called a decision loop because it
terminates based on the result of a logical condition. Fig. 2.5 shows the most
generic type of decision loop, the DOEXIT construct, also called break loop.
If the first block is not included, the structure is sometimes called a pretest
loop because the logical test is performed before anything occurs. Alternatively, if
the second block is omitted, it is called a posttest loop. Because both blocks are
included, the general case in Fig 2.5 is sometimes called a midtest loop.
The break loop in Fig 2.5 is called a logical loop because it terminates on a
logical condition. In contrast, a count-controlled or DOFOR loop (Fig 2.6) performs
a specified number of repetitions, or iterations
PROGRAMMING
LOGICAL REPRESENTATION:
Repetition:
PROGRAMMING
LOGICAL REPRESENTATION:
Repetition:
PROGRAMMING
LOGICAL REPRESENTATION:
Repetition:
The index (i in Fig. 2.6) is a variable that is set at an initial value of
start. The program then tests whether the index is less than or equal
to the final value, finish. If so, it executes the body of the loop, and
then cycles back to the DO statement. Every time the ENDDO
statement is encountered, the index is automatically increased by the
step. Thus, the index acts as a counter. Then, when the index is
greater than the final value (finish), the computer automatically exits
the lopp and transfers control to the line following the ENDDO
statement.
PROGRAMMING
Eq. 2.2
MODULAR PROGRAMMING
Example, Fig. 2.7 shows a function
developed to implement Euler’s method.
Notice that this function application and
the previous versions differ in how they
handle input/output. In the former
versions, input and output directly come
from (via INPUT statement) and to (via
DISPLAY statements) the user. In the
function, the inputs are passed into the
FUNCTION via its argument list
Function Euler(dt, ti, tf, yi)
and the output is returned via the
assignment statement
y = Euler(dt, ti, tf, yi)
PROGRAMMING
MODULAR PROGRAMMING
In addition, recognized how generic the routine has become. There
are no references to the specifics of the parachutist problem. For
example, rather than calling the dependent variable y for velocity,
the more generic label, y, is used within the function. Further, notice
that the derivative is not computed within the function by an explicit
equation. Rather, another function, dy, must be invoked to compute
it. This acknowledges the fact that we might want to use this function
for many different problems beyond solving for the parachutist’s
velocity.
EXCEL
You should recognize that normal MATLAB use is closely related to programming.
For example, suppose that we wanted to determine the analytical solution to the
parachutist problem. This could be done with the following series of MATLAB
commands
>>g=9.81;
>>m=68.1;
>>cd=12.5;
>>tf=2;
>>v=g*m/cd*(1-exp(-cd/m*tf))
With the result being displayed as
v= 16.4217
Thus, the sequence of commands is just like the sequence of instructions in a
typical programming language.
MATHCAD
MATHCAD attempts to bridge the gap between spreadsheets like Excel and
notepads. It was originaaly developed by Allen Razdow of MIT who co founded
Mathsoft, Inc., which published the first commercial version in 1986.
MATHCAD is essentially an interactive notepad that allows engineers and
scientists to perform a number of common mathematical, data-handling, and
graphical tasks. Information and equations are input to a “whiteboard” design
environment that is similar in spirit to a page of paper. Unlike a programming tool
or spreadsheet, Mathcad’s interface accepts and displays natural mathematical
notation using keystrokes or menu palette clicks – with no programming required.
Because the worksheets contain live calculations, a single keystroke that changes
an input or equation instantly returns an updated result.
MATHCAD can perform tasks in either numeric or symbolic mode. In numeric
mode, Mathcad functions and operators give numeriocal responses, whereas in
symbolic mode results are given as general expressions or equations. Maple V, a
comprehensive symbolic math package, is the basis of the symbolic mode and was
incorporated into Mathcad in 1993
ASSIGNMENT #2:
INSTRUCTION: Write your solution in 1 whole long bond paper. All solution must
be hand written and submit it through google classroom. Follow the format given
(Deadline will be before the next meeting)