Lecture_7_Boolean_Logic_and_Logical_Operations_cont
Lecture_7_Boolean_Logic_and_Logical_Operations_cont
)
(Chapter 4—Riggs)
Upcoming assignments:
Assignment 5 (Boolean Logic & Logical Operations): due February 6, 11:59 PM
EXAM 1 (February 18)
Ask the user to input a number x, then compute the appropriate value of y. If x < 0, display
an error message to the screen.
Error display command: disp(‘The value of x < 0’) (disp(‘Error’) also OK)
Review: In-Class Exercise, Cont’d.
This is one example of solution for Problem 1 (other solutions may also be
correct).
if (x < 0)
disp(‘The value of x < 0’)
elseif (x < 5)
y = sqrt(x)
else
y = ln(x)
end
for Loops
Basic Form:
Note: index tracks which iteration of
for index = n_initial : n_increment : n_final
the loop is being run.
executable code
It often directly affects the executable
end
code.
Where: n_initial = initial value of index (real or integer)
n_final = final value of index (real or integer)
n_increment = increment used between n_initial and n_final
for Loops
The index variable takes on the first value in the range, then MATLAB runs all the commands
between the for and end lines.
Then, the index variable takes on the next value in the range, and MATLAB runs the
commands again.
Once the loop has finished the last iteration, the program continues from after the end line.
In-class exercise
Given: c = [1 0 2 1]
Using a for loop, create a script that loops through vector c and adds 6 to each
element.
How to Compute a “Running” Sum
Given:
Compute:
V_rms = sqrt(sum(V.*V)/N)
V_rms =
7.2214
or
V_rms = sqrt(dot(V,V)/N)
V_rms =
7.2214
Loop structures are generally not computationally efficient compared to matrix operations
Nested for loops
Repetitively applies the execution of a set of statements until a logic state IS NOT
satisfied.
Basic Form:
Example (Lecture7_Ex5.m) : Print integers from 1 to 3. For each integer, print its
squared value.
1
i = 1; 1
while i <= 3 2
disp(i); 4
disp(i^2); 3
i = i + 1; 9
end >>
A Simple Example
In this script, using a while loop and the rand function, you will write a
program to generate a random number greater than 0.5.
In this script, using a while loop and the rand function, you will write a
program to generate a random number greater than 0.5.
Has an index variable which automatically Does not have an automatic index variable.
changes value at each iteration. If you want one, it must be added manually.
An Infinite Loop
Infinite loop occurs if the logical expression in a WHILE loop never becomes
false.
x = 5;
while (x > 0)
disp(x)
end
You can often save lines of code by using MATLAB commands instead of loops.
For example:
x = [0 : 5 : 100]; for k = 1 : 21
y = cos( x ); x = (k – 1)*5;
y(k) = cos(x);
end
Equivalent Statements
A Simple Example
n = 5;
Factorial_Product = 1;
for i = 2:n
Factorial_Product = Factorial_Product*i; A “Running” Product!
end
Answer:
Factorial_Product = 120
An Example, Cont’d.
n = 5;
factorial(n)
ans =
120
Some examples: function and for loop
x = 0.6;
Cos_x = fct_cosx(x);
Ans = Note: this series can be used
0.8253 to approximate a function or
compute an otherwise incomputable
The function file (fct_cosx.m) consists of: sum. Will be revisited later
function [Cos_x] = fct_cosx(x) (numerical methods).
sum = 0;
for n = 0:10
sum = sum + (-1)^n * x^(2*n)/factorial(2*n);
end
Cos_x = sum;
end