EE2372 Programming elements in MATLAB

21
EE2372 Programming elements in MATLAB Dr. Jose Gerardo Rosiles Spring 2010 TTh 10:30-11:50 am

description

EE2372 Programming elements in MATLAB. Dr. Jose Gerardo Rosiles Spring 2010 TTh 10:30-11:50 am. Variables. Store numerical and alphanumerical values x = 10, z=9.5, s=‘cat’ Can use more meaningful names mixing letters, numbers and underscore character - PowerPoint PPT Presentation

Transcript of EE2372 Programming elements in MATLAB

Page 1: EE2372  Programming elements in MATLAB

EE2372 Programming elements in MATLAB

Dr. Jose Gerardo RosilesSpring 2010

TTh 10:30-11:50 am

Page 2: EE2372  Programming elements in MATLAB

Variables

• Store numerical and alphanumerical values– x = 10, z=9.5, s=‘cat’

• Can use more meaningful names mixing letters, numbers and underscore character– Variables must start with a letter– Temperature = 45, name = ‘Peter’,

number_of_students = 31

Page 3: EE2372  Programming elements in MATLAB

Relational operators

• >, >=, <=, < • == compares if two values are equal• != compares if two values are not equal• Result is a BOOLEAN decision: TRUE of FALSE• How do we represent boolean decisions

numerically?– TRUE = 1– FALSE = 0

Page 4: EE2372  Programming elements in MATLAB

Decision commands

• Logical operators allow us to compare values• What are comparisons useful for?• Make decision within your program• In most computer languages decision are made using the if-

then-else statement• General syntax if ( relational operation is TRUE) do action A else do action B end

Page 5: EE2372  Programming elements in MATLAB

Decision command

• Example: A smoke detector • What are the steps taken by the program running by

the microprocessor inside a smoke detector?• Let’s implement in MATLAB, syntax varies a little if ( relational operation) do A else do B end

Page 6: EE2372  Programming elements in MATLAB

Logical operators

• AND, OR, NOT• Also useful to make decisions• Make more complex logic statements• In MATLAB: & = AND | = OR ~ = NOT

Page 7: EE2372  Programming elements in MATLAB

Example of compound statements

if ( (x > 5) & (x < 10) )

If ( ( (x+y) <= z ) | (y>10) )

• What is the hierarchy of these commands?

Page 8: EE2372  Programming elements in MATLAB

More on variables

• The = operator has a different meaning than in mathematics.

• What is the meaning of: x = x + 1 ?In Math: 0=1????In programming: How does the microprocessor do it?

• Equal “=“ is an assignment operator • What about x = x + y, where y is another variable?

Page 9: EE2372  Programming elements in MATLAB

Looping structures• An important type of variable are arrays.• Array a group of variables with the same name, indexed by a unique

integer• Recall sequences and series from calculus

• Example in MATLAB: grade = zeros(1,10);

Creates 10 variables names grade INDEXED by an integer from 1 to 10 :

grade(1) = 80;grade(5) = 10;etc.

Page 10: EE2372  Programming elements in MATLAB

Looping Structures

• Allow you to repeat the same process many times.

• We have for loops and while loops• for loops consist of an initial value, an

increment and a final value• while loops require a stopping condition.

Page 11: EE2372  Programming elements in MATLAB

for loops

• MATLAB structurefor initial value: increment: final value//Do something endExample: Compute the average value of the

grade() array.

Page 12: EE2372  Programming elements in MATLAB

for loops

grade = zeros(1,10);avg = 0; sum = 0; for ?: ?: ? //How do we

accumulate the values of grade?

endavg = sum/10;

0 avg

0 sum

80 grade(1)

75 grade(2)

85 grade(3)

… …

Memory map

Steps taken by microprocessor on first iteration1. Fetch sum2. Fetch grade(1)3. Add sum and grade(1)4. Store result in sum

Page 13: EE2372  Programming elements in MATLAB

while loops

