1 / 29

Functions

Functions. ผศ.ดร.อนันต์ ผลเพิ่ม Anan Phonphoem http://www.cpe.ku.ac.th/~anan anan@cpe.ku.ac.th. Overview. Polynomial review Functions Built-in functions User-defined functions. Order = 2. Order = 3. Polynomial. f(x) = a 1 x n + a 2 x n-1 + a 3 x n-2 + … + a n x + a n+1. n.

habib
Download Presentation

Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. Order = 2 Order = 3 Polynomial f(x) = a1xn + a2xn-1 + a3xn-2 + …+ anx + an+1 n Degree = Order = Example: y = 3x2 + 4 y = 12x3 + 2x2 + 1

  4. Polynomial Coefficient f(x) = a1xn + a2xn-1 + a3xn-2 + …+ anx + an+1 [ a1 a2 a3… an-1 an an+1 ] y = 12x3 + 2x2 + 1 [ 12 2 0 1 ]

  5. y = 5x4 + 6x3 + 3x2 + 2 b = 4a3– 6a2 T = x3 + x2 + x + 1 Polynomial Coefficient [ 5 6 3 0 2 ] [ 4 -6 0 0 ] [ 1 1 1 1 ]

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

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

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

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

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

  11. f(x) = x3– 4x2 + 2 g(x) = x2 + 3x – 10 x3– 4x2 + 2 f(x) g(x) = x2 + 3x – 10 31x – 68 = (x – 7) + x2 + 3x – 10 Polynomial division >>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

  12. Plotting -2 ≤ x ≤ 2 >>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)’)

  13. Black Box ? Output Input Some Mechanics Functions

  14. 100 16 x 10 4 y What function is it? ? y = f(x) y = sqrt(x) Square root

  15. x = 4 y = 3 x = 1 y = 2 x , y z = 2.2 z = 5 z z = √(x2+y2) What function is it? ? z = f(x,y) • 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 = ex • sqrt(x) = Squart root = √x • Logarithmic functions • log(x) = Natural log = ln x • log10(x) = Based 10 log = log10(x)

  18. Imaginary Axis x Real Axis 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

  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 >>round(x) ans = 3 2 -2 >>floor(x) ans = 3 1 -3

  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 = (ex+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. x x User-defined functions 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. >>a a = [ 1 10 ] >>b b = [ 2 5 ] >>a+b ans = [ 3 15 ] %Example of program %Program Test1.m a = [ 1 10]; b = [ 2 5]; c = a + b >>Test1 c = [ 3 15 ] Interactive mode (Calculator) Running a script file (Program) Operation modes in MATLAB

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

  27. x x User-defined function length.m %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(4) L = 16 ans = 16 >>result = length(5) L = 20 result = 20

  28. User-defined function x y length2.m %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 >>x=[4 10];>>y=[15 20]; >>length2(x,y) L2 = 38 60 ans = 38 60 >>result = length(4,5) L2 = 18 result = 18

  29. User-defined function r h volume.m %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 >>r = 3; >>h = 5; >>z = volume(r,h) z = 141.3717 >>r = 3; >>h = 5; >>z = volume(h,r) z = 235.6194 >>h = 3; >>r = 5; >>z = volume(r,h) z = 235.6194

More Related