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

Slides:



Advertisements
Similar presentations
Complex Numbers in Engineering (Chapter 5 of Rattan/Klingbeil text)
Advertisements

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Arithmetic in Pascal (2) Arithmetic Functions Perform arithmetic calculations Gives an argument to the function and it returns the result.
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
Functions.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Builtin and user defined functions
4. Week 04.March Use of M-File Editor/Debugger: text editor, debugger; editor works with file types in addition to.m (MATLAB “m-files”)
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 MATLAB Programming Chapter 2.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
MATLAB An Introduction to MATLAB (Matrix Laboratory) 1.
Copyright © The McGraw-Hill Companies, Inc. Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 1 An Overview of MATLAB.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt.
Lecture 16: Sinusoidal Sources and Phasors Nilsson , App. B ENG17 : Circuits I Spring May 21, 2015.
1 Functions ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
MATLAB Trigonometry, Complex Numbers and Array Operations.
ENGR-25_Functions-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Registered Electrical.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Lecture 13: Complex Numbers and Sinusoidal Analysis Nilsson & Riedel Appendix B, ENG17 (Sec. 2): Circuits I Spring May 13, 2014.
Scientific Computing Introduction to Matlab Programming.
Introduction to Matlab
EE 1270 Introduction to Electric Circuits Suketu Naik 0 EE 1270: Introduction to Electric Circuits Lecture 17: 1) Sinusoidal Source 2) Complex Numbers.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Part 4 Chapter 16 Fourier Analysis PowerPoints organized by Prof. Steve Chapra, University All images copyright © The McGraw-Hill Companies, Inc. Permission.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Working with Arrays in MATLAB
Visual Basic I Programming
Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 3 Functions and Files PowerPoint to accompany Copyright © The McGraw-Hill.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Puff! The magic dragon, live by the tree… 第 3 章 檔案、函數、資料結構 files, functions, and data structures.
Digital Signal Processing – Chapter 10 Fourier Analysis of Discrete-Time Signals and Systems Dr. Ahmed Samir Fahmy Associate Professor Systems and Biomedical.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
Copyright © 2013, 2009, 2005 Pearson Education, Inc. 1 8 Complex Numbers, Polar Equations, and Parametric Equations.
Applied Circuit Analysis Chapter 12 Phasors and Impedance Copyright © 2013 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Chapter 2 Excel Fundamentals Logical IF (Decision) Statements Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
User Defined Functions Spring EE 201. Class Learning Objectives  Achieve Comprehension LOL of User Defined Functions. Spring
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. This work is only for non-profit use by instructors in courses for which this textbook has been adopted.
Arithmetic Operations
Sinusoidal Excitation of Circuits
Lesson 1: Phasors and Complex Arithmetic
Numeric, Cell and Structural Arrays One of the strenghts of MATLAB is the capabilty to handle collection of numbers called ARRAYS. MATLAB refers to scalars,
ECE 1304 Introduction to Electrical and Computer Engineering
Built-in MATLAB Functions Chapter 3
BIL 104E Introduction to Scientific and Engineering Computing
LECTURE #3 Complex Exponentials & Complex Numbers
ECE 1270: Introduction to Electric Circuits
ELEC 202 Circuit Analysis II
Introduction to MATLAB
Complex Numbers MAΘ
Complex Numbers in Engineering (Chapter 5 of Rattan/Klingbeil text)
Presentation transcript:

Copyright © 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

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

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

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)‏ 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 < More? See pages

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

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) More? See pages

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

You can use arrays as input arguments: >>r = fun([2:4],[7:9])‏ r =

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 =

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)

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

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:

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 )

For a great conceptual aid, see tutorial at 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

2 Forms of complex numbers Rectangular form: Rectangular form: 3 + 4i i 3 + 4i i 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)

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

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

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

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)

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

Operations with Complex Numbers >>x = i; >>y = 6 - 8i; >>mag_x = abs(x)‏ mag_x = >>mag_y = abs(y)‏ mag_y = >>mag_product = abs(x*y)‏ (continued …)‏

Operations with Complex Numbers (continued)‏ >>angle_x = angle(x)‏ angle_x = >>angle_y = angle(y)‏ angle_y = >>sum_angles = angle_x + angle_y sum_angles = >>angle_product = angle(x*y)‏ angle_product = More? See pages

Practice using abs( ) and angle( ) functions