2

If I do the following in MATLAB:

ppval(mkpp(1:2, [1 0 0 0]),1.5)
ans =  0.12500

This should construct a polynomial f(x) = x^3 and evaluate it at x = 1.5. So why does it give me the result 1.5^3 = .125? Now, if I change the domain defined in the first argument to mkpp, I get this:

> ppval(mkpp([1 1.5 2], [[1 0 0 0]; [1 0 0 0]]), 1.5)
ans = 0

So without changing the function, I change the answer. Awesome.

Can anyone explain what's going on here? How does changing the first argument to mkpp change the result I get?

gnovice
  • 125,304
  • 15
  • 256
  • 359
Xodarap
  • 11,581
  • 11
  • 56
  • 94

1 Answers1

3

The function MKPP will shift the polynomial so that x = 0 will start at the beginning of the corresponding range you give it. In your first example, the polynomial x^3 is shifted to the range [1 2], so if you want to evaluate the polynomial at an unshifted range of [0 1], you would have to do the following:

>> pp = mkpp(1:2,[1 0 0 0]);   %# Your polynomial
>> ppval(pp,1.5+pp.breaks(1))  %# Shift evaluation point by the range start

ans =

    3.3750                     %# The answer you expect

In your second example, you have one polynomial x^3 shifted to the range [1 1.5] and another polynomial x^3 shifted to the range of [1.5 2]. Evaluating the piecewise polynomial at x = 1.5 gives you a value of zero, occurring at the start of the second polynomial.

It may help to visualize the polynomials you are making as follows:

x = linspace(0,3,100);                     %# A vector of x values
pp1 = mkpp([1 2],[1 0 0 0]);               %# Your first piecewise polynomial
pp2 = mkpp([1 1.5 2],[1 0 0 0; 1 0 0 0]);  %# Your second piecewise polynomial
subplot(1,2,1);                            %# Make a subplot
plot(x,ppval(pp1,x));                      %# Evaluate and plot pp1 at all x
title('First Example');                    %# Add a title
subplot(1,2,2);                            %# Make another subplot
plot(x,ppval(pp2,x));                      %# Evaluate and plot pp2 at all x
axis([0 3 -1 8])                           %# Adjust the axes ranges
title('Second Example');                   %# Add a title

alt text

gnovice
  • 125,304
  • 15
  • 256
  • 359
  • I've been staring at this for so long... Thank you! If I want to evaluate it at some vector of points how would I do it? Is there a better function to use than ppval? – Xodarap Nov 16 '10 at 16:53
  • @Xodarap: You can pass a vector of points to [PPVAL](http://www.mathworks.com/help/techdoc/ref/ppval.html) as illustrated in my sample plotting code above. – gnovice Nov 16 '10 at 17:01