ppval and polyval giving different results

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

 Accepted Answer

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
Your evaluation
polyval(ppm.coefs(1,:),8750)
is wrong unless pp.breaks(1) is 0.

More Answers (1)

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.

6 Comments

How it cuts? He type directly the variable ppm.coefs(1,:)
polyval(ppm.coefs(1,:),8750)
Torsten
Torsten on 19 Apr 2022
Edited: Torsten on 19 Apr 2022
But it's the correct syntax to use polyval in my opinion.
Result should be
0.00031269*8750-10.246
And I was surprised it really came out to be
-7.5099625
Since the values 0.00031269 and 10.246 seem to be cut, I think the OP fed "polyval" with cut coefficients and worked with ppval with the coefficients in full length.
"But it's the correct syntax to use polyval in my opinion."
It's not a problem of syntax, it's problem of convention of pp.coefs which is the coefficients of polynomial of shifted variable yi := x-pp.breaks(i), and not directly x.
breaks = [0 4 10 15];
coefs = [0 1 -1 1 1; 0 0 1 -2 53; -1 6 1 4 77];
pp = mkpp(breaks,coefs)
But you prescribe the coefficients usually. And I don't think you must recalculate them for the shifted polynomial.
"And I don't think you must recalculate them for the shifted polynomial."
You are wrong here is the doc of mkpp
"Polynomial coefficients, specified as an L-by-k matrix with the ith row coefs(i,:) containing the local coefficients of an order k polynomial on the ith interval, [breaks(i), breaks(i+1)]. In other words, the polynomial is coefs(i,1)*(X-breaks(i))^(k-1) + coefs(i,2)*(X-breaks(i))^(k-2) + ... + coefs(i,k-1)*(X-breaks(i)) + coefs(i,k)."
You are right. This also explains the deviations in the breakpoints themselves.

Sign in to comment.

Categories

Products

Asked:

cr
on 19 Apr 2022

Commented:

on 19 Apr 2022

Community Treasure Hunt

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

Start Hunting!