Jan 16, 2016
2
Overview
Polynomial review Functions
Built-in functions User-defined functions
3
Polynomial
Example:
f(x) = a1xn + a2xn-1 + a3xn-2 + …+ anx + an+1
Degree = Order =
n
y = 3x2 + 4 Order = 2
y = 12x3 + 2x2 + 1 Order = 3
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
Polynomial Coefficient
[ 5 6 3 0 2 ]
[ 4 -6 0 0 ]
[ 1 1 1 1 ]
y = 5x4 + 6x3 + 3x2 + 2
b = 4a3 – 6a2
T = x3 + x2 + x + 1
6
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 ] )
7
Polynomial of roots
Roots of y = 1 , 2
y = x2 – 3x + 2
(x – 1) (x – 2)
>>r = [ 1 2 ];
>>poly(r)ans = 1 -3 2
>>P = poly([1 2])
8
Poly ( ) roots ( )
y = x2 – 3x + 2 roots ( [1 –3 2 ] ) [ 1 2 ]
[ 1 –3 2 ] (x – 1)(x – 2)poly ( [1 2 ] )
9
Polynomial multiplicationf(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]
>>a=[1 -3 2];>>b=[1 3 -10];>>conv(a,b)ans = [1 0 -17 36 -20]
10
Polymonial division
173 = 5 + 2
3
remainderquotient
11
Polynomial division
f(x) = x3 – 4x2 + 2 g(x) = x2 + 3x – 10
f(x) g(x)
= x3 – 4x2 + 2
x2 + 3x – 10
= (x – 7) + 31x – 68
x2 + 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 -7T = 0 0 31 -68
>>[S,T] = deconv(a,b)S = 1 -7T = 0 0 31 -68
12
Plotting
24 16)( xxxr
>>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
Functions
Input ?
Black Box
Some Mechanics
Output
14
x
What function is it?
?Square root
y = f(x)y = sqrt(x) y100 1016 4
15
x , y
What function is it?
?z = f(x,y) zx = 4y = 3
z = 5x = 1y = 2
z = 2.2z = √(x2+y2)
-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
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 = [ 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
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
User-defined functions
x
x
Write a function to find area of the fieldArea = x * x
If we want to install a fence around the fieldWrite a function to find the length of the fenceL = 4 * x
23
Operation modes in MATLAB
>>aa = [ 1 10 ]>>bb = [ 2 5 ]>>a+bans = [ 3 15 ]
>>aa = [ 1 10 ]>>bb = [ 2 5 ]>>a+bans = [ 3 15 ]
Interactive mode (Calculator)
%Example of program%Program Test1.ma = [ 1 10];b = [ 2 5];c = a + b
%Example of program%Program Test1.ma = [ 1 10];b = [ 2 5];c = a + b
>>Test1c = [ 3 15 ]
>>Test1c = [ 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
filefunction [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 fieldfunction y = Area(x)%Area of square box is x*xy = x * x
%This is function to find area of a fieldfunction y = Area(x)%Area of square box is x*xy = x * x
Area.m
>>Area(3)y = 9ans = 9
>>Area(3)y = 9ans = 9
>>result = Area(2)y = 4result = 4
>>result = Area(2)y = 4result = 4
27
User-defined function
x
x
%This is function to find the length of the fencefunction L = length(x)%Length of the fence is 4*xL = 4 * x
%This is function to find the length of the fencefunction L = length(x)%Length of the fence is 4*xL = 4 * x
length.m
>>length(4)L = 16ans = 16
>>length(4)L = 16ans = 16
>>result = length(5)L = 20result = 20
>>result = length(5)L = 20result = 20
28
User-defined function
y
x
%This is function to find the length of the fencefunction L2 = length2(x,y)%Length of the fence is 2x+2yL2 = 2.*x + 2.*y
%This is function to find the length of the fencefunction L2 = length2(x,y)%Length of the fence is 2x+2yL2 = 2.*x + 2.*y
length2.m
>>x=[4 10];>>y=[15 20];>>length2(x,y)L2 = 38 60ans = 38 60
>>x=[4 10];>>y=[15 20];>>length2(x,y)L2 = 38 60ans = 38 60
>>result = length(4,5)L2 = 18result = 18
>>result = length(4,5)L2 = 18result = 18
29
User-defined function
%This is function to find the volumefunction v = volume(r,h)%Length of the fence is pi*r*r*harea = pi .* (r.^2);v = area .* h
%This is function to find the volumefunction v = volume(r,h)%Length of the fence is pi*r*r*harea = 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