Presentation is loading. Please wait.

Presentation is loading. Please wait.

November 1, 2005 Lecture 12 - By Paul Lin 1 CPET 190 User Defined Functions Lecture 12 Problem Solving with MATLAB

Similar presentations


Presentation on theme: "November 1, 2005 Lecture 12 - By Paul Lin 1 CPET 190 User Defined Functions Lecture 12 Problem Solving with MATLAB"— Presentation transcript:

1 November 1, 2005 Lecture 12 - By Paul Lin 1 CPET 190 User Defined Functions Lecture 12 Problem Solving with MATLAB http://www.etcs.ipfw.edu/~lin

2 November 1, 2005 Lecture 12 - By Paul Lin 2 User-Defined M-File Functions 12-1 Intro to MATLAB Functions 12-2 Variable Passing (By Value)

3 November 1, 2005 Lecture 12 - By Paul Lin 3 User-Defined M-File Functions Past 30-years Programming Practices Past 30-years Programming Practices ReuseReuse PortabilityPortability ReliabilityReliability MaintainabilityMaintainability Programming Language and Practices Programming Language and Practices Modular (Structured) Programming (1970 to late 1980s) – Fortran, C, Ada; (MATLAB 1990 – present)Modular (Structured) Programming (1970 to late 1980s) – Fortran, C, Ada; (MATLAB 1990 – present) Object-Oriented Programming (1990s) – SmallTalk, C++, JavaObject-Oriented Programming (1990s) – SmallTalk, C++, Java Component-Based Programming (2000s) – C#, or.NETComponent-Based Programming (2000s) – C#, or.NET

4 November 1, 2005 Lecture 12 - By Paul Lin 4 User-Defined M-File Functions MATLAB Programming Language Features (Modular and Structured) MATLAB Programming Language Features (Modular and Structured) Top-down design – Module and subtasksTop-down design – Module and subtasks Independent testing of function-level sub-tasks - maintainabilityIndependent testing of function-level sub-tasks - maintainability Reuse – reusability thorough packaging of functions, reduce programming efforts and increasing productivityReuse – reusability thorough packaging of functions, reduce programming efforts and increasing productivity Isolation from unintended side effects – reliability through data hiding, separate workspace for each function to avoid certain mistakesIsolation from unintended side effects – reliability through data hiding, separate workspace for each function to avoid certain mistakes

5 November 1, 2005 Lecture 12 - By Paul Lin 5 Introduction to MATLAB Functions Types of M-File Functions Types of M-File Functions MATLAB Built-In Functions: abs(x), cos(x), sin(x), max(x)MATLAB Built-In Functions: abs(x), cos(x), sin(x), max(x) User-Defined M-File Functions (increase code re-usability)User-Defined M-File Functions (increase code re-usability) Functions Functions Subroutines or Procedures (providing services)Subroutines or Procedures (providing services) Function name, arguments, return arguments list (values)Function name, arguments, return arguments list (values) Function CallsFunction Calls Function M File Input Data Output Data

6 November 1, 2005 Lecture 12 - By Paul Lin 6 Introduction to MATLAB Functions MATLAB Function Format MATLAB Function Format function [out_arg1, out_arg2, …] = function_name(in_arg1, in_arg2, …) % Comments lines % More comments Executable codes (return) -- Optional (end) -- For version 7.0 and newer

7 November 1, 2005 Lecture 12 - By Paul Lin 7 MATLAB Built-In Functions Toolbox\matlab\ Toolbox\matlab\ elefun folder (elementary functions)elefun folder (elementary functions) elemat folder (elementary matrix function)elemat folder (elementary matrix function) general foldergeneral folder etcetc

8 November 1, 2005 Lecture 12 - By Paul Lin 8 MATLAB Built-In Functions \toolbox\matlab\elefun \toolbox\matlab\elefun Elementary Math Functions Elementary Math Functions abs.mabs.m exp.mexp.m cos.mcos.m sin.msin.m pow.mpow.m

9 November 1, 2005 Lecture 12 - By Paul Lin 9 MATLAB fliplr Function function y = fliplr(x) %FLIPLR Flip matrix in left/right direction. % FLIPLR(X) returns X with row preserved and columns flipped % in the left/right direction. % % X = 1 2 3 becomes 3 2 1 % 4 5 6 6 5 4 % % See also FLIPUD, ROT90, FLIPDIM. % Copyright 1984-2002 The MathWorks, Inc. % $Revision: 5.9 $ $Date: 2002/04/08 20:21:05 $ if ndims(x)~=2, error('X must be a 2-D matrix.'); end [m,n] = size(x); y = x(:,n:-1:1); Return value Error Checking An Example 8-line Help Comments

