Re: polynomial plot for spline interpolation
- From: "John D'Errico" <woodchips@xxxxxxxxxxxxxxxx>
- Date: Mon, 24 Nov 2008 21:59:02 +0000 (UTC)
"Otavio " <otaviofm@xxxxxxxxx> wrote in message <ggerg2$n51$1@xxxxxxxxxxxxxxxxxx>...
Use mkpp and ppval was really a good hint, but I still can't find out how to convert all the segments in one. Instead, I tried to plot each one using 'hold', but it didn't work. I wrote the code as follows:
for i=1:20
cc = [a(i) b(i) c(i) d(i)];
pp = mkpp([x(i) x(i+1)],cc);
xx = x(i):0.1:x(i+1);
plot(xx,ppval(pp,xx))
hold on;
end
but the resulting plot was very far from what I expected.. The curve segments weren't even conected with each other. Any suggestions..?
Ohhh man, I found out what was going wrong in my plot! The plot wasn't really 'a(i)x^3 + b(i)x^2 + c(i)x + d1' ! Instead, I should do a(i)(x-x(i))^3 + b(i)(x-x(i))^2 + c(i)(x-x(i)) + d1. Too bad, since it means I can not use mkpp anymore... Or can I..? Now I'm really confused..
You are gradually getting there. Yes, the pp form
must be a set of polynomials in a specific form.
They will be evaluated, first subtracting the left
hand endpoint of the interval that the polynomial
lives in. This is done for a very good reason.
Suppose I have a spline that lived over the set of
knots 0:1:10. Now, suppose I had exactly the
same function with the same set of values it must
pass through, but assume that it lives on the knots
1e6+(0:1:10).
The polynomials in the second case will force us to
square and cube numbers on the order of 1e6, then
adding and subtracting the results. This will lead to
numerical garbage. So instead, we always create
the polynomials to live on the domains
[0, x(i+1) - x(i)]
In your case, you created a set of polynomials that
live on the wrong domains. This is still survivable.
Suppose we have the polynomial with coefficients
P = [a b c d]
representing the symbolic polynomial
a*x^3 + b*x^2 + c*x + d
Assume that P has been created to live on the
interval [x1,x2], but we need to convert P into
another polynomial that lives on the interval
[0,x2-x1], yet produces the same predictions
over these intervals.
Q = a*x^3 + (b + 3*a*x1)*x^2 + ...
(c+2*b*x1+3*a*x1^2)*x + ...
(d+c*x1+b*x1^2+a*x1^3)
In terms of the original polynomial P, this is
most easily written with just a matrix multiply
to create the new polynomial coefficients.
Q = P*[1 3*x1 3*x1^2 x1^3; ...
0 1 2*x1 x1^2; 0 0 1 x1;0 0 0 1];
Test it out on a random polynomial.
P =P = rand(1,4)
0.94478 0.71447 0.67922 0.95938
x1 = 5;
0 1 2*x1 x1^2; 0 0 1 x1;0 0 0 1]Q = P*[1 3*x1 3*x1^2 x1^3; ...
Q =
0.94478 14.886 78.683 140.32
evaluate P at a set of points starting from x = 5.
ans =polyval(P,5:.1:5.5)
140.32 148.33 156.65 165.29 174.23 183.5
And evaluate Q at the corresponding points, from
zero up.
ans =polyval(Q,0:.1:.5)
140.32 148.33 156.65 165.29 174.23 183.5
Yup, it worked. Must've got lucky.
HTH,
John
.
- Follow-Ups:
- Re: polynomial plot for spline interpolation
- From: Otavio
- Re: polynomial plot for spline interpolation
- From: Otavio
- Re: polynomial plot for spline interpolation
- From: Otavio
- Re: polynomial plot for spline interpolation
- References:
- polynomial plot for spline interpolation
- From: Otavio
- Re: polynomial plot for spline interpolation
- From: John D'Errico
- Re: polynomial plot for spline interpolation
- From: Otavio
- Re: polynomial plot for spline interpolation
- From: Otavio
- polynomial plot for spline interpolation
- Prev by Date: Re: Function to Return an Image's DPI
- Next by Date: Re: Rapid, simultaneous 2nd deg. polynomial curve fitting
- Previous by thread: Re: polynomial plot for spline interpolation
- Next by thread: Re: polynomial plot for spline interpolation
- Index(es):
Relevant Pages
|