Ch02_Programming_s
Ch02_Programming_s
NUMERICAL METHODS
CHAPTER 2 – PROGRAMMING AND SOFTWARE
1
PACKAGES AND PROGRAMMING
Objective is how to use the computer as a tool to obtain numerical solutions to a given
engineering model.
2
STRUCTURED PROGRAMMING
Algorithm: It is a form of step-by-step instructions to perform a specific task.
Structured programming: Set of rules that prescribe good style habits for the programmer
to code algorithms.
Flowchart Example
4
PSEUDOCODE
• A pseudocode uses code-like statements instead of the graphical symbols of the
flowchart.
• Some common conventions for the pseudocode :
• Keywords such as IF, DO, INPUT, etc., are capitalized.
• Conditions, processing steps, and tasks are in lowercase.
• Processing steps are indented.
• Keywords form a “sandwich” around the steps to visually define the extent of each
control structure.
Pseudocode Example
In this course, Pseudocode will be the main tool for
communicating algorithms.
5
LOGICAL REPRESENTATION - SEQUENCE
In an algorithm, there are mainly three logical representations:
• sequence
• selection
• repetition
Sequence: Shows that the computer code is to be implemented one instruction at a time
(i.e., step-by-step procedure).
• The structure can be expressed generically as a flowchart or as pseudocode.
6
LOGICAL REPRESENTATION - SELECTION
Selection: Splits the program’s flow into branches based on the outcome of a logical
condition.
Four ways for selection:
1) The single-alternative decision (IF/THEN):
• It allows for a detour in the program flow if a logical condition is
true.
• If it is false, nothing happens, and the program moves directly to the
next statement following the ENDIF.
7
SELECTION – Continued
10
EXAMPLE - SOLUTION
Pseudocode for the Example: An Improved Pseudocode:
11
PSEUDOCODE FOR PARACHUTIST PROBLEM (self study)
Recall that, given an initial condition for time and velocity, the problem involved
iteratively solving the formula (Euler’s method)
where
Simple Pseudocode: Improved Pseudocode:
12
MODULAR PROGRAMMING
Modular programming: The approach of dividing computer programs into small
subprograms, or modules, that can be developed and tested separately.
13
MODULAR PROGRAMMING EXAMPLE
Falling Parachutist: Euler Method in general: Using Modular Pseudocode of Euler
method for Parachutist Problem:
New value = old value + slope × step size Calling the Euler function to calculate
y = yi + dydt × h the output:
FUNCTION dy (t, y)
g = 9.81
cd = 12.5
m = 68.1
dydt = g-(cd/m)*v
END dy
14
PROGRAMMING ERRORS
Syntax errors: Miswriting the commands. The code will not compile.
Run-time error: Program will compile, but stops running at some point. (e.g. due to
division by zero.)
Logical error: Program will compile and run, but the result is wrong.
A program is said to have a bug if it either contains a mistake that ends up with errors
or it does not function in the way it is intended to.
Debugging is the process of finding the root cause, workarounds and possible fixes
for bugs.
15
EXCEL®
• Excel is the spreadsheet produced by Microsoft, Inc.
• Spreadsheets are a special type of mathematical software that allow the user to
enter and perform calculations on rows and columns of data.
• Excel has some built-in numerical capabilities, including equation solving, curve
fitting, and optimization.
• It also has several visualization tools, such as graphs and three-dimensional
surface plots, that serve as valuable adjuncts for numerical analysis.
• It also includes Visual Basic (VBA) as a macro language that can be used to
implement numerical calculations.
16
MATLAB®
• MATLAB is the software product of The MathWorks, Inc.,
• It was co-founded by the numerical analysts Cleve Moler and John N. Little.
• MATLAB was originally developed as a matrix laboratory.
• To this day, the major element of MATLAB is still the matrix.
• To the matrix manipulations, MATLAB has added a variety of numerical
functions, symbolic computations, and visualization tools.
• A MATLAB code can be directly written to MATLAB Command Window:
When you run this M-file, the Command Window the following prompt will show:
The user can then enter a value like 100, and the result will be displayed as
18
MATLAB CODE VS. PSEUDOCODE
19
MATLAB CODE FOR PARACHUTIST EXAMPLE (self study)
EXERCISE: Create an M-file in MATLAB by typing the following code and run it to get results.
g=9.81;
m=input('mass (kg): '); Once you save this as .m file and run the .m
cd=12.5; file in MATLAB,
ti=0; The following prompt should appear in the
tf=2;
vi=0; Command Window:
dt=0.1;
t = ti;
v = vi;
h = dt;
while (1) Type the value as 100 and you will have the
if t + dt > tf result as
h = tf - t;
end
dvdt = g - (cd/m) * v;
v = v + dvdt * h;
t = t + h;
if t >= tf, break, end
end
disp('velocity (m/s):')
disp(v)
20
MATLAB FUNCTION FOR EULER METHOD (self study)
Recall the Pseudocode for the EXERCISE: Write a MATLAB Then, write the following
Euler Method we defined before. function for the Euler method: commands to the
function yy = euler(dt,ti,tf,yi)
Command Window and
t = ti; get results:
y = yi;
h = dt; >> ti=0;
while (1)
if t + dt > tf
>> tf=2.;
h = tf - t; >> vi=0;
end >> dt=0.1;
dydt = dy(t, y); >>
y = y + dydt * h;
t = t + h;
euler(dt,ti,tf,vi,m,cd)
if t >= tf, break, end
end The answer will be
yy = y; displayed as
Save this function as “euler.m”.
Also Recall the Pseudocode for
the slope of parachutist problem: Write the slope function
and save as “dy.m”:
FUNCTION dy (t, y)
function dydt = dy(t, y)
g = 9.81, cd = 12.5, m = 68.1
g = 9.81; cd = 12.5; m = 68.1;
dydt = g-(cd/m)*v
dydt = g - (cd/m) * y;
END dy
21
OTHER LANGUAGES AND LIBRARIES
• Python
• Fortran
• C++
• Mathcad
• etc.
• In this course, our approach will be to provide you with well-structured procedures written as
pseudocode.
• This collection of algorithms then constitutes a numerical library that can be accessed to perform
specific numerical tasks in a range of software tools and programming languages.
22