1 Functions ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem

Slides:



Advertisements
Similar presentations
Arithmetic in Pascal (2) Arithmetic Functions Perform arithmetic calculations Gives an argument to the function and it returns the result.
Advertisements

CMPS 1371 Introduction to Computing for Engineers FUNCTIONS.
Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,
DEDiscover Differential Equation Modeling Solution CANGLIN WU June
Functions and methods A method is a function that is a member of a class A method is a function that is a member of a class FCL(Framework Class Library)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
1 A Portable Scientific Visualization Program: GnuPlot Asst. Prof. Emin Korkut.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
8.3 Solving Right Triangles
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.
By: Kelley Borgard Block 4A
1 Chapter 1 Preliminaries Functions and Their Graphs.
Builtin and user defined functions
Predefined MATLAB Functions ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
Foundations Basics: Notation, and other things Algebraic manipulations Indices, Logs, Roots and Surds Binomial expansion Trigonometric functions Trigonometric.
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 MATLAB Programming Chapter 2.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Chapter 2 Numeric, Cell, and Structure Arrays. Physics Connection - Specification of a position vector using Cartesian coordinates. Figure 2.1–1 2-2 The.
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.
Matlab for Engineers Built-in Matlab Functions Chapter 3.
Lecture 1 - Introduction June 3, 2002 CVEN 302. Lecture’s Goals General Introduction to CVEN Computer Applications in Engineering and Construction.
STANDARD FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Jeopardy Math Review Unit 1: Functions and Their Graphs Unit 2: Polynomial and Rational Functions Unit 3: Exponential and Logarithmic Functions Unit 4:
Functions An Overview. Functions A function is a procedure for assigning a single output to any acceptable input. Functions can be written as formulas,
ENGR-25_Functions-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Registered Electrical.
HCMUT – DEP. OF MATH. APPLIED LEC 2b: BASIC ELEMENTARY FUNCTIONS Instructor: Dr. Nguyen Quoc Lan (October, 2007)
1 Programming with MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
Introduction to Matlab
Visual Basic I Programming
Inverse Trigonometric
Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 3 Functions and Files PowerPoint to accompany Copyright © The McGraw-Hill.
MATLAB Tutorial EE313 Signals and Systems Created: Thursday Jan 25, 2007 Rayyan Jaber Modified by: Jeff Andrews.
Puff! The magic dragon, live by the tree… 第 3 章 檔案、函數、資料結構 files, functions, and data structures.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
PH15720 Laboratory Techniques - An Introduction to MATHCAD.
MATLAB Lecture 1 염익준. Introduction MATLAB (MATrix LABoratory) a special purpose computer program optimized to perform engineering and scientific calculations.
Prof. K. V. Mali, Sinhgad College of Engineering, Pune-41.
Introduction to MATLAB ENGR 1181 MATLAB 1. MATLAB Built-in Math Functions  Pre-defined in MATLAB ready for use exp(x) – exponential (e x ) log(x) – natural.
Section 3.5 Trigonometric Functions Section 3.5 Trigonometric Functions.
8-3 Trigonometry Part 2: Inverse Trigonometric Functions.
Basic operations in Matlab EE 201.  Achieve Comprehension LOL of Matlab basic operations. Class Learning Objectives 2.
Simple C Programs.
Part(2) MATLAB.
Arithmetic Operations
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
Chapter 1 Preliminaries.
Matlab Introduction.
Built-in MATLAB Functions Chapter 3
CPSC Pascal Brent M. Dingle Texas A&M University Chapter 3
BIL 104E Introduction to Scientific and Engineering Computing
Computer programming Dr. Ivan A. Hashim.
Computer Simulation Lab
Math Library and IO formatting
محاسبات عددی در مهندسی پزشکی جلسه اول و دوم مقدمه ای بر نرم افزار MATLAB گلناز بغدادی 1391.
§1.5 Inverse and log functions
Introduction to MATLAB
Objective: To be able to create script and function files
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
Complex Number.
Computer Simulation Lab
Section 4.7.
Presentation transcript:

