Polynomials curve fitting and interpolation
This presentation is the property of its rightful owner.
Sponsored Links
1 / 25

Polynomials, Curve Fitting, and Interpolation PowerPoint PPT Presentation


  • 120 Views
  • Uploaded on
  • Presentation posted in: General

Polynomials, Curve Fitting, and Interpolation. Numerical Computing with . MATLAB for Scientists and Engineers. You will be able to. Use MATLAB to manipulate polynomials for calculating values, derivatives and for multiplications as well as divisions,

Download Presentation

Polynomials, Curve Fitting, and Interpolation

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Polynomials curve fitting and interpolation

Polynomials, Curve Fitting, and Interpolation

Numerical Computing

with .

MATLAB

for Scientists and Engineers


You will be able to

You will be able to

  • Use MATLAB to manipulate polynomials for calculating values, derivatives and for multiplications as well as divisions,

  • Findthe polynomials for curve fitting in a least square sense,

  • Interpolate intermediate values between sampled points.


Polynomials

Polynomials

  • Functions of the form:

    • n is the degree, or the order of the polynomial.

  • Examples

polynomial of degree 5

polynomial of degree 2

polynomial of degree 1


Representing polynomials

Representing Polynomials

  • MATLAB represents a polynomial as a vector of coefficients of the x with the highest power first.

p = [ 5 0 0 6 4 2];

d = [ 2 -4 20];

p = [ -4 12];


Value of a polynomial

Value of a Polynomial

  • Value of a polynomial p at a value of x

  • Examples

y = polyval(p, x);

18

p = [ 5 0 0 6 4 2];

x = 0;

y = polyval(p,x)

x = [0 1];

y = polyval(p,x)

x = -1:0.01:1;

figure, plot(x, polyval(p,x));

16

14

12

10

8

6

4

2

0

-2

-1

-0.5

0

0.5

1


Roots of a polynomial

Roots of a Polynomial

  • Roots: a set of values where the polynomial has the value of zero.


Example 1 polynomial and its roots

Example 1: Polynomial and Its Roots

  • Plot the following polynomial for the range of x=-2.1~2.1together with its real roots.

p = [1 0 -5 0 4 0];

x = -2.1:0.01:2.1;

% Plot the polynomial

figure(1), plot(x, polyval(p, x), 'LineWidth',2);

hold on; plot([-2.5 2.5], [0 0]);

% Find its roots and plot them using markers.

r = roots(p); ry = zeros(size(r));

h = plot(r,ry,'ro', 'MarkerSize', 10, 'LineWidth', 2);

% Display the polynomial in mathematical terms.

t = text(-0.5, 2, '{\itf}({\itx})\it=x^5-5x^3+4x');

set(t,'FontName', 'Times', 'FontSize', 16);


Addition multiplication and division

Addition, Multiplication and Division

  • Addition: sum of two equal length vectors

  • Multiplication: convolution of two vectors

  • Division: deconvolution of two vectors

p1 = [1 0 -5 0 4 0];

p2 = [0 0 0 1 2 0];

pa = p1 + p2

pm = conv(p1, p2)

[q r] = deconv(p1, p2)


Derivatives of polynomials 1 2

Derivatives of Polynomials 1/2

  • Derivative of a single polynomial

  • Derivative of a product of two polynomials

p1 = [1 0 -5 0 4 0];

p1d = polyder(p1)

p2 = [1 2 0];

pd = polyder(p1, p2)


Derivatives of polynomials 2 2

Derivatives of Polynomials 2/2

  • Derivative of a quotient of two polynomials

[n d ] = polyder(p1, p2)


Exercise 1 mass of glass cup

Exercise 1: Mass of Glass Cup

  • There is a glass cup of the following shape.

    When the density

    of the glass is given

    as ,

    plot the mass of the

    cup as a function of

    the thickness x.

    What is the thickness x, when the mass of

    the cup is 400g.

8cm

x cm

20cm

5x cm


Solution 1

Solution 1

  • Script and Plot

  • Thickness x when the mass is 400g.

glass_cup.m


Polynomials passing through points

Polynomials Passing through Points

  • Find a polynomial passing through the red points.

  • Substitute the points to the polynomial and get the unknown coefficients.

