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

Download Report

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

Functions
ผศ.ดร.อนันต์ ผลเพิม่
Anan Phonphoem
http://www.cpe.ku.ac.th/~anan
[email protected]
1
Overview


Polynomial review
Functions


Built-in functions
User-defined functions
2
Polynomial
f(x) = a1xn + a2xn-1 + a3xn-2 + …+ anx + an+1
Degree = Order =
n
Example:
y = 3x2 + 4
Order = 2
y = 12x3 + 2x2 + 1
Order = 3
3
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 ]
4
Polynomial Coefficient
[5 6 3 0 2]
y = 5x4 + 6x3 + 3x2 + 2
[ 4 -6 0 0 ]
b = 4a3 – 6a2
[1 1 1 1]
T = x3 + x2 + x + 1
5
Roots of polynomial
y = x2 – 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 ] )
6
Polynomial of roots
Roots of y = 1 , 2
(x – 1) (x – 2)
y = x2 – 3x + 2
>>r = [ 1
2 ];
>>poly(r)
ans
= 1
-3
2
>>P = poly([1 2])
7
Poly ( )
y = x2 – 3x + 2
[ 1 –3 2 ]
roots ( )
roots ( [1 –3 2 ] )
poly ( [1 2 ] )
[1 2]
(x – 1)(x – 2)
8
Polynomial multiplication
f(x) = x2 – 3x + 2
g(x) = x2 + 3x – 10
f(x) g(x) =
x4 – 17x2 + 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]
9
Polymonial division
quotient
remainder
17
2
=5 +
3
3
10
Polynomial division
f(x) = x3 – 4x2 + 2
f(x)
g(x)
>>a=[1 -4 0 2];
>>b=[1 3 -10];
>>deconv(a,b)
ans =
[1 -7]
g(x) = x2 + 3x – 10
x3 – 4x2 + 2
=
x2 + 3x – 10
31x – 68
= (x – 7) +
x2 + 3x – 10
>>[S,T] = deconv(a,b)
S =
1 -7
T =
0 0 31 -68
11
Plotting
r ( x)  x  16 x
4
2
-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)’)
12
Functions
Black Box
Input
?
Output
Some Mechanics
13
What function is it?
100
16
x
?
yy==sqrt(x)
f(x)
10
y4
Square root
14
What function is it?
x=
4y
x ,1
y=2
3
?
z=
z=
√f(x,y)
(x2+y2)
z z==2.2
5
-Multiple inputs
-Multiple outputs
-Complicate
15
Functions

Type of functions


Built-in functions (predefined in MATLAB)
User-defined functions (create your own function)
16
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)
17
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
x
Real Axis
18
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
>>ceil(x)
ans =
4
>>fix(x)
ans =
3
1.98
2
1
-2
-2
-2.16 ]
>>round(x)
ans =
3
>>floor(x)
ans =
3
2
-2
1
-3
19
Built-in functions (IV)

Trigonometric functions



Inverse trigonometric



sin(x)
cos(x)
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
20
Functions

Type of functions


Built-in functions (predefined in MATLAB)
User-defined functions
functions(create your own function)
User-defined
21
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
22
Operation modes in MATLAB
>>a
a =
[ 1
10 ]
>>b
b =
[ 2
>>a+b
ans =
[ 3
5 ]
15 ]
Interactive mode (Calculator)
%Example of program
%Program Test1.m
a = [ 1 10];
b = [ 2 5];
c = a + b
>>Test1
c =
[ 3
15 ]
Running a script file (Program)
23
Program file


Program file (M-files) “ .m ”
Two types of M-Files


Script file
Function file
24
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 !
25
User-defined function
x
Area.m
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(3)
y =
9
ans =
9
>>result = Area(2)
y =
4
result =
4
26
User-defined function
x
length.m
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(4)
L =
16
ans =
16
>>result = length(5)
L =
20
result =
20
27
User-defined function
x
length2.m
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
>>x=[4 10];
>>y=[15 20];
>>length2(x,y)
L2 =
38 60
ans =
38 60
>>result = length(4,5)
L2 =
18
result =
18
28
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
29