Num Method 02 Structured Programming MAE284 SP20
Num Method 02 Structured Programming MAE284 SP20
MAE 284
Numerical Methods
R. N. Tantaris
1
MATLAB Basics
• Basic navigation in the command window
– Enter key – always takes you to the command prompt.
>> ( Also hit enter to execute a command)
– Up and Down arrow keys – Scroll through entries in
the MATLAB command history window.
– Backspace and Delete keys – used to edit command
lines.
– Tab key – smart recall, completes previously typed
functions after typing the first few letters.
– clc – command used to clear the MATLAB command
window. (The use of this command is also recorded in
the command history)
2
MATLAB Basics
• Basic arithmetic operations: multiplication of two decimals.
Note: the default
decimal display in
MATLAB is four
decimal places. This
may be changed using
the format command.
3
MATLAB Formatting
• Some Basic Formatting Commands:
– format short – 4 decimal places (default)
– format long – 16 digits
– format short e – five digits (4 decimals) plus exponent
– format long e – 16 digits (15 decimals) plus exponent
– format bank – 2 decimal digits
– format compact – removes extra blank lines
• Other formatting options described in Chapra
page 27 or in MATLAB help (type help
format)
4
MATLAB Basics
5
MATLAB Basics
• Work Session Commands:
1. clear – removes all variables from memory. Be careful
using clear!!! You can loose all of your data!
2. clear var1 var2 – removes ONLY the specific
variables var1 and var2 from memory.
3. exist(‘name’) – determines if a file or a variable
exists with the name ‘name’.
4. quit and exit– closes the MATLAB program.
5. who – lists the variables in current memory.
6. whos – lists the current variables and sizes, and indicates
if they have imaginary parts.
6
MATLAB Basics
6. : - the colon generates an array of equally spaced elements.
7. , - the comma separates array elements and multiple
commands.
8. ; - the semicolon suppresses screen printing and also denotes
a new row in an array.
9. … - ellipsis are used for line continuation.
10. Ctrl-C, Ctrl-Break – Stops execution
of a program
11. And my favorite command, why
7
MATLAB Basics
• Two variables are defined and added below:
Note: the comma (or a
semicolon) is used to
separate multiple
commands (e.g. the
definition of the two
variables)
8
MATLAB Basics
• Built in variables and constants:
1. ans – most recent answer (can easily be
overwritten)
2. eps – specifies accuracy of floating point
precision.
3. i, j – either may be used for the imaginary unit.
4. Inf – infinity.
5. NaN – indicates an undefined numerical result.
6. pi – the number pi.
• Elementary function syntax may be found on page 35-
37 of Chapra.
9
MATLAB Basics
• One of MATLAB’s great strengths is it’s ability to handle
arrays (vectors and matrices). For example, to define a vector
in MATLAB one would do the following:
y = [2, 8, 14, 20]
Where the elements in the vector are separated by spaces or
commas. Remember, the order of the elements is important!
• A vector with equally spaced elements may be created by use
of the colon as: start_value : increment : end_value
For example, a=1:0.1:10 creates a vector from 1 to 10 in steps
of 0.1,
[1.0 1.1 1.2 1.3 1.4 1.5 … 9.8 9.9 10]
Note: x = 0:10 creates an array from 0 to 10 in steps of 1.
• In both cases, we created a row vector (one row with
multiple columns).
10
MATLAB Basics
• An example of the definition and subtraction
of two vectors:
11
MATLAB Basics
• An array index is used to
locate a certain element in
a vector. For example,
x(3), represents the third
value in the vector
defined as x.
1
5
Structured Programming
Sequential execution: First execute
Module 1, then execute Module 2
Module 1
Module 2
Module 1 Module 2
2
0
Structured Programming
• In MATLAB, modules can be built-in or user-
defined functions.
• Examples of modules include:
– Modules (aka Blocks) – sequence of sequentially
executed statements (a collection of related statements)
– Conditional statements – statements that are
executed based on some condition (e.g. if x > 0.5, y = 1.
Otherwise, y = 0).
– Loops – repeated execution of a module (block of code)
– Subroutine or function calls
• We will discuss the first three types of modules today.
2
1
Terminology
• Flow Chart – graphically shows the structure of a
program
• Pseudocode – the structure of a program written in a
generic way. Can be converted to any programming
language
k=0
k<11
k=k+1
This is the
FOR loop x=x+2*k
If 𝑘 ≥ 11
exit FOR loop and
continue to next step
Examples
2
6
Statement Types
• Other than basic processing or execution statements
like x = 4 + 2. There are two main types of
statements:
• Conditional Statements (ask a question)
• If/Then
• If/Then/Else
• If/Then/Elseif/Else
• Loop Statements
• While loops
• While/Break loops
• For loops
Conditional Statements
• Conditional statements are statements that compare
two or more things in order to make a decision.
• Example: if (x < xmax), …
• 3 main types: If/Then, If/Then/Else,
and If/Then/Elseif/Else
if (x < xmax) then
if (x < xmax) then
Done = True
Done = TRUE
end
elseif (y > ymin)
if (x < xmax) then Done = MAYBE
Done = TRUE else
else Done = FALSE
Done = FALSE end
end
Logical (Boolean) Operators
• Logical or Boolean (or relational) operators are used
to compare two things.
• The result of using these is either TRUE, represented
as a 1 or FALSE (0) MATLAB
• Relational operators Notation
– Less than A < B
– Greater than A > B
– Less than or equal to A <= B
– Greater than or equal to A >= B
– Equal to A == B
– Not equal to A ~= B
9
Logical (Boolean) Operators
• Example: >> A = 1 >> A = [1 2 3 4]
>> A = 5; >> B = 0 >> y = A > 2
>> B = 6; >> x = A&B y =
>> x = A > B x = 1×4 logical array
logical logical 0 0 1 1
0 0
Boolean – a data type
>> class(x) >> x = A|B with only two possible
ans = x = values, in this case True
'logical' logical (1) or False (0). In
1 Matlab, they are
considered to be a logical
data type. 9
And, Or, Not, and Exclusive Or
Truth Table
True = 1, False = 0
31
If/Then Statement
Pseudocode
IF b is not 0 THEN
r1=-c/b
ENDIF
MATLAB
MATLAB
33
Pseudocode
If/Then/Elseif/Else IF class = 1 THEN
x=x+8
ELSEIF class < 1
x=x-8
ELSEIF class < 10
x=x-32
ELSE
x=x-64
ENDIF
MATLAB
34
Matlab Bug & Checking Your PDF
• There is a known bug in Matlab starting back in version
R2017a. If you publish your program on a non-lab PC (e.g. your
personal laptop) and your "default path" for saving your figures
for the PDF has a space in it, Matlab will generate an error and
will not include the plots in the PDF.
• The error will look like this:
• [ERROR] FOUserAgent - Image not found. URI: C:/Users/FirstName LastName/AppData/Local/Temp/xxx.bmp. (See position -
1:-1)
• [ERROR] FOUserAgent - Image not found. URI: C:/Users/FirstName LastName/AppData/Local/Temp/xxx.bmp. (No context info
available)
36
Reminder
• Bring clickers next class/lab. Haven’t bought yet,
forgot to bring, no batteries, etc. are not valid excuses.
37