필터 지우기
필터 지우기

Help with an interpolation problem

조회 수: 3 (최근 30일)
Steven
Steven 2012년 4월 18일
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
Walter Roberson
Walter Roberson 2012년 4월 20일
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
Steven
Steven 2012년 4월 20일
Ok, yeah that's what it says in my textbook. I must have typed in the equation wrong sorry. It should read
y(x) = c0 + c1*x + c2*x^2 + c3*x^3 + c4*x^4 + c5*x^5
So would the years correspond to the x values in the equation?
Am I suppose to make a matrix with values of x and a matrix with values of y? But then what am I suppose to set them to?

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by