Download Matlab Chapter 3 slides, with some basic skill. and more Slides Computer Science in PDF only on Docsity!
Chapter 3 Branching Statements and
Program Design
Introduction to Top-Down Design Techniques
Use of Pseudocode
The Logical Data Type
Branches
Additional Plotting Features
More on Debugging MATLAB Programs
Summary
Exercises
Introduction
Sequential programs
Reading input data, process it to produce a desired answer, print out the answer, and quit.
There is no way to repeat sections of the program more than once
There is no way to selectively execute only certain portion of the program depending on values of the input data.
We will introduce a number of MATLAB statement
Allow us to control the order in which statements are executed in a program. - Branches : select specific sections of the code to execute - Loops : which cause specific sections of the code to be repeated. (Chapter 4)
We will introduce a top-down design techniques.
We will introduce pseudocode algorithm.
Matlab Matlab
Top-Down Design
Clearly state the problem that you are trying to solve.
(Page 88)
Define the inputs required by the program and the outputs
to be produced by the program. (Page 89)
Design the algorithm that you intend to implement in the
program. (Page 90)
Algorithm: a step-by-step procedure for finding the solution to a problem.
Stepwise refinement: pseudocode.
Turn the algorithm into MATLAB statements.
Test the resulting MATLAB program.
3.2 Use of Pseudocode
• Pseudocode is a hybrid mixture of
MATLAB and English.
• Pseudocode is intended to aid you in
organizing your thoughts before converting
them into MATLAB code.
– Example 2.3 (page 64)
– Pseudocode (page 93)
3.3 The Logical Data Type
• true or false
• MATLAB operators
– Relational operator
– Logic operator (single byte)
Relational operators may be used to compare a scalar value with an array.
Relational operators can only compare two strings if they are
of equal lengths. (see chapter 6)
== symbol is a comparison operation that return a logical result
= symbol is a assignment operation
7 + 3 < 2 + 11 is equal to (7 + 3) < ( 2 + 11)
A caution about the == and ~= operators
For string comparison is safe
For numeric is not safe
Due to roundoff errors
Logical ANDs: && and &
Difference 1
&& supports short-circuit evaluations
&& will evaluate expression l1 and immediately return result
if l1 is false , never evaluates l
& evaluates both l1 and l2 before returning an answer
Difference 2
&& works between scalar values
& works with either scalar or array values, as long as the sizes of the arrays are compatible
faster
x = a / b > 10.0 b = 0, x =? x = (b ~= 0) && (a/b > 10.0)
Logical inclusive ORs: || and |
Difference 1
|| supports partial evaluations
|| will evaluate expression l1 and immediately return result
if l1 is true , never evaluates l
| evaluates both l1 and l2 before returning an answer
Difference 2
|| works between scalar values
| works with either scalar or array values, as long as the sizes of the arrays are compatible
Using numeric data with logic operators
Real numeric data can be use with logic operators
~5 is false, ~0 is true
Logic operators may be used to compare scalar value with an array.
Logic operators may be used to compare two arrays with same size
Logic operators may not be used with complex or imaginary numeric data - 2i & 2i will produce an error when it is evaluated
Hierarchy of operations
All arithmetic operators are evaluated first in the order
previously described.
All relational operators (==, ~=, >,>=,<,<=) are
evaluated, working from left to right.
All ~ operators are evaluated.
All & and && operators are evaluated, working from
left to right.
All | and || , and xor operators are evaluated, working