Re: Help with Splines, please
- From: John D'Errico <woodchips@xxxxxxxxxxxxxxxx>
- Date: Thu, 03 Nov 2005 13:14:39 GMT
In article
<28385248.1131018696775.JavaMail.jakarta@xxxxxxxxxxxxxxxxxxxxxx>,
Ethirea <adlquijano@xxxxxxxxxxx> wrote:
> Hello to all.
> My problem with splines is the following one: function SPLINE of Matlab
> interpolates several points although (x4<x3 i.e.). I have developed my own
> code but she only interpolates well if (x1<x2<x3< ...)in another case it does
> not do it, ignores the points. how I can solve it? some idea? how does
> internally Matlab? Thank you very much.
> This is my code:
Hi. Without getting deeply into your code, I'll try to
make a quick stab at the answer. If my answer is not
adequate, I promise I'll go through the code more
carefully. I do have a couple of comments from a quick
scan of it.
First, you can always gain in matlab by using vectorized
operations. Thus, to compute the differences between
successive knots, use diff instead of a loop:
h = diff(x);
b is apparently a vector of chord slopes between successive
points. Again, its easily computed with no loops.
b = diff(y)./h;
While its not always necessary to avoid loops, it can
make your code more efficient. Always add a comment
that explains what was done if its not obvious.
You also use loops to compute vectors u, v and e. I'll
guess that these solve an implicit tridiagonal system of
equations to build the cubic spline. I'll trust you on
that for now. They look quite reasonable.
At the end, where you determine in which interval a point
lies, you use a loop. Simpler, and very possibly faster,
is to use histc, especially if you want to interpolate
many points. You could also have used my bindex code
on the file exchange. Its only necessary for those who
have old enough versions of matlab that they are lacking
histc.
This brings up another issue. If you wish to interpolate
multiple points through the spline, your code will build
the spline all over again for each point xx, since you
only provide the ability to evaluate it for a scalar xx.
My final comment is the only one which really deals with
your question. It sounds like you wish to fit a spline
through data which does not have x(i) < x(i+1), i.e.,
x is not monotone. The result would be a multi-valued
relationship, so not a single-valued function of x at
all.
If you have a problem where you wish to interpolate
data where x is not increasing, then the usual trick is
to interpolate x(t) and y(t), where t is the cumulative
piecewise linear arclength along the curve. An example
is a set of points around a circle. For x and y vectors
along such a curve, this will compute the arclength in
a vectorized fashion (assuming that x and y are ROW
vectors)
t = cumsum(sqrt([0,diff(x).^2 + diff(y).^2]));
Overall, thanks for posting your code. Its always fun
to play with spline equations. Fun for me at least.
Some might claim I'm a bit disturbed there. ;-)
Ask again if I've confused you or I missed the point
of your question.
HTH,
John D'Errico
--
The best material model of a cat is another, or preferably the same, cat.
A. Rosenblueth, Philosophy of Science, 1945
Those who can't laugh at themselves leave the job to others.
Anonymous
.
- References:
- Help with Splines, please
- From: Ethirea
- Help with Splines, please
- Prev by Date: Re: Vectorized If
- Next by Date: Re: compare matrix
- Previous by thread: Help with Splines, please
- Next by thread: Re: Help with Splines, please
- Index(es):
Relevant Pages
|