Help with an interpolation problem
3 views (last 30 days)
Show older comments
Given the (fictitious) gasoline price data over 10 years,
Year, x 1986 1988 1990 1992 1994 1996
Price (¢),y 113.5 132.2 138.7 141.5 137.6 144.2
a simple interpolating polynomial is proposed as:
y(x) = c0 + c1*x + c2*x^2 + c3*x^3 + c4*x^4 + c5*x^5
Find and plot the interpolating polynomials of gasoline prices by
(a) Solving system of equations
(b) Using Lagrange polynomial
(c) Using Newton polynomial
(Note: Use the MATLAB command format long e to clearly identify the differences between the values of the coefficients produced by using different methods.)
Here is my code for lagrange:
%Given data points
x=[1986 1988 1990 1992 1994 1996];
y=[113.5 132.2 138.7 141.5 137.6 144.2];
%Find the Lagrange polynomial
[l,L]=lagranp(x,y);
disp(' (1) Lagrange coefficient polynomials:'),L
disp(' (2) Lagrange polynomial coefficients:'),l
%Create an xx vector [0 4] and define xi=1 which is the point of interest
xx=[1986:0.1:1996];
%Interpolate for xx
yy=polyval(l,xx);
%Plot the polynomial
plot(xx,yy,'r',x,y,'d-'), grid on
xlabel('x'),ylabel('y')
title('Lagrange Plot')
I'm not sure my plot is right because there is two lines one the graph
Here is my code for newton:
%Given data points
x=[1986 1988 1990 1992 1994 1996];
y=[113.5 132.2 138.7 141.5 137.6 144.2];
%Find the Newton polynomial
[n,D]=newtonp(x,y);
disp(' (1) Newton polynomial coefficients:'),n
disp(' (2) Newton divided differences:'),D
%Create an xx vector and point to evaluate the function at
xx=1986:1996;
%Interpolate for xx
yy=polyval(n,xx);
%Plot the polynomial
plot(xx,yy,'b',x,y,'d'), grid on
xlabel('x'),ylabel('y')
title('newton')
I'm pretty sure this is right but I'm not 100% sure
Also I'm not quite sure how to do this for system of equations
5 Comments
Walter Roberson
on 20 Apr 2012
If we understand c1x to be c subscript 1, multiplied by x, then
y(x) = c0 + (c1 + c2 + c3 + c4 + c5) * x
which would simplify to
y(x) = c0 + c6 * x
when c6 = c1 + c2 + c3 + c4 + c5
This could be done, but you would not be able to meaningfully extract c1, c2, c3, c4, c5 from it.
A different polynomial would make more sense:
y(x) = c0 + c1*x + c2*x^2 + c3*x^3 + c4*x^4 + c5*x^5
Answers (0)
See Also
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!