Presentation is loading. Please wait.

Presentation is loading. Please wait.

Beginning Programming for Engineers Matlab Conditional Computation.

Similar presentations


Presentation on theme: "Beginning Programming for Engineers Matlab Conditional Computation."— Presentation transcript:

1 Beginning Programming for Engineers Matlab Conditional Computation

2 Learning goals for class 3 Understand how conditional computation can be used in an algorithm, and know how to express it using the if and else statements. Know how to use Matlab's relational and logical operators. Understand how iteration can be used in in an algorithm, and know how to express it using the while statement. Understand several algorithm design idioms based on iteration: counter variables, running sums, accumulating results, and related ideas.

3 Conditional execution (flowchart)

4 Conditional execution: The if statement Format is: if expression some statements elseif expression some statements else some statements end There can be more than one elseif stanza. Both elseif and else stanzas can be omitted. The expressions must return true or false.

5 Relational Operators Relational operators return either true or false. o True is represented by 1. o False is represented by 0. Relations can be further combined using the logical operators. Try vector forms: A = [1 2 3 4 5]; A >= 3 A ~= 4 A(4) == 4 a equals b a == b a not equal ba ~= b a less than ba < b a less than or equal to b a <= b a greater than ba > b a greater than or equal to b a >= b

6 Logical operators Logical operators combine expressions that are either true or false. Examples: isreal(a) ~isreal(a) if (time ==0) && (dist == 0) speed = 0; else speed = dist / time end a and ba & b a && b a or ba | b a || b not a~a

7 Conditional execution: Basic hi-lo % Script to play a very basic hi-lo game. clear clc M = 100; % Maximum value % Pick a number. myNum = floor(1+M*rand()); % Get player's guess. yourNum = input('Enter your guess: '); % Display message if yourNum < myNum disp('Too low!'); elseif yourNum > myNum disp('Too high!'); else disp('That is it!'); end disp('My number was'); disp(myNum);

8 Design Technique: Break into cases Suppose you want to calculate the point of intersection of two lines represented as: y = m1*x + b1 y = m2*x + b2 Solve for x yields: x = (b2 - b1) / (m1 - m2) if m1 ~= m2 x = (b2 - b1) / (m1 - m2); y = m1*x + b1; else disp('Lines are parallel!'); end

9 Iteration with a test (flowchart)

10 Iteration with a test: The while statement Format is: while expression statements end The expression is evaluated; if true, the statements are executed and the test repeated... The expression should depend on something that will eventually be false.

11 Iteration: Better hi-lo % Script to play a better hi-lo game. clear clc M = 100; % Pick a number. myNum = floor(1+M*rand()); % Get player's guess. disp('Enter a guess between 1 and') disp(M) yourNum = input('Enter your guess: '); % Repeat until correctly guessed. while yourNum ~= myNum if yourNum < myNum disp('Too low!') else disp('Too high!') end yourNum = input('Next guess: '); end % Finish up disp('You got it!')

12 Design Technique: Counters Sometimes we need to build up results in pieces: running sums, running products, etc. % simple_counting.m: Counting. clear clc c = 1; while c <= 10 disp(c); c = c + 1; end The variable c is a counter.

13 Design Technique: Running sums function [t] = triangle(n) % TRIANGLE computes triangle numbers. i = 0; t = 0; while i <= n t = t + i; i = i+1; end return; end The variable t is a running sum. The variable i is another counter (loop variable, index variable, control variable).

14 Multiple running sums... function [f] = fib(n) % FIB computes fibonacci sequence f = 1; f1 = 0; f2 = 1; i = 2; while i <= n f = f1 + f2; f1 = f2; f2 = f; i = i+1; end return end

15 Design Technique: Accumulating vectors function [ft] = fib_table(c) % FIB_TABLE creates table of fibonacci's. ft = []; i = 1; while i <= c; ft = [ft fib(i)]; i = i+1; end return end


Download ppt "Beginning Programming for Engineers Matlab Conditional Computation."

Similar presentations


Ads by Google