# ppval and polyval giving different results

16 views (last 30 days)
cr on 19 Apr 2022
Commented: Torsten on 19 Apr 2022
I created a piecewise polynomial using mkpp. But when I evaluate a set of points I get incorrect results using ppval, although polyval() gives correct result. Looks like I'm missing something. Any idea what that is?
Thanks!
>> ppm
ppm =
struct with fields:
form: 'pp'
breaks: [749.5 16747 32768 48771 64784]
coefs: [4×2 double]
pieces: 4
order: 2
dim: 1
K>> ppm.coefs
ans =
0.00031269 -10.246
0.00031268 -10.246
0.00031274 -10.247
0.00031265 -10.245
>> ppval(ppm,ppm.breaks(1))
ans =
-10.246
>> polyval(ppm.coefs(1,:),ppm.breaks(1))
ans =
-10.012
>> polyval(ppm.coefs(1,:),8750)
ans =
-7.51
>> ppval(ppm,8750)
ans =
-7.7444

Bruno Luong on 19 Apr 2022
Edited: Bruno Luong on 19 Apr 2022
Here is a correct way to know how ppval works
pp=spline(cumsum(rand(1,10)),rand(1,10));
x=3;
ppval(pp,x)
ans = 2.1433
%
i=discretize(x,pp.breaks);
polyval(pp.coefs(i,:),x-pp.breaks(i))
ans = 2.1433
polyval(ppm.coefs(1,:),8750)
is wrong unless pp.breaks(1) is 0.

Torsten on 19 Apr 2022
I think you fed "polyval" with the coefficients cut to a certain number of digits.
You must use the coefficients in full precision to get equal results from ppval and polyval.
Torsten on 19 Apr 2022
You are right. This also explains the deviations in the breakpoints themselves.

### 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!

Translated by