(1,1)

(2,0)

(-2,-1)


Script

Script

  • The matrix equation after the substitution.

poly_3points.m

px = [-2 1 2];

py = [-1 1 0];

A = [4 -2 1; 1 1 1; 4 2 1];

c = inv(A) * py';

x = -3:0.01:3;

y = polyval(c,x);

figure(1);

h = plot(x,y,'b-',px,py,'ro','LineWidth',2);

grid;

set(h(2),'MarkerSize',10);


Least square polynomial fitting

Least Square Polynomial Fitting

  • Find a1 and a0s.t. square errors become min.

2

1.5

Solution

1

0.5

0

-0.5

p = polyfit(x,y,n)

-1

-1.5

-2

-3

-2

-1

0

1

2

3


Script1

Script

  • 1st order polynomial approximation using the least square method

polyfit_3points.m

px = [-2 1 2];

py = [-1 1 0];

p = polyfit(px, py, 1);

x = -3:0.01:3;

y = polyval(p,x);

figure(1);

h = plot(x,y,'b-',px,py,'ro','LineWidth',2);

grid;

set(h(2),'MarkerSize',10);

hold on;

plot([-3 3],[0 0],'r-',[0 0], [-2 2],'r-');

hold off;


Polynomial fitting with diff degrees

Polynomial Fitting with Diff. Degrees

  • Compare several polynomial curve fitting schemes with different degrees.


Script2

Script

polyfit_6points.m

%% Experimental Data

x = [1 3 4 6 8 10];

y = 2.5*x+2+4*randn(size(x));

figure(1);

plot(x,y,'ro','LineWidth',2,'MarkerSize',10);

grid; hold on; axis([0 11 -5 30]);

%% Polyfit using polynomials

x1 = 0:0.01:11;

linespec = ['b-','r-','k-'];

forn=1:2:5

p = polyfit(x,y,n);

y1 = polyval(p,x1);

plot(x1,y1,linespec(n), 'LineWidth', 2);

end

hold off;

legend('Experiment','1st order poly', '3rd order poly',...

'5th order poly', 'Location', 'SouthEast');


Curve fitting with other functions

Curve Fitting with Other Functions


Example

Example

  • Find the best-fit for the following data set.

x = 0:0.5:5;

y = [3.50 2.69 2.02 1.74 1.17 1.05 0.89 0.63 0.50 0.35 0.15];


Script3

Script

unknown_fit.m

x = 0:0.5:5;

y = [3.50 2.69 2.02 1.74 1.17 1.05 0.89 0.63 0.50 0.35 0.15];

figure(1);

plot(x,y,'ro','LineWidth',2,'MarkerSize',10);

grid; hold on; axis([0 5 0 3.6]);

...


Exercise 2

Exercise 2

  • Find the best-fit for the following data set using the functions on page 19.


Solution 2

Solution 2

  • Script and the screenshot

best_fit.m

x = [1.0 1.8 2.9 4.1 4.6 5.4 7.1 7.6 8.2 10.0];

y = [2.00 1.35 0.93 0.70 0.63 0.55 0.43 0.40 0.38 0.31];

figure(1);

plot(x,y,'ro','LineWidth',2,'MarkerSize',10);

grid; hold on;

xlabel('x'); ylabel('y');

...


Interpolation

Interpolation

  • One dimensional interpolation

yi = interp1( x, y, xi, 'method' )


Script4

Script

interp1_demo.m

x = [0.1 0.2 0.4 0.5 0.9 1.0];

y = x .* sin(pi*x);

figure(1);

plot(x,y,'ro','LineWidth',2,'MarkerSize',10);

grid; hold on;

xlabel('x'); ylabel('y');

xi = 0:0.01:1;

yi_n = interp1(x,y,xi,'nearest');

yi_l = interp1(x,y,xi,'linear');

yi_s = interp1(x,y,xi,'spline');

yi_c = interp1(x,y,xi,'pchip');

plot(xi,yi_n,xi,yi_l,xi,yi_s,xi,yi_c,'LineWidth',2);

legend('Exp','Nearest','Linear','Spline','Cubic', ...

'Location','NorthWest');


  • Login