Download presentation
Presentation is loading. Please wait.
Published byArchibald Bradley Modified over 9 years ago
1
Copyright © 2005. The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm III Chapter 3x Functions and Recursion PowerPoint to accompany
2
Function error Function overwrites parameters Function overwrites parameters function A = funcone2(r) r = input('enter r'); % overwrites parameter r V = 10; h = 3*V/(pi*(r.^2)); A = pi*r.*sqrt(h.^2 + r.^2);
3
AGENDA More practice with Functions Polynomial Functions Recursive Functions 3-2
4
A MATLAB function is… A package of code stored in a.m-file A package of code stored in a.m-file Dedicated to a particular task, such as Dedicated to a particular task, such as Calculating a math formula (sqrt) Calculating a math formula (sqrt) Organizing values in a vector (sort) Organizing values in a vector (sort) A powerful way to solve complex problems A powerful way to solve complex problems Big task is broken into subtasks Big task is broken into subtasks Subtasks are coded into functions Subtasks are coded into functions Functions are tested individually Functions are tested individually Entire solution is assembled from functions Entire solution is assembled from functions
5
Polynomial Multiplication and Division The function conv(a,b) computes the product of the two polynomials described by the coefficient arrays a and b. The two polynomials need not be the same degree. The result is the coefficient array of the product polynomial. The function [q,r] = deconv(num,den) computes the result of dividing a numerator polynomial, whose coefficient array is num, by a denominator polynomial represented by the coefficient array den. The quotient polynomial is given by the coefficient array q, and the remainder polynomial is given by the coefficient array r. 2-48
6
Polynomial Multiplication and Division: Examples >>a = [9,-5,3,7]; >>b = [6,-1,2]; >>product = conv(a,b) product = 54 -39 41 29 -1 14 >>[quotient, remainder] = deconv(a,b) quotient = 1.5 -0.5833 remainder = 0 0 -0.5833 8.1667 2-49 More? See pages 107-109.
7
Polynomial Roots The function roots(a) computes the roots of a polynomial specified by the coefficient array a. The result is a column vector that contains the polynomial’s roots. For example, >>r = roots([2, 14, 20]) r = -2 -2 -7 -7 2-50 More? See page 107.
8
Polynomial Coefficients The function poly(r) computes the coefficients of the polynomial whose roots are specified by the vector r. The result is a row vector that contains the polynomial’s coefficients arranged in descending order of power. For example, >>c = poly([-2, -7]) c = 1 7 10 1 7 10 2-51 More? See page 107.
9
Plotting Polynomials The function polyval(a,x) evaluates a polynomial at specified values of its independent variable x, which can be a matrix or a vector. The polynomial’s coefficients of descending powers are stored in the array a. The result is the same size as x. 2-52
10
Example of Plotting a Polynomial To plot the polynomial f (x) = 9x 3 – 5x 2 + 3x + 7 for –2 ≤ x ≤ 5, you type >>a = [9,-5,3,7]; >>x = [-2:0.01:5]; >>f = polyval(a,x); >>plot(x,f),xlabel(’x’),ylabel(’f(x)’) 2-53 More? See pages 109-110.
13
How to calculate... 1 + 2 + 3 + 4 +... + N 1 + 2 + 3 + 4 +... + N where N is given by user. where N is given by user. – N=4 1 + 2 + 3 + 4 = 10 – N=8 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 We could do a loop (2 weeks) We could do a loop (2 weeks) Or... Or...
14
function s = rsum( n ) [n= ] if n > 0 s = n + sum(n-1); else s = 0; s = 0; [ s = ] [ s = ]
15
Recursion A function is defined recursively if it has the following two parts An anchor or base case An anchor or base case The function is defined for one or more specific values of the parameter(s) The function is defined for one or more specific values of the parameter(s) An inductive or recursive case An inductive or recursive case The function's value for current parameter(s) is defined in terms of previously defined function values and/or parameter(s) The function's value for current parameter(s) is defined in terms of previously defined function values and/or parameter(s) Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 15
16
Recursive Example Consider a recursive power function function p = pow(x, n) if n == 0 p = 1; else p = x * pow(x, n-1); end Consider a recursive power function function p = pow(x, n) if n == 0 p = 1; else p = x * pow(x, n-1); end Which is the anchor? Which is the anchor? Which is the inductive or recursive part? Which is the inductive or recursive part? How does the anchor keep it from going forever? How does the anchor keep it from going forever? Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 16
17
Recursive Example Note the results of a call Note the results of a call Recursive calls Recursive calls Resolution of the calls Resolution of the calls Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 17
18
A Bad Use of Recursion Fibonacci numbers 1, 1, 2, 3, 5, 8, 13, 21, 34 f 1 = 1, f 2 = 1 … f n = f n -2 + f n -1 Fibonacci numbers 1, 1, 2, 3, 5, 8, 13, 21, 34 f 1 = 1, f 2 = 1 … f n = f n -2 + f n -1 A recursive function function f = Fib(n) if n <= 2 A recursive function function f = Fib(n) if n <= 2 f = 1; else f = Fib(n – 1) + Fib(n – 2); f = 1; else f = Fib(n – 1) + Fib(n – 2); end end Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 18
19
A Bad Use of Recursion Why is this inefficient? Why is this inefficient? Note the recursion tree Note the recursion tree Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 19
20
Greatest Common Divisor gcd ( 36, 24 ) is 12 gcd ( 36, 24 ) is 12 gcd ( 100, 75) is 25, etc... gcd ( 100, 75) is 25, etc... A recursive algorithm A recursive algorithm Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 20
21
Palindrome Checker Palindrome checker Palindrome checker A palindrome has same value with characters reversed 1234321 racecar A palindrome has same value with characters reversed 1234321 racecar A vector is a palindrome if it reads the same reversed: A vector is a palindrome if it reads the same reversed: pal([ 1, 5, 6, 5, 1]) should say TRUE pal([ 1, 5, 6, 5, 1]) should say TRUE pal([ 1, 4, 6, 6, 5, 1]) should say FALSE pal([ 1, 4, 6, 6, 5, 1]) should say FALSE 21
22
Palindrome Checker Recursive algorithm to check a vector of numbers Recursive algorithm to check a vector of numbers If length of vector < = 1 return true If length of vector < = 1 return true Else check first and last values in vector v(1) and v(end) Else check first and last values in vector v(1) and v(end) if they do not match return false if they do not match return false If they match, check more bins/values in vector by making a recursive call on subvector from 2 to end-1 If they match, check more bins/values in vector by making a recursive call on subvector from 2 to end-1 22
23
Recursion Example: Towers of Hanoi Recursive algorithm especially appropriate for solution by recursion Recursive algorithm especially appropriate for solution by recursion Task Task Move disks from left peg to right peg Move disks from left peg to right peg When disk moved, must be placed on a peg When disk moved, must be placed on a peg Only one disk (top disk on a peg) moved at a time Only one disk (top disk on a peg) moved at a time Larger disk may never be placed on a smaller disk Larger disk may never be placed on a smaller disk Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 23
24
Recursion Example: Towers of Hanoi Identify base case: If there is one disk move from A to C Identify base case: If there is one disk move from A to C Inductive solution for n > 1 disks Inductive solution for n > 1 disks Move topmost n – 1 disks from A to B, using C for temporary storage Move topmost n – 1 disks from A to B, using C for temporary storage Move final disk remaining on A to C Move final disk remaining on A to C Move the n – 1 disk from B to C using A for temporary storage Move the n – 1 disk from B to C using A for temporary storage View code for solution, Fig 10.4 View code for solution, Fig 10.4Fig 10.4Fig 10.4 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 24
25
Recursion Example: Towers of Hanoi Note the graphical steps to the solution Note the graphical steps to the solution Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 25
26
Solution Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909- 3 26
27
Challenge – Calc change in postage stamps Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909- 3 27
28
How would you find Minimum number of stamps making up 65 cents (Letters are 42, postcards 28): Minimum number of stamps making up 65 cents (Letters are 42, postcards 28): remove 42, that's 23 left, too small for postcard so 24 stamps required. OR remove 42, that's 23 left, too small for postcard so 24 stamps required. OR remove 28, remove 28, 9 left over, so 11 stamps remove 28, remove 28, 9 left over, so 11 stamps Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909- 3 28
29
Recursive stampCount Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909- 3 29
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.