MATLAB Programming Concepts Explained
MATLAB Programming Concepts Explained
1
Algorithms
2
Control Structures
3
Structured Programming
4
Advantages of Structured Programming 1
5
Advantages of Structured Programming 2
6
Procedure Oriented Programming
How to get the task done
7
Object Oriented Programming
Encapsulation/ Class
Main focus: Data security
8
Steps for Developing a Computer
Solution 1
9
Steps for Developing a Computer
Solution 2
10
Effective documentation
4. Use of flowcharts.
11
Documenting with Charts
12
Structure Chart of a Game Program
13
Use of Flowcharts
14
Flowchart representation of the if statement
15
Documenting with Pseudocode
16
Example; Iterative Operations
Determine how many terms are required for the sum of the series
10𝑘^2−4𝑘+2,𝑘=1,2,3,...to exceed 20,000. What is the sum for this many terms?
1. Initialize the total to zero.
2. Initialize the counter to zero.
3. While the total is less than 20,000 compute the total.
3.1 Increment the counter by 1.
𝑘=𝑘+1
3.2 Update the total.
total = 10∗𝑘^2−4∗𝑘+2+ total
4. Display the current value of the counter.
5. Display the value of the total.
6. Stop.
17
total = 0;
1. Initialize the total to zero.
2. Initialize the counter to zero. k = 0;
3. While the total is less than 20,000 while total < 2e+4
compute the total. k = k+1;
3.1 Increment the counter by 1.
total = 10*k^2 − 4*k + 2 + total;
𝑘=𝑘+1
3.2 Update the total. end
total = 10∗𝑘^2−4∗𝑘+2+ total disp(’The number of terms is:’)
4. Display the current value of the counter. disp(k)
5. Display the value of the total.
6. Stop. disp(’The sum is:’)
disp(total)
end
18
Finding Bugs
Debugging a program is the process of finding and
removing the “bugs,” or errors, in a program. Such errors
usually fall into one of the following categories.
19
Locating Runtime Errors 1
20
Locating Runtime Errors 2
21
Development of Large Programs 1
22
Development of Large Programs 2
23
Relational Operators 1
Operator Meaning
< Less than.
<= Less than or equal to.
> Greater than.
>= Greater than or equal to.
== Equal to.
~= Not equal to.
24
Relational Operators 2
25
Relational Operators 3
finds all the elements in x that are less than the corresponding
elements in y. The result is z = 6.
26
Relational Operators 4
is equivalent to
z = 5 >(2+7)
and returns the result z = 0.
27
The logical Class 1
28
The logical Class 2
30
Accessing Arrays Using Logical Arrays 2
31
If x=[-1,13,5,-14] and y=[-9,13,0,4], what will be the result of the
~(y>x).
a) 0 1 1 0
b) 1 0 1 1
c) 1 1 1 0
d) 1 0 0 1
32
Logical Operators 1
33
Logical Operators 2
34
Order of Precedence for Operator Types
35
Logical Functions 1
36
Logical Functions 2
37
Logical Functions 3
38
The find Function
39
Logical Operators and the find Function 1
Note that the find function returns the indices, and not the
values.
40
Logical Operators and the find Function 2
Note that the find function returns the indices, and not the
values.
In the following session, note the difference between the
result obtained by y(x&y) and the result obtained by
find(x&y) in the previous slide.
>>x = [5, −3, 0, 0, 8];y = [2, 4, 0, 5, 7];
>>values = y(x&y)
values =
2 4 7
>>how_many = length(values)
how_many =
3
41
The if Statement
statements
end
Every if statement must have an accompanying end
statement. The end statement marks the end of the
statements that are to be executed if the logical expression
is true.
42
The else Statement
statement group 1
else
statement group 2
end
43
yourNumber = input('Enter a number: ');
if yourNumber < 0
disp('The number is negative')
end
if yourNumber >= 0
disp('the number is either zero or positive')
end
44
Flowchart of the else Structure
45
The Logical Expression in the else
Structure 1
46
The Logical Expression in the else
Structure 2
47
The Logical Expression in the else
Structure 3
48
Making Statements Concise
if logical expression 1
if logical expression 2
statements
end
end
statements
end
49
The elseif Statement
51
Flowchart for the General
if-elseif-else Structure
52
Example of an if-elseif-else
Structure
For example, suppose that y = log(x) for x > 10, y
=sqrt(x) for 0 <= x <= 10, and y = exp(x) − 1 for
x < 0. The following statements will compute y if x already
has a scalar value.
if x > 10
y = log(x)
elseif x > 0
y = sqrt(x)
else
y = exp(x) − 1
end
53
Strings and Conditional Statements 1
54
Strings and Conditional Statements 2
55
Strings and Conditional Statements 3
56
Strings and Conditional Statements 4
58
Flowchart of a for Loop
59
Loop Variable Expression 1
Note the following rules when using for loops with the loop variable
expression k = m:s:n:
• If the step value s is not an integer, round-off errors can cause the
loop to execute a different number of passes than intended.
60
Loop Variable Expression 2
61
Using a Logical Array as a Mask
We can often avoid the use of loops and branching and thus create
simpler and faster programs by using a logical array as a mask that
selects elements of another array. Any elements not selected will
remain unchanged.
The following session creates the logical array C from the numeric array
A given previously.
The result is
1 0 1
C = 1 0 1
0 1 1
62
Example of the Mask Technique
63
while Loops
while x < 25
disp(x)
x = 2*x − 1;
end
64
The Typical Structure of a while Loop
65
Flowchart of the while Loop
66
The switch Structure
67
Syntax of the switch Structure
68
Example of the switch Structure