Can someone explain the behavior of the functions mkpp and ppval?

此生再无相见时 提交于 2019-12-12 17:08:39

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/4196417/can-someone-explain-the-behavior-of-the-functions-mkpp-and-ppval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!