Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2005. The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.

Similar presentations


Presentation on theme: "Copyright © 2005. The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm."— Presentation transcript:

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 3 Functions and Files PowerPoint to accompany

2 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

3 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

4 Exponential exp(x)‏ sqrt(x)‏ Logarithmic log(x)‏ log10(x) Exponential; e x Square root;  x Natural logarithm; ln x Common (base 10) logarithm; log x = log 10 x (continued…)‏ Some common mathematical functions: Table 3.1–1 3-3

5 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

6 Some common mathematical functions (continued)‏ Numeric ceil(x)‏ fix(x)‏ floor(x) round(x)‏ sign(x)‏ 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. 3-5More? See pages 142-148.

7 Last week we created circle.m A SCRIPT file A SCRIPT file just like running matlab commands only easier to modify and rerun just like running matlab commands only easier to modify and rerun Today we create FUNCTION files Today we create FUNCTION files like SCRIPTs but with more rules for getting data into and out of like SCRIPTs but with more rules for getting data into and out of very modular very modular each function does only one thing ideally each function does only one thing ideally We begin with circleFn.m We begin with circleFn.m

8 Our first User Defined Function

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

10 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 …)‏

11 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. 3-20 (continued …)‏

12 You can use arrays as input arguments: >>r = fun([2:4],[7:9])‏ r = 300 393 498 3-25

13 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

14 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

15 Using vector data with rectangle.m suppose L = [1:0.1:10], W = [10:-0.1:1] suppose L = [1:0.1:10], W = [10:-0.1:1] [A P] = rectangle(L, W); [A P] = rectangle(L, W); find max area: max(A) find max area: max(A) find max area and what index it is at: find max area and what index it is at: [ m, kmax] = max(A) [ m, kmax] = max(A) find the L and W that gave max Area: find the L and W that gave max Area: Lmax = L(kmax) Lmax = L(kmax) Wmax = W(kmax) Wmax = W(kmax)

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

17 Complex Numbers Incorporate i, the square root of -1 Incorporate i, the square root of -1 Come about naturally in the quadratic formula for calculating roots of a 2 nd order polynomial. Come about naturally in the quadratic formula for calculating roots of a 2 nd order polynomial.

18 Vibration Analysis Complex numbers are used to represent sinusoidal quantities (in polar form, pairing a magnitude with a phase) 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 They allow rapid calculations of sinewaves phases and amplitude if they are all at the same frequency Based on the famous Euler's formula: Based on the famous Euler's formula:

19 The rectangular and polar representations of the complex number a + ib. Figure 3.1–1 3-6 Engineers often use degrees for polar form and the following format: M < Ө  a+bi Ө = arctan(b/a) a = Mcos(Ө) b = Msin(Ө) and M = sqrt(a 2 +b 2 )

20 For a great conceptual aid, see tutorial at http://www.picomonster.com/ 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. 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, phase- shifted sinewave Input signal is multiplied by complex response to form output, a scaled, phase- shifted sinewave System function varies with frequency System function varies with frequency

21 2 Forms of complex numbers Rectangular form: Rectangular form: 3 + 4i -56 + 23i 3 + 4i -56 + 23i Real + Imag*i or X + Yi Real + Imag*i or X + Yi Polar form: Polar form: Mag < Phs (in circuits Phs in degrees) Mag < Phs (in circuits Phs in degrees)

22 Converting from one form to another Polar to Rect: Polar to Rect: X = M * cosd(Phs) X = M * cosd(Phs) Y = M * sind(Phs) Y = M * sind(Phs) Rect to Polar Rect to Polar M = sqrt( X^2 + Y^2) M = sqrt( X^2 + Y^2) Phs = atan2(Y,X) * 180/pi  convert to deg Phs = atan2(Y,X) * 180/pi  convert to deg

23 atan2 ? Four quadrant arctangent Four quadrant arctangent Perserves proper quadrant of angle Perserves proper quadrant of angle regular atan only gives results –pi/2 to pi/2 regular atan only gives results –pi/2 to pi/2 atan2(-5,3) Quadrant II atan2(-5,3) Quadrant II atan2(4, -2)Quadrant IV atan2(4, -2)Quadrant IV and so on... and so on...

24 Rectangular to Polar in matlab Suppose z = X + Yi Suppose z = X + Yi abs(z) returns magnitude abs(z) returns magnitude angle(z) returns angle in radians angle(z) returns angle in radians

25 Polar to Rect in Matlab Suppose we have a magnitude M and a phase Q (in radians) Suppose we have a magnitude M and a phase Q (in radians) Z = M*cos(Q) + i*M*sin(Q) Z = M*cos(Q) + i*M*sin(Q)

26 Complex Arithmetic in Matlab All work: All work: z1 + z2 z1 + z2 z1/z2 z1/z2 z1*z2 z1*z2 according to the definition of complex numbers according to the definition of complex numbers

27 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 3-7 (continued …)‏

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

29 Practice using abs( ) and angle( ) functions


Download ppt "Copyright © 2005. The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm."

Similar presentations


Ads by Google