grade = zeros(1,10);avg = 0; sum = 0;i = 1; while ( stop condition )

sum = sum + grade(i); i = i + 1;endavg = sum/10;

i < 10

Page 14: EE2372  Programming elements in MATLAB

Input and output in MATLAB

• To get user input we have the special function “input” that allows to display a message

Temperature = input(‘Enter toda’s tempetature: ‘);

• Write value at prompt and press enter to continue with program

Page 15: EE2372  Programming elements in MATLAB

Input and output in MATLAB

• For output there are more variations:

disp(‘my message’);

error(‘to print messages when an error condition has … occurred’)

• File print file function, very advanced formatting:

fprintf(1, ‘my string %d %f %s’, int_val, float_val, … str_val);

Page 16: EE2372  Programming elements in MATLAB

A Complete Example%% program to convert number grades to letter graders

grade = [80 70 75 90 65 95 75 80];

fprintf(1, ' Student Grade Letter Grade \n');for i = 1:1:8 if(grade(i)>=90) letter_grade = 'A'; elseif (grade(i) >= 80) letter_grade = 'B'; elseif (grade(i) >= 70) letter_grade = 'C'; elseif (grade(i) >= 60) letter_grade = 'D'; else letter_grade = 'F'; end

fprintf(1,' %d %d %c \n', i, grade(i), letter_grade); end

Student Grade Letter Grade 1 80 B 2 70 C 3 75 C 4 90 A 5 65 D 6 95 A 7 75 C 8 80 B

Formatted Program Output

Page 17: EE2372  Programming elements in MATLAB

Modularity• Consider the previous program%% program to convert number grades to letter graders

grade = [80 70 75 90 65 95 75 80];

fprintf(1, ' Student Grade Letter Grade \n');for i = 1:1:8 if(grade(i)>=90) letter_grade = 'A'; elseif (grade(i) >= 80) letter_grade = 'B'; elseif (grade(i) >= 70) letter_grade = 'C'; elseif (grade(i) >= 60) letter_grade = 'D'; else letter_grade = 'F'; end

fprintf(1,' %d %d %c \n', i, grade(i), letter_grade); end

Some parts of the programmay look too cluttered,making the program confusing

Page 18: EE2372  Programming elements in MATLAB

Modularity

• We can improve the program in terms of functionality and clarity using functions.

• Functions (aka subroutines) encapsulate certain program behaviors or patterns that repeat over and over in your program.

• This encapsulation – visually makes your program read better (less clutter) and – allows the “extension” of the language to add customized

functionality

Page 19: EE2372  Programming elements in MATLAB

Modularity• Let’s analyze our program• The chain of if-else statements is hard to read.• Let encapsulate this functionality into a function.

function output_value = function_name (param1, pararm2, param3, ….)

// body of function

end

Page 20: EE2372  Programming elements in MATLAB

Modularity and Functions%% program to convert number grades to letter graders

grade = [80 70 75 90 65 95 75 80];

fprintf(1, ' Student Grade Letter Grade \n');for i = 1:1:8 if(grade(i)>=90) letter_grade = 'A'; elseif (grade(i) >= 80) letter_grade = 'B'; elseif (grade(i) >= 70) letter_grade = 'C'; elseif (grade(i) >= 60) letter_grade = 'D'; else letter_grade = 'F'; end

fprintf(1,' %d %d %c \n', i, grade(i), letter_grade); end

function letter = convert_to_letter(in_grade)

if (in_grade>=90) letter_grade = 'A'; elseif (in_grade >= 80) letter_grade = 'B'; elseif (in_grade >= 70) letter_grade = 'C'; elseif (in_grade >= 60) letter_grade = 'D'; else letter_grade = 'F'; end

letter = letter_grade;

end

letter_grade = convert_to_letter( grade(i) );

Page 21: EE2372  Programming elements in MATLAB

Exercise

• Write a function to compute .• Let be a non-negative integer ( ).