I try to connect several Points (sometimes some thousands) in a 3D space within Matlab. The trajectory can go in any directions, forwards and backwards in any dimension. I'd like to have the result in the 5th order because the second derivative of the trajectory must not have any glitches in its second derivative.
The problem with the matlab spline() function is, that there must always be only one y-value per x-value. I tried an easy 2D example, the infinity symbol (Lemniscate).
t = pi*[0:.1:2]; a = 1;
for i=1:size(t,2)
x(i) = a*sqrt(2)*cos(t(i))/(sin(t(i)).^2+1);
y(i) = a*sqrt(2)*cos(t(i))*sin(t(i))/(sin(t(i))^2+1);
end
This generated me 21 points, which should look like the lemniscate if connected.
Now changing the first line, calculating the spline and adding the plot
t = pi*[-1:.1:0];
[..]
xx = x(1):.1:x(end);
yy = spline(x,y,xx);
plot(x,y,'o',xx,yy)
This results in the following plot. As you can see this works quite good. But as mentioned the problem is, that this only works for x(i) < x(i+1)
Forward going half of the lemniscate
So now this leads to the following questions:
Would the Matlab spline function be suitable at all for my problem? Regarding the fact that it's need to be continuous in the fifth derivative and is it possible to work around the problem that I need to go in every dimension in every direction sometimes?
If not, what's the way to go? The Problem about B-Splines is, that they are not going through the input Points. A friend mentioned I just should use polynominals of order five and connect them. He said something like I would need to do that for every dimension separately. Does anyone can explain that to me?
Thanks for your help!
I solved this problem with the help of the following code (credit to this post in a different forum)
n=100; [x_t, y_t, tt] = ParametricSpline(x, y, n);
xref = ppval(x_t, tt); yref = ppval(y_t, tt);
with the function
function [ x_t, y_t, t_t ] = ParametricSpline(x,y,n)
m = length(x);
t = zeros(m, 1);
for i=2:m
arc_length = sqrt((x(i)-x(i-1))^2 + (y(i)-y(i-1))^2);
t(i) = t(i-1) + arc_length;
end
t=t./t(length(t));
x_t = spline(t, x);
y_t = spline(t, y);
t_t = linspace(0,1,n);
end