Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Functions ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem"— Presentation transcript:

1 1 Functions ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem http://www.cpe.ku.ac.th/~anan anan@cpe.ku.ac.th

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

3 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 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 2 + 1 [ 12 2 0 1 ]

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

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

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

8 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 9 Polynomial multiplication f(x) = x 2 – 3x + 2 g(x) = x 2 + 3x – 10 f(x) g(x) =x 4 – 17x 2 + 36x – 20 conv( [1 -3 2], [1 3 -10] ) = [ 1 0 -17 36 -20 ] >>a=[1 -3 2]; >>b=[1 3 -10]; >>conv(a,b) ans = [1 0 -17 36 -20] >>a=[1 -3 2]; >>b=[1 3 -10]; >>conv(a,b) ans = [1 0 -17 36 -20]

10 10 Polymonial division 17 3 = 5 + 2 3 remainder quotient

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

12 12 Plotting >>ar=[1 0 -16 0 0]; >>x=[-2:0.2:2]; >>R=polyval(ar,x); >>plot(x,R); >>xlable(‘x’) >>ylabel(‘r(x)’) >>title(‘Plotting r(x)’) >>ar=[1 0 -16 0 0]; >>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 13 Functions Input ? Black Box Some Mechanics Output

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

15 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 16 Functions Type of functions Built-in functions (predefined in MATLAB) User-defined functions (create your own function) Built-in functions

17 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 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 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 = [ 3.45 1.98 -2.16 ] >>ceil(x) ans = 4 2 -2 >>fix(x) ans = 3 1 -2 >>x = [ 3.45 1.98 -2.16 ] >>ceil(x) ans = 4 2 -2 >>fix(x) ans = 3 1 -2 >>round(x) ans = 3 2 -2 >>floor(x) ans = 3 1 -3 >>round(x) ans = 3 2 -2 >>floor(x) ans = 3 1 -3

20 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 21 Functions Type of functions Built-in functions (predefined in MATLAB) User-defined functions (create your own function) User-defined functions

22 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 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 24 Program file Program file (M-files) “.m ” Two types of M-Files Script file Function file

25 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 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 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 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 = 38 60 ans = 38 60 >>x=[4 10]; >>y=[15 20]; >>length2(x,y) L2 = 38 60 ans = 38 60 >>result = length(4,5) L2 = 18 result = 18 >>result = length(4,5) L2 = 18 result = 18

29 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 = 141.3717 >>r = 3; >>h = 5; >>z = volume(r,h) z = 141.3717 r h >>r = 3; >>h = 5; >>z = volume(h,r) z = 235.6194 >>r = 3; >>h = 5; >>z = volume(h,r) z = 235.6194 >>h = 3; >>r = 5; >>z = volume(r,h) z = 235.6194 >>h = 3; >>r = 5; >>z = volume(r,h) z = 235.6194


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

Similar presentations


Ads by Google