Ch03

advertisement
PowerPoint to accompany
Introduction to MATLAB 7
for Engineers
William J. Palm III
Chapter 3
Functions and
Files
Copyright © 2005. The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
AGENDA
What is a function?
How are they helpful in problem solving?
Writing a user-defined Matlab function
Using built in Matlab functions for doing complex math
Finding the min and max of a math curve using vector min
and max functions
Writing a complex valued function to compute a mechanical
system response
3-2
A MATLAB function is…


A package of code stored in a .m-file
Dedicated to a particular task, such as
Calculating a math formula (sqrt)
 Organizing values in a vector (sort)


A powerful way to solve complex problems
Big task is broken into subtasks
 Subtasks are coded into functions
 Functions are tested individually
 Entire solution is assembled from functions

Some common mathematical functions: Table 3.1–1
Exponential
exp(x)
Exponential; e x
sqrt(x)
Square root; x
Logarithmic
log(x)
Natural logarithm; ln x
log10(x)
Common (base 10) logarithm;
log x = log10 x
(continued…)
3-3
Some common mathematical functions (continued)
Complex
abs(x)
angle(x)
conj(x)
imag(x)
real(x)
Absolute value.
Angle of a complex number.
Complex conjugate.
Imaginary part of a complex number.
Real part of a complex number.
(continued…)
3-4
Some common mathematical functions
(continued)
Numeric
ceil(x)
fix(x)
floor(x)
round(x)
sign(x)
3-5
Round to nearest integer toward ∞.
Round to nearest integer toward zero.
Round to nearest integer toward -∞.
Round toward nearest integer.
Signum function:
+1 if x > 0; 0 if x = 0; -1 if x < 0.
More? See pages 142-148.
Last week we created circle.m


A SCRIPT file
just like running
matlab commands
only easier to modify
and rerun





Today we create
FUNCTION files
like SCRIPTs but with
more rules for getting
data into and out of
very modular
each function does
only one thing ideally
We begin with
circleFn.m
Our first User Defined Function
User-Defined Functions – how to make your own
The first line in a function file must begin with a function
definition line that has a list of inputs and outputs. This line
distinguishes a function M-file from a script M-file. Its syntax is
as follows:
function [output variables] = name(input variables)
Note that the output variables are enclosed in square
brackets, while the input variables must be enclosed with
parentheses. The function name (here, name) should be the
same as the file name in which it is saved (with the .m
extension).
3-18
More? See pages 148-149.
User-Defined Functions: Example
function z = fun(x,y)
u = 3*x;
z = u + 6*y.^2;
Note the use of a semicolon at the end of the lines. This
prevents the values of u and z from being displayed.
Note also the use of the array exponentiation operator
(.^). This enables the function to accept y as an array.
3-19
(continued …)
User-Defined Functions: Example (continued)
Call this function with its output argument:
>>z = fun(3,7)
z =
303
The function uses x = 3 and y = 7 to compute z.
(continued …)
3-20
You can use arrays as input arguments:
>>r = fun([2:4],[7:9])
r =
300
393
498
3-25
A function may have more than one output. These are
enclosed in square brackets.
For example, the function rectangle computes the area
A and perimeter P of a rectangle, given its length and
width as an input argument.
function [A, P] = rectangle(l,w)
A = l.*w;
% note .* for vector data
P = 2*l + 2*w;
3-26
The function is called as follows, if the length is 4 and
width is 2,
>>[A, P] = rectangle(4,2)
A =
8.0
P =
12.0
if you just want the area you can say:
>>A = rectangle(5,3);
A =
15.0
3-27
Using vector data with rectangle.m




suppose L = [1:0.1:10], W = [10:-0.1:1]
[A P] = rectangle(L, W);
find max area: max(A)
find max area and what index it is at:


[ m, kmax] = max(A)
find the L and W that gave max Area:
Lmax = L(kmax)
 Wmax = W(kmax)

Function Checklist
1)What is the name of the function?
2) What are the arguments?
3) What are the return values?
4) What is the first line of the function file?
5) How can you call (use) the function with the desired data?
Complex Numbers


Incorporate i, the square root of -1
Come about naturally in the quadratic
formula for calculating roots of a 2nd order
polynomial.
Vibration Analysis

Complex numbers are used to represent
sinusoidal quantities (in polar form, pairing
a magnitude with a phase)
They allow rapid calculations of sinewaves
phases and amplitude if they are all at the
same frequency
 Based on the famous Euler's formula:

The rectangular
and polar
representations
of the complex
number a + ib.
Figure 3.1–1
Engineers often use
degrees for polar
form and the
following format:
M < Ө a+bi
Ө = arctan(b/a)
a = Mcos(Ө)
b = Msin(Ө) and M = sqrt(a2+b2)
3-6
For a great conceptual aid, see tutorial at
http://www.picomonster.com/



Complex numbers allow us to model the
response of systems to sinusoidal
excitation. The complex valued "system
function" (response) codifies a gain factor
and a phase shift that applies to the input
signal.
Input signal is multiplied by complex
response to form output, a scaled, phaseshifted sinewave
System function varies with frequency
2 Forms of complex numbers

Rectangular form:
3 + 4i
 Real + Imag*i


-56 + 23i
or X + Yi
Polar form:

Mag < Phs
(in circuits Phs in degrees)
Converting from one form to
another

Polar to Rect:
X = M * cosd(Phs)
 Y = M * sind(Phs)


Rect to Polar
M = sqrt( X^2 + Y^2)
 Phs = atan2(Y,X) * 180/pi

 convert to deg
atan2 ?






Four quadrant arctangent
Perserves proper quadrant of angle
regular atan only gives results –pi/2 to pi/2
atan2(-5,3)
atan2(4, -2)
and so on...
Quadrant II
Quadrant IV
Rectangular to Polar in matlab

Suppose z = X + Yi

abs(z)
returns magnitude
angle(z) returns angle in radians

Polar to Rect in Matlab


Suppose we have a magnitude M and a
phase Q (in radians)
Z = M*cos(Q) + i*M*sin(Q)
Complex Arithmetic in Matlab

All work:
z1 + z2
 z1/z2
 z1*z2


according to the definition of complex
numbers
Operations with Complex Numbers
>>x = -3 + 4i;
>>y = 6 - 8i;
>>mag_x = abs(x)
mag_x =
5.0000
>>mag_y = abs(y)
mag_y =
10.0000
>>mag_product = abs(x*y)
50.0000
(continued …)
3-7
Operations with Complex Numbers (continued)
>>angle_x = angle(x)
angle_x =
2.2143
>>angle_y = angle(y)
angle_y =
-0.9273
>>sum_angles = angle_x + angle_y
sum_angles =
1.2870
>>angle_product = angle(x*y)
angle_product =
1.2870
3-8
More? See pages 143-145.
Practice using abs( ) and
angle( ) functions
Download