180 likes | 194 Views
This lecture covers more problems from Chapter 6 and introduces conditional statements in MATLAB, including if...end and switch...case. Examples and exercises from Chapter 7 are also discussed.
E N D
Lecture 12 Oct 10, 2011 • more problems from Chapter 6 • Chapter 7 • conditional statement • if … end; • if … elseif … elseif … else … end; • switch and case • examples and exercises from Chapter 7
Exercise 6.7. Write a function perfectSquare that returns true (false) if the input is a perfect square (not a perfect square). Example: perfectSquare(23) returns 0, perfectSquare(49) returns 1.
Exercise 6.7. Write a function perfectSquare that returns true (false) if the input is a perfect square (not a perfect square). Example: perfectSquare(23) returns 0, perfectSquare(49) returns 1. Solution: function out = perfectSquare(x) out = (sqrt(x)*sqrt(x)== x);
Exercise 6.9 Write a function wholeAndPart that takes as input a number and returns the integer part and the fractional part. Example: >> [a,b] = wholeAndPart(23.4) a = 23 b = 0.4000
Exercise 6.9 Write a function wholeAndPart that takes as input a number and returns the integer part and the fractional part. Example: >> [a,b] = wholeAndPart(23.4) a = 23 b = 0.4000 function [a,b] = wholeAndPart(x) a = floor(x); b = x-a;
Chapter 7 Conditionals (branches) • Example: Write a function in Matlab that takes a, b and c and determines if the triangle formed by sides a, b and c is a right triangle, acute or obtuse. • First we need to determine the sides in sorted order a <= b <= c. Then determine if • a2 + b2 = c2 (right triangle) • a2 + b2 < c2 (obtuse angle) • a2 + b2 > c2 (acute angle)
Example: Determine letter grade from score by the rules: score >= 90 A >= 80 B >= 70 C >= 60 D none of the above F We can do this with the construct if … elseif … elseif … else … end;
score >= 90 A >= 80 B >= 70 C >= 60 D none of the above F function res = convertGrade(score) if score >= 90 res = ‘A’; elseif score >= 80 res = ‘B’; elseif score >= 70 res = ‘C’; elseif score >= 60 res = ‘D’; else res = ‘F’; end;
Same example using switch statement: function res = grade( score) switch ( score) case { 100, 99,98, 97, 96, 95, 94, 93, 92, 91, 90} res = 'A'; case { 89, 88, 87, 86, 85, 84, 83, 82, 81, 80} res = 'B'; ... otherwise res = 'F'; end; Problem with the solution: does not allow scores like 89.5
Exercise 7.1 Write a function to compute the median of three numbers (without using sort).
Exercise 7.1 solution: Alternate solution is to use min and max: res = a + b + c –min(a, b, c) – max(a, b, c)
Exercise 7.7 • function res = isvowel(letter) • switch(letter) • case ’a’ • res = 1; • case ’e’ • res = 1; • case ’i’ • res = 1; • case ’o’ • res = 1; • case ’u’ • res = 1; • otherwise • res = 0; • end;
Exercise 7.7 Another solution: function res = isvowel(letter) res = any(letter == ’aeiou’);
Exercise 7.8 Can you suggest a more succinct solution using array indexing?
Exercise 7.8 (alternative solution) function res = protons(atom) symbols = {'H', 'He', 'Li', 'Be', ... 'B', 'C', 'N', 'O', 'F', 'Ne', ... 'Na', 'Mg', 'Al', 'Si', 'P', 'S', 'Cl', 'Ar'}; nprotons = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12, 13, 14, 15, 16, 17, 18]; ind = strcmpi(atom, symbols); % Case independent if ~any(ind) error([atom, ' not an atomic symbol']); else res = nprotons(ind); end
Another exercise involving branch: Write a Matlab function insertionsort that sorts by inserting the k-th element into its correct place relative to the first k–1 elements for k = 2, 3, … We will first write a function insert(A, k) that inserts A(k) in its correct place. >> A = [1, 9, 12, 7, 2, 14]; >> B = insert(A, 4); >> B ans = [ 1 7 9 12 2 14]