10 November 1, 2005 Lecture 12 - By Paul Lin 10 fliplr Function If x = [1 2 3; 4 5 6]; or = 1 2 3 = 1 2 3 4 5 6 4 5 6 [m,n] = size(x) m= 2, n = 3 y = x(:,n:-1:1); y = x(:,n:-1:1); x(:, -- All rows remain unchanged x(:, -- All rows remain unchanged n:-1:1 -- Colon operator to access column elements n:-1:1 -- Colon operator to access column elements The meaning of first n is to copy the column-n of the x array, into the column-1 of the y arrayThe meaning of first n is to copy the column-n of the x array, into the column-1 of the y array The meaning of the :1 is to copy the column 1 of x into the very last column of yThe meaning of the :1 is to copy the column 1 of x into the very last column of y The meaning of the :-1 is to copy column n-1 of x into column n-1 of y; then decrement the column by -1 to copy the column n-2 of x into the column-2 of y; until all columns between n and 1 are copied.The meaning of the :-1 is to copy column n-1 of x into column n-1 of y; then decrement the column by -1 to copy the column n-2 of x into the column-2 of y; until all columns between n and 1 are copied. y = 3 2 1 6 5 4 6 5 4

11 November 1, 2005 Lecture 12 - By Paul Lin 11 View fliplr Function >> dbtype fliplr 1 function y = fliplr(x) 2 %FLIPLR Flip matrix in left/right direction. 3 % FLIPLR(X) returns X with row preserved and columns flipped 4 % in the left/right direction. 5 % 6 % X = 1 2 3 becomes 3 2 1 7 % 4 5 6 6 5 4 8 % 9 % See also FLIPUD, ROT90, FLIPDIM. 10 11 % Copyright 1984-2002 The MathWorks, Inc. 12 % $Revision: 5.9 $ $Date: 2002/04/08 20:21:05 $ 13 14 if ndims(x)~=2, error('X must be a 2-D matrix.'); end 15 [m,n] = size(x); 16 y = x(:,n:-1:1);

12 November 1, 2005 Lecture 12 - By Paul Lin 12 Example 1 A User-Define Function Example Phases of Function Development - Designing phase - Coding phase - Testing Phase - Release and Implementation The Desired Function Calculating the Hypotenuse Calculating the Hypotenuse Hypotenuse – the side of a right- triangle that is opposite the right angle (domain knowledge) Hypotenuse – the side of a right- triangle that is opposite the right angle (domain knowledge) Function name – hypotenuse Function name – hypotenuse Input arguments – a, b Input arguments – a, b Output arguments – h Output arguments – h The function hypotenuse.m The function hypotenuse.m function h = hypotenuse(a, b) h = sqrt(a.^2 + b.^2); A BC AC - Hypotenuse h a b

13 November 1, 2005 Lecture 12 - By Paul Lin 13 Example 1 A User-Define Function Example Code the hyotense.m M-file function and save it under cpet190/codes folder Code the hyotense.m M-file function and save it under cpet190/codes folder Run the function: Click on Debug -> Run Run the function: Click on Debug -> Run Error message shows Error message shows

14 November 1, 2005 Lecture 12 - By Paul Lin 14 Example 1 A User-Define Function Example Testing Functions: Testing Functions: First Testing: First Testing:hypotense(3,4) Second Testing: Second Testing: a = 3, b =4; a = 3, b =4; hypotenuse(a,b) hypotenuse(a,b) Third Testing: Third Testing: side_a = 3; side_b = 4; side_a = 3; side_b = 4; c = hypotenuse(side_a, side_b) c = hypotenuse(side_a, side_b)

15 November 1, 2005 Lecture 12 - By Paul Lin 15 Variable Passing (By Value) MATLAB programs communicate with their functions – values passing for both arrays and scalars MATLAB programs communicate with their functions – values passing for both arrays and scalars Make a copy of the actual arguments and passes them to the function Make a copy of the actual arguments and passes them to the function Service requesting function cannot modify the actual arguments Service requesting function cannot modify the actual arguments

16 November 1, 2005 Lecture 12 - By Paul Lin 16 Example 2 Parallel Resistance Function for calculating Parallel resistance Function for calculating Parallel resistance Domain Knowledge: Domain Knowledge: Req = R1 || R2 = (R1 * R2)/(R1 + R2) Function design Function design function Req = p_rs(r1, r2) function Req = p_rs(r1, r2) % Comments % if r1 < 0 Req = -1; Req = -1; elseif r2 < 0 Req = -1; Req = -1; elseif (r1 == 0) || (r2 == 0) Req = 0; Req = 0;else Req = (r1 * r2)/(r1 + r2) Req = (r1 * r2)/(r1 + r2)end

17 November 1, 2005 Lecture 12 - By Paul Lin 17 Example 2 Parallel Resistance Coding Coding Documentation Documentation PurposesPurposes Calling sequenceCalling sequence Defining VariablesDefining Variables Record of revisionsRecord of revisions Testing Testing p_rs(10,10) -- 5 ohmsp_rs(10,10) -- 5 ohms p_rs(10, p_rs(20,20)) – 5 ohmsp_rs(10, p_rs(20,20)) – 5 ohms p_rs(ra, rb)p_rs(ra, rb)

18 November 1, 2005 Lecture 12 - By Paul Lin 18 Example 3 Rectangular-to-Polar Conversion Problem Statement Problem Statement The location of a point in a cartesian plane can be expressed in either the rectangular coordinates(x,y) or the polar coordinates(r, theta) as shown on the slide.The location of a point in a cartesian plane can be expressed in either the rectangular coordinates(x,y) or the polar coordinates(r, theta) as shown on the slide. The point P(x,y) or P(r, theta)The point P(x,y) or P(r, theta) Two functions for converting between rectangular coordinate polar coordinateTwo functions for converting between rectangular coordinate polar coordinate

19 November 1, 2005 Lecture 12 - By Paul Lin 19 Example 3 Rectangular-to-Polar Conversion Domain Knowledge Domain Knowledge x = r cos(theta) y= r sin(theta) r = sqrt(x^2 + y^2) theta = tan -1 (y/x) Define the function’s name, inputs and outputs Define the function’s name, inputs and outputs function [x, y] polar2rect(r, theta) function [r, theta] rect2polar(x,y) Convert equations to MATLAB statement Convert equations to MATLAB statement Code and test function Code and test function Add documentation to the two functions Add documentation to the two functions

20 November 1, 2005 Lecture 12 - By Paul Lin 20 Example 3 Rectangular-to-Polar Conversion The two functions The two functions function [x, y] = polar2rect(r, theta) x = r * cos(theta*pi/180); y = r * sin(theta*pi/180); end

21 November 1, 2005 Lecture 12 - By Paul Lin 21 Example 3 Rectangular-to-Polar Conversion The two functions The two functions function [r, theta] = rect2polar(x,y) %ATAN2 Four quadrant inverse tangent. % ATAN2(Y,X) is the four quadrant arctangent of the % real parts of the elements of X and Y. % -pi <= ATAN2(Y,X) <= pi. r = sqrt(x^2 + y^2); theta = (180/pi)* atan2(y,x); end

22 November 1, 2005 Lecture 12 - By Paul Lin 22 Example 3 Rectangular-to-Polar Conversion Testing Functions Testing Functions >> [x, y] = polar2rect(5, 36.8699) x = 4.0000 4.0000 y = 3.0000 3.0000 >> [x, y] = polar2rect(5, -36.8699) x = 4.0000 4.0000 y = -3.0000 -3.0000 Testing Functions Testing Functions >> [r, theta] = rect2polar(4,3) r = 5 theta = 36.8699 36.8699 >> [r, theta] = rect2polar(-4,-3) r = 5 theta = -143.1301 -143.1301

23 November 1, 2005 Lecture 12 - By Paul Lin 23 Summary Intro to MATLAB Functions Intro to MATLAB Functions Variable Passing (By Value) Variable Passing (By Value) Example 1 – Function for Hypotenuse calculation Example 1 – Function for Hypotenuse calculation Example 2 – Function for Parallel Resistance computation Example 2 – Function for Parallel Resistance computation Example 3 – Function for rectangular to polar conversion Example 3 – Function for rectangular to polar conversion


Download ppt "November 1, 2005 Lecture 12 - By Paul Lin 1 CPET 190 User Defined Functions Lecture 12 Problem Solving with MATLAB"

Similar presentations


Ads by Google