Asked by Sara Jones
on 23 Jul 2019

I am trying to write a program that constructs a trajectory from an intial position [x1,y1,z1] to an end position that passes through a series of intermediate points. e.g. the program accepts three arrays x=[1,6,2,5,3], y=[4,1,6,4,1,1] and z=[9,5,1,5,1,2]. From what I can gather, cubic splines could be implemented in order to achieve this, however I'm not sure if this is the most suitable approach. Does any one have any alternative options? I've managed to find examples of cubic splines in 2 dimensions (i.e. x and y directions) but none that are applied to three axes and if this is the correct approach, I am not sure how to extrapolate this to 3 dimensions. If someone could provide an explanation and example, I would be very greatful! Essentially, I am aiming to write a program that iterates through the spline incrementally so as to achieve a smooth trajectory that could be followed by a simulated robot.

Answer by Akira Agata
on 24 Jul 2019

One possible straight-forward way would be like this:

Actually, spline interpolation seems to be better than cubic spline...

But if you want to use cubic spline, please use 'pchip' option, instead of 'spline'.

% Sample point

x = [1,6,2,5,3,1];

y = [4,1,6,4,1,1];

z = [9,5,1,5,1,2];

t = [1,2,3,4,5,6]; % Assumed time stamp

% Apply interpolation for each x,y and z

tt = linspace(t(1),t(end));

xx = interp1(t,x,tt,'spline');

yy = interp1(t,y,tt,'spline');

zz = interp1(t,z,tt,'spline');

% Visualize the result

figure

scatter3(x,y,z)

hold on

plot3(xx,yy,zz)

David Goodmanson
on 26 Jul 2019

Akira Agata
on 28 Jul 2019

Hi David-san,

Thank you for the clarification. Yes, as you mentioned, interp1(...'spline') and cubic spline (not-a-knot spline) is the same thing. I was just confused "piecewise cubic interpolation" and "cubic spline" when posting my previous answer.

Thank you again!

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## David Goodmanson (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/473087-how-do-you-calculate-a-trajectory-through-a-series-of-3d-points-using-cubic-splines#comment_728558

Sign in to comment.