C - How to program - Indiana University

Download Report

Transcript C - How to program - Indiana University

CPET 190
User Defined Functions
Lecture 12
Problem Solving with MATLAB
http://www.etcs.ipfw.edu/~lin
November 1, 2005
Lecture 12 - By Paul Lin
1
User-Defined M-File Functions
12-1 Intro to MATLAB Functions
12-2 Variable Passing (By Value)
November 1, 2005
Lecture 12 - By Paul Lin
2
User-Defined M-File Functions

Past 30-years Programming Practices
•
•
•
•

Reuse
Portability
Reliability
Maintainability
Programming Language and Practices
• Modular (Structured) Programming (1970 to late
1980s) – Fortran, C, Ada; (MATLAB 1990 – present)
• Object-Oriented Programming (1990s) – SmallTalk,
C++, Java
• Component-Based Programming (2000s) – C#, or
.NET
November 1, 2005
Lecture 12 - By Paul Lin
3
User-Defined M-File Functions

MATLAB Programming Language Features
(Modular and Structured)
• Top-down design – Module and subtasks
• Independent testing of function-level sub-tasks maintainability
• Reuse – 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 mistakes
November 1, 2005
Lecture 12 - By Paul Lin
4
Introduction to MATLAB
Functions


Types of M-File Functions
• MATLAB Built-In
Functions: abs(x), cos(x),
sin(x), max(x)
• User-Defined M-File
Functions (increase code
re-usability)
Functions
• Subroutines or
Procedures (providing
services)
• Function name,
arguments, return
arguments list (values)
• Function Calls
November 1, 2005
Lecture 12 - By Paul Lin
Input Data
Function
M File
Output Data
5
Introduction to MATLAB
Functions
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

November 1, 2005
Lecture 12 - By Paul Lin
6
MATLAB Built-In Functions

Toolbox\matlab\
• elefun folder
(elementary functions)
• elemat folder
(elementary matrix
function)
• general folder
• etc
November 1, 2005
Lecture 12 - By Paul Lin
7
MATLAB Built-In Functions


\toolbox\matlab\elefun
Elementary Math
Functions
• abs.m
• exp.m
• cos.m
• sin.m
• pow.m
November 1, 2005
Lecture 12 - By Paul Lin
8
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.
%
8-line Help Comments
% X = 1 2 3 becomes 3 2 1
%
456
654
An Example
%
% 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);
Error Checking
Return value
November 1, 2005
Lecture 12 - By Paul Lin
9
fliplr Function
If x = [1 2 3; 4 5 6]; or
=1 2 3
45 6
[m,n] = size(x)
m= 2, n = 3
y = x(:,n:-1:1);


x(:,
-- All rows remain unchanged
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 array
The 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.
y=3 2 1
6 5 4
November 1, 2005
Lecture 12 - By Paul Lin
10
View fliplr Function
>> dbtype fliplr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
%
456
654
%
% 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);
November 1, 2005
Lecture 12 - By Paul Lin
11
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

Hypotenuse – the side of a righttriangle that is opposite the right
angle (domain knowledge)

Function name – hypotenuse

Input arguments – a, b

Output arguments – h

The function hypotenuse.m
function h = hypotenuse(a, b)
h = sqrt(a.^2 + b.^2);
November 1, 2005
A
h
C
a
b
B
AC - Hypotenuse
Lecture 12 - By Paul Lin
12
Example 1 A User-Define Function
Example


Code the hyotense.m M-file function and save it under
cpet190/codes folder
Run the function: Click on Debug -> Run
Error message shows
November 1, 2005
Lecture 12 - By Paul Lin
13
Example 1 A User-Define Function
Example




Testing Functions:
First Testing:
hypotense(3,4)
Second Testing:
a = 3, b =4;
hypotenuse(a,b)
Third Testing:
side_a = 3; side_b = 4;
c = hypotenuse(side_a,
side_b)
November 1, 2005
Lecture 12 - By Paul Lin
14
Variable Passing (By Value)



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
Service requesting function cannot modify
the actual arguments
November 1, 2005
Lecture 12 - By Paul Lin
15
Example 2 Parallel Resistance
Function for calculating Parallel
resistance

Domain Knowledge:
Req = R1 || R2 = (R1 * R2)/(R1 + R2)

Function design
function Req = p_rs(r1, r2)
% Comments
%
if r1 < 0
Req = -1;
elseif r2 < 0
Req = -1;
elseif (r1 == 0) || (r2 == 0)
Req = 0;
else
Req = (r1 * r2)/(r1 + r2)
end

November 1, 2005
Lecture 12 - By Paul Lin
R1
R2
Rn
RT = xxx Ω
16
Example 2 Parallel Resistance


Coding
Documentation
•
•
•
•

Purposes
Calling sequence
Defining Variables
Record of revisions
Testing
• p_rs(10,10) -- 5 ohms
• p_rs(10, p_rs(20,20)) – 5
ohms
• p_rs(ra, rb)
November 1, 2005
Lecture 12 - By Paul Lin
17
Example 3 Rectangular-to-Polar
Conversion

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 point P(x,y) or P(r,
theta)
• Two functions for
converting between
rectangular coordinate
polar coordinate
November 1, 2005
Lecture 12 - By Paul Lin
Y Axis
P
y
r
q
x
X Axis
18
Example 3 Rectangular-to-Polar
Conversion
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
function [x, y] polar2rect(r, theta)
function [r, theta] rect2polar(x,y)

Convert equations to MATLAB statement

Code and test function

Add documentation to the two functions

November 1, 2005
Lecture 12 - By Paul Lin
19
Example 3 Rectangular-to-Polar
Conversion

The two functions
function [x, y] = polar2rect(r, theta)
x = r * cos(theta*pi/180);
y = r * sin(theta*pi/180);
end
November 1, 2005
Lecture 12 - By Paul Lin
20
Example 3 Rectangular-to-Polar
Conversion

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
November 1, 2005
Lecture 12 - By Paul Lin
21
Example 3 Rectangular-to-Polar
Conversion

Testing Functions

Testing Functions
>> [r, theta] = rect2polar(4,3)
r=
5
theta =
36.8699
>> [x, y] = polar2rect(5, 36.8699)
x=
4.0000
y=
3.0000
>> [r, theta] = rect2polar(-4,-3)
r=
5
theta =
-143.1301
>> [x, y] = polar2rect(5, -36.8699)
x=
4.0000
y=
-3.0000
November 1, 2005
Lecture 12 - By Paul Lin
22
Summary





Intro to MATLAB Functions
Variable Passing (By Value)
Example 1 – Function for Hypotenuse
calculation
Example 2 – Function for Parallel
Resistance computation
Example 3 – Function for rectangular to
polar conversion
November 1, 2005
Lecture 12 - By Paul Lin
23