1 Functions ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem

2 Overview Polynomial review Functions Built-in functions User-defined functions

3 Polynomial Example: f(x) = a 1 x n + a 2 x n-1 + a 3 x n-2 + … + a n x + a n+1 Degree = Order = n y = 3x 2 + 4Order = 2 y = 12x 3 + 2x 2 + 1Order = 3

4 Polynomial Coefficient f(x) = a 1 x n + a 2 x n-1 + a 3 x n-2 + … + a n x + a n+1 [ a 1 a 2 a 3 … a n-1 a n a n+1 ] y = 12x 3 + 2x [ ]

5 Polynomial Coefficient [ ] [ ] [ ] y = 5x 4 + 6x 3 + 3x b = 4a 3 – 6a 2 T = x 3 + x 2 + x + 1

6 Roots of polynomial y = x 2 – 3x + 2 = (x – 1) (x – 2) Roots of y 1, 2 a = [ ] c = roots(a) = [ 1 2 ] T = roots( [ ] )

7 Polynomial of roots Roots of y = 1, 2 y = x 2 – 3x + 2 (x – 1) (x – 2) >>r = [ 1 2 ]; >>poly(r) ans = >>P = poly([1 2])

8 Poly ( ) roots ( ) y = x 2 – 3x + 2 roots ( [1 – 3 2 ] ) [ 1 2 ] [ 1 – 3 2 ](x – 1)(x – 2) poly ( [1 2 ] )

9 Polynomial multiplication f(x) = x 2 – 3x + 2 g(x) = x 2 + 3x – 10 f(x) g(x) =x 4 – 17x x – 20 conv( [1 -3 2], [ ] ) = [ ] >>a=[1 -3 2]; >>b=[ ]; >>conv(a,b) ans = [ ] >>a=[1 -3 2]; >>b=[ ]; >>conv(a,b) ans = [ ]

10 Polymonial division 17 3 = remainder quotient

11 Polynomial division f(x) = x 3 – 4x 2 + 2g(x) = x 2 + 3x – 10 f(x) g(x) = x 3 – 4x x 2 + 3x – 10 =(x – 7) + 31x – 68 x 2 + 3x – 10 >>a=[ ]; >>b=[ ]; >>deconv(a,b) ans = [1 -7] >>a=[ ]; >>b=[ ]; >>deconv(a,b) ans = [1 -7] >>[S,T] = deconv(a,b) S = 1 -7 T = >>[S,T] = deconv(a,b) S = 1 -7 T =

12 Plotting >>ar=[ ]; >>x=[-2:0.2:2]; >>R=polyval(ar,x); >>plot(x,R); >>xlable(‘x’) >>ylabel(‘r(x)’) >>title(‘Plotting r(x)’) >>ar=[ ]; >>x=[-2:0.2:2]; >>R=polyval(ar,x); >>plot(x,R); >>xlable(‘x’) >>ylabel(‘r(x)’) >>title(‘Plotting r(x)’) -2 ≤ x ≤ 2

13 Functions Input ? Black Box Some Mechanics Output

14 x What function is it? ? Square root y = f(x) y = sqrt(x) y

15 x, y What function is it? ? z = f(x,y) z x = 4 y = 3 z = 5 x = 1 y = 2 z = 2.2 z = √ (x 2 +y 2 ) -Multiple inputs -Multiple outputs -Complicate

16 Functions Type of functions Built-in functions (predefined in MATLAB) User-defined functions (create your own function) Built-in functions

17 Built-in functions (I) Exponential functions exp(x) = Exponential = e x sqrt(x) = Squart root = √ x Logarithmic functions log(x) = Natural log = ln x log10(x) = Based 10 log = log 10 (x)

18 Built-in functions (II) Complex number functions abs(x) = Absolute x = |x| imag(x) = imaginary part of x real(x) = real part of x angle(x) =angle of x x = a + ib Imaginary Axis Real Axis x

19 Built-in functions (III) Numeric functions ceil(x) = Round toward  fix(x) = Round toward 0 round(x) = Round toward nearest integer Floor(x) = Round toward nearest –  >>x = [ ] >>ceil(x) ans = >>fix(x) ans = >>x = [ ] >>ceil(x) ans = >>fix(x) ans = >>round(x) ans = >>floor(x) ans = >>round(x) ans = >>floor(x) ans =

20 Built-in functions (IV) Trigonometric functions sin(x) cos(x) Inverse trigonometric acos(x) = arccos x = cos – 1 x atan(x) = arctan x = tan – 1 x Hyperbolic functions cosh(x) = Hyperbolic cosine = cosh x = (e x +e – x )/2

21 Functions Type of functions Built-in functions (predefined in MATLAB) User-defined functions (create your own function) User-defined functions

22 User-defined functions x x Write a function to find area of the field Area = x * x If we want to install a fence around the field Write a function to find the length of the fence L = 4 * x

23 Operation modes in MATLAB >>a a = [ 1 10 ] >>b b = [ 2 5 ] >>a+b ans = [ 3 15 ] >>a a = [ 1 10 ] >>b b = [ 2 5 ] >>a+b ans = [ 3 15 ] Interactive mode (Calculator) %Example of program %Program Test1.m a = [ 1 10]; b = [ 2 5]; c = a + b %Example of program %Program Test1.m a = [ 1 10]; b = [ 2 5]; c = a + b >>Test1 c = [ 3 15 ] >>Test1 c = [ 3 15 ] Running a script file (Program)

24 Program file Program file (M-files) “.m ” Two types of M-Files Script file Function file

25 Function file format First line must begin with a function definition function [outputs] = function_name(inputs) Function name should be the same as.m file function [outputs] = Test1(inputs) Should be save as “Test1.m” MATLAB is case sensitive !

26 User-defined function x x %This is function to find area of a field function y = Area(x) %Area of square box is x*x y = x * x %This is function to find area of a field function y = Area(x) %Area of square box is x*x y = x * x Area.m >>Area(3) y = 9 ans = 9 >>Area(3) y = 9 ans = 9 >>result = Area(2) y = 4 result = 4 >>result = Area(2) y = 4 result = 4

27 User-defined function x x %This is function to find the length of the fence function L = length(x) %Length of the fence is 4*x L = 4 * x %This is function to find the length of the fence function L = length(x) %Length of the fence is 4*x L = 4 * x length.m >>length(4) L = 16 ans = 16 >>length(4) L = 16 ans = 16 >>result = length(5) L = 20 result = 20 >>result = length(5) L = 20 result = 20

28 User-defined function y x %This is function to find the length of the fence function L2 = length2(x,y) %Length of the fence is 2x+2y L2 = 2.*x + 2.*y %This is function to find the length of the fence function L2 = length2(x,y) %Length of the fence is 2x+2y L2 = 2.*x + 2.*y length2.m >>x=[4 10]; >>y=[15 20]; >>length2(x,y) L2 = ans = >>x=[4 10]; >>y=[15 20]; >>length2(x,y) L2 = ans = >>result = length(4,5) L2 = 18 result = 18 >>result = length(4,5) L2 = 18 result = 18

29 User-defined function %This is function to find the volume function v = volume(r,h) %Length of the fence is pi*r*r*h area = pi.* (r.^2); v = area.* h %This is function to find the volume function v = volume(r,h) %Length of the fence is pi*r*r*h area = pi.* (r.^2); v = area.* h volume.m >>r = 3; >>h = 5; >>z = volume(r,h) z = >>r = 3; >>h = 5; >>z = volume(r,h) z = r h >>r = 3; >>h = 5; >>z = volume(h,r) z = >>r = 3; >>h = 5; >>z = volume(h,r) z = >>h = 3; >>r = 5; >>z = volume(r,h) z = >>h = 3; >>r = 5; >>z = volume(r,h) z =