using polyval with one of the polynomials of a spline obtained with the unmkpp command, the results do not overlap! Why?
조회 수: 10 (최근 30일)
이전 댓글 표시
I first interpolated some data using the spline command. Then I calculated the coefficients of the different cubic polynomials that make up the spline. Then I used the polyval command to check the first polynomial but the spline and polyval curves do not overlap.
What am I doing wrong?.
There is definitely something related to the various polynomials that I have not understood correctly. I thank anyone who helps me understand better. See the file script please:
%butta_sto_test
%
clear all
clc
% Data
% Time
Time=[7, 9, 11, 12]
% Temperture
Temperature=[49, 57, 71, 75]
% Data Time query
Timeq=[7:0.1:12];
% Results
% piecewise polynomial structure
pp=spline(Time, Temperature);
% Timeq data interpolation
Temperatureq=spline(Time, Temperature, Timeq);
% Calculating the coefficients of the 3 polynomials that make up the spline
[breaks, coeffs, m, n] = unmkpp(spline(Time,Temperature));
% First polinomial p1
p1=coeffs(1,:)
% First time interval corresponding to the first polynomial of the spline
Timeq1=[7:0.1:9];
% using polyval to calculate the values corresponding to Timeq1 using the polynomial p1
Temperatureq1=polyval(p1,Timeq1);
% Spline plot
figure(1)
plot(Timeq, Temperatureq, Time, Temperature, 'o', Time, Temperature, '--')
hold on
plot(Timeq1, Temperatureq1,'*')
grid on
댓글 수: 0
채택된 답변
David Goodmanson
2024년 6월 18일
편집: David Goodmanson
2024년 6월 18일
Hello TH,.
spline polynomials are local to the secgment in question starting from x = 0, so if an interval is, for example 3 to 4.4, polyval uses 0 to 1.4. If you use
Timeq1=[7:0.1:9]; % existing line
Timeq1poly = Timeq1-Timeq1(1);
% using polyval to calculate the values corresponding to Timeq1 using the polynomial p1
Temperatureq1=polyval(p1,Timeq1poly);
then it works.
추가 답변 (2개)
dpb
2024년 6월 18일
% Data
% Time
Time=[7, 9, 11, 12];
% Temperture
Temperature=[49, 57, 71, 75];
% Data Time query
Timeq=[7:0.1:12];
% Results
% piecewise polynomial structure
pp=spline(Time, Temperature);
% Timeq data interpolation
Temperatureq=spline(Time, Temperature, Timeq);
plot(Time,Temperature,'*')
hold on
plot(Timeq,Temperatureq)
Ts=ppval(pp,Timeq);
plot(Timeq,Ts,'x')
[bk,p]=unmkpp(pp)
T1=bk(1):0.1:bk(2);
Tp=polyval(p(1,:),T1-bk(1));
plot(T1,Tp,'+')
xlim([7 9]), ylim([45 60])
Spline coefficients are local to the break region so to evaluate by conventional polynomial functions you must subract the leading breakpoint from each section. Since this is done for you automagically by ppval, it's nonsense to not use the toolset for the job.
댓글 수: 3
dpb
2024년 6월 18일
I didn't say it wasn't a worthwhile exercise to figure out; only to not use the wrong toolset for the job... :)
Perhaps "nonsense" wasn't a good choice, granted.
John D'Errico
2024년 6월 18일
@David Goodmanson and @dpb and @Steven Lord have all said the polynomial segments are intended to be LOCAL with respect to the break points. I'm adding this as an answer only because I want to explain why it is done that way.
Supose I gave you two curves to interpolate using a spline, thus f(x1,y) and f(x2,y).
x1 = 0:1:10;
x2 = 1e8 + (0:1:10);
y = rand(1,11); % I dont give a hoot what values you choose for y.
spl1 = spline(x1,y);
spl2 = spline(x2,y);
The two curves should, in theory, be virtually identical. All I have done is horizontally translate the curve in x by some very large number.
Remember, these will be cubic splines. If the cubic segments for these splines were not local things, then in spl2, you would be forming a cubic polynomial where you are raising numbers on the order of 1e8 to the third power, and then adding and subtracting those values to other nubmers. Those polynomials would be unmanagable. You would expect to see massive numerical problems.
You don't want that. Even smaller shifts would cause serious problems. The solution is trivial. Just formulate each segment to be defined as a cubic polynomial, but where the LEFT end of each segment ALWAYS starts at zero, and has length given by diff(breaks). This resolves the numerical problems as much as possible.
댓글 수: 6
dpb
2024년 6월 18일
I was thinking specifically of both spline and interp1, but also are the options in Curve Fitting TB with fit based on the same algorithms/methods?
dpb
2024년 6월 19일
RE: your "archeology", I remember polyfit from the early 3.1 release thru R11 before started the self-employed consulting gig (coincidentally also in 2000) and bought the license from former employer and upgraded. Prior to that I had been browbeat/indoctrinated from reading GEP Box that I had built a standardized version around the existing implementation that also included normalizing shape transitions.
Interestingly enough, I hadn't ever taken the time to do the exercise @John D'Errico just illustrated to recognize that the zero shift accomplished the same thing numerically (although doesn't do anything about the shape transformation, of course). I just looked but I do not seem to have an archival copy of that code at hand although I'm sure I could dig it up...
참고 항목
카테고리
Help Center 및 File Exchange에서 Spline Postprocessing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!