Cannot get the right coefficient from PCHIP fit
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mattia Bellini
am 18 Apr. 2019
Beantwortet: Torsten
am 18 Apr. 2019
Hello,
I want to fit my x, y data using the PCHIP-fit and then I would like to get the coefficients to create several equation that would piecewise fit my data.
I am able to create the fit and to access the single coefficients, but when I try to plot the single polinomial using the coefficients from the PCHIP, the result is wrong.
Here is the code:
y = [0; 600; 843.6; 914.5; 941.1];
x = [0; 100; 500; 1000; 1500];
CurveFit = fit(x,y,'pchip'); % Create the PCHIP fit
pp = pchip(x,y);
[breaks,coefs,L,order,dim] = unmkpp(pp); % Get Coefficientfrom PCHIP
scatter(x,y); grid on; hold on; % Plot my data x, y
plot(CurveFit,'k'); % Plot the pchip fit
syms f1(x) % Plot the first polinomial function for the first two set of data using the coefficient from PCHIP
f1(x) = coefs(1,1)*x.^3 + coefs(1,2)*x.^2 + coefs(1,3)*x + coefs(1,4);
fplot(f1,[0 100]);
syms f2(x) % Plot the second polinomial function for the second set of data using the coefficient from PCHIP
f2(x) = coefs(2,1)*x.^3 + coefs(2,2)*x.^2 + coefs(2,3)*x + coefs(2,4);
fplot(f2,[100 500]);
syms f3(x)
f3(x) = coefs(3,1)*x.^3 + coefs(3,2)*x.^2 + coefs(3,3)*x + coefs(3,4);
fplot(f3,[500 1000]);
syms f4(x)
f4(x) = coefs(4,1)*x.^3 + coefs(4,2)*x.^2 + coefs(4,3)*x + coefs(4,4);
fplot(f4,[1000 1500]);
The result I got looks like this! I don't know why only f1(x) is overlapping my PCHIP- fit, while the other functions f2(x), f3(x) and f4(x) are not. Should not all the functions overlap the fit?
Do you know what I am getting wrong? Thank you
0 Kommentare
Akzeptierte Antwort
Torsten
am 18 Apr. 2019
From the documentation:
Since the polynomial coefficients in coefs are local coefficients for each interval, you must subtract the lower endpoint of the corresponding knot interval to use the coefficients in a conventional polynomial equation. In other words, for the coefficients [a,b,c,d] on the interval [x1,x2], the corresponding polynomial is
f(x)=a(x−x1)^3+b(x−x1)^2+c(x−x1)+d .
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Calculus finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!