Sie sind auf Seite 1von 20

ENGINEERING PROBLEM

SOLVING WITH MATLAB


AND MAPLE
MODULE 5
POLYNOMIAL , CURVE
FITTING AND INTERPOLATION
2017
Polynomials are mathematical expressions that are frequently used
for problem solving and modeling in science and engineering. In
many cases an equation that is written in the process of solving a
problem is a polynomial, and the solution of the problem is the zero
off the
th polynomial.
l i l
MATLAB has a wide selection functions that are specifically
d i d for
designed f handling
h dli polynomials.
l i l
Curve fitting is process of finding a function that can be used to
model data. The function does not necessarily pass through any of
the points, but models the data with the smallest possible error.
MATLAB provides different type of equations fro curve fitting:
polynomial, exponential, logarithmic, power functions, etc.
Interpolation is the process of estimating values between data points.
The simplest kind of interpolation is done by drawing a straight line
between the points. In a more sophisticated interpolation, data from
2
additional points is used. MATLAB
1. Polynomials

Values of a Polynomial
The value of a polynomial at a point x can be calculated with the
function ployval that has the form:

x can also be a vector. 3


MATLAB
Example
>> p=[2
[2 -3
3 5 -1]
1]
>> polyval(p,1)
>> x=0:0.01:2;
x 0:0.01:2;
>> y=polyval(p,x);
>> plot(x,y)

Roots of a Polynomial
Roots off a polynomial
R l i l are the
h values
l off the
h argument for
f which
hi h the
h
value of the polynomial is equal to zero.

>> roots(p)
>> roots([1 -3 5 -2 4])
4
MATLAB
When the roots of a polynomial are known, the ploy command can
be used for determining the coefficients of the polynomial.

Example
>> r=[1 -2 -1]
>> p=poly(r)

Addition and Subtraction of Polynomials


Two polynomials can be added or subtracted by adding the vectors of
thee coe
coefficients.
c e s. If thee po
polynomials
y o s aree not
o oof thee ssamee o
order,
de , thee
shorter vector has to be modified to be of the same length as the
longer vector by adding zeros.
5
MATLAB
Example
>> p1=[2 -1
1.5
5 3 -4
4 5 7 2];
>> p2=[4 -5 6 -1];
>> p2m=[0 0 0 p
p p2];
>> s=p1+p2m
>> d=p1-p2m
Multiplication of Polynomials
Two p
polynomials
y can be multiplied
p with MATLAB built-in function
conv.

The two p
polynomials
y do not have to be of the same order.
6
MATLAB
Example
>> p=conv(p1
p=conv(p1,p2)
p2)

Division of Polynomials
A polynomial can be divided by another polynomial with MATLAB
built-in function deconv.

Example
p
>> [q,r]=deconv(p1,p2)

7
MATLAB
Derivatives of Polynomials
The built
built-in
in function polyder can be used to calculate the
derivative of a single polynomial, a product of two polynomials and a
quotient of two polynomials.

8
MATLAB
2. Curve Fitting
Curve Fitting with Polynomials
Curve fitting with polynomials is done in MATLAB with the
polyfit functions,
functions which uses the least squares method.
method

Example
It is expected that the average percentage yield, y , from a chemical
process is linearly related to the process temperature, x, in 0C.

9
MATLAB
>> x=[45:5:90];
>> y=[43 45 48 51 55 57 59 63 66 68];
>> p=polyfit(x,y,1);
>> yp=polyval(p,x);
>> plot(x y 'o' x yp)
plot(x,y,'o',x,yp)
Curve Fitting with other Functions

10
MATLAB
The result of polyfit function is assigned to p, which is a two
element vector.
vector The first element of the vector is the constant m and
the second element is b.

3 Interpolation
3. I t l ti
One-dimensional interpolation in MATLAB is done with the
interpl function,
function which has the form:

11
MATLAB
• The vector x must be monotonic (with elements in ascending or
descending order).
order)

• xi can be a scalar (interpolation of one point) or a vector


(interpolation of many points). yi is a scalar or a vector with the
corresponding interpolated values.
MATLAB can do the interpolation using one of several methods
that can be specified. These methods include:
'nearest’: returns the value of the data point that is nearest to
the interpolated point.
‘linear’: uses linear spline interpolation.
'spline’: uses cubic spline interpolation.
'pchip’: uses piecewise cubic Hermite interpolation, also called
'cubic'
12
MATLAB
POLYNOMIAL , CURVE
FITTING AND INTERPOLATION
1. Differentiation
You can use diff to differentiate symbolic expressions.
expressions
>> diff('x^3-3*x^2+4*x-2')
>> diff('x^3*y+5*y^2*x^2-7*y^3*x-9','y')
>> diff('sin(x)')
>> syms y
>> diff(cos(y))
>> diff(log(y))
( p( y) ( y))
>> diff(exp(2*y)*sin(3*y))

The syntax for second derivatives is diff(f(x), 2), and for nth
d i i
derivatives, diff(f(x), n).
>> diff('x^3-3*x^2+4*x-2',2)
>> diff('x^3-3*x^2+4*x-2',3)
14
MATLAB
2. Integration
MATLAB can compute definite
d fi i andd indefinite
i d fi i integrals.
i l
>> int('x^2+2*x-3')
>> int('y*x^3+y^2*x^2-6*y+8','y')
>> int(
int('y*x^3+y^2*x^2-6*y+8'
y x 3+y 2 x 2 6 y+8 ,‘x')
x )
>> syms z >> int(log(z))
>> int(exp(2*z)*cos(3*z))
>> int('x^2',0,1)
>> int('sin(x)',0,pi)
You should be aware that not every function that appears in calculus
can be symbolically integrated, and so numerical integration is
sometimes necessary. y MATLAB has two commands for numerical
integration of a function f(x): quad and quadl. 15
MATLAB
3. Finding Minimum of a Function
IIn MATLAB the h value
l off x where
h a one-variable
i bl function
f i f(x)
f( ) within
i hi
the interval x1  x  x2 has a minimum can be determined with the
fminbnd command.
command

The value of the function at the minimum can be added to the output
by
y using
g the option:.
p

>>[x,fval]=fminbnd('0.75*x^3-7.23*x^2+15.58*x-0.5',4,6)

16
MATLAB
3. Limits of a Function
You can use limit
Y li it to
t compute
t right-
i ht andd left-handed
l ft h d d limits
li it and
d
limits at infinity.
>> syms x
>> limit(sin(x)/x,x,0)
>> limit((x^4 + x^2 - 3)/(3*x^4 - log(x)), x, Inf)

4 Taylor Series
4.
You can use taylor to generate Taylor polynomial expansions of a
specified order at a specified point.
point
>> taylor(sin(x),0,5)

17
MATLAB
5. Solution of ODE
The symbolic
Th b li solution
l ti f the
for th ordinary
di diff
differential
ti l equation
ti i
in
MATLAB can be determined by dsolve.

>> dsolve('Dy=5*t-6*y')

>> dsolve ('D2y+3*Dy+y=0')

>> dsolve('Dy+y-exp(3*x)','y(0)=2','x')

>> dsolve('D2x+12*Dx+15*x-35','x(0)=2','Dx(0)=1')

18
MATLAB
5. Numerical Solution of ODE

19
MATLAB
function dydt=ODE1(t,y) Create a function ODE1.m.
dydt=(t^3-2*y)/t;
dydt (t 3 2 y)/t;

>> [t y]=ode45('ODE1',[1:0.1:3],4.2)
>> plot(t,y)

20
MATLAB

Das könnte Ihnen auch gefallen