1 / 18

MATLAB Lecture 12: Chapter 6 Problems and Chapter 7 Conditionals

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.

lhathaway
Télécharger la présentation

MATLAB Lecture 12: Chapter 6 Problems and Chapter 7 Conditionals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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.

  3. 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);

  4. 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

  5. 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;

  6. 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)

  7. 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;

  8. 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;

  9. 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

  10. Exercise 7.1 Write a function to compute the median of three numbers (without using sort).

  11. Exercise 7.1 solution: Alternate solution is to use min and max: res = a + b + c –min(a, b, c) – max(a, b, c)

  12. Exercise 7.5

  13. 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;

  14. Exercise 7.7 Another solution: function res = isvowel(letter) res = any(letter == ’aeiou’);

  15. Exercise 7.8 Can you suggest a more succinct solution using array indexing?

  16. 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

  17. Exercise 7.10

  18. 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]

More Related