Is there a 2-D spline function to interpolate data and specify endpoint derivative information

I have a set of [x,y] points representing starting, intermediate and final condition waypoints for a trajectory. I'd like to connect them using a spline, but I'd like to specify the initial derivative (initial heading) and preferably the endpoint derivative condition (final heading). Most searches provide solutions for 1-D spline functions with specified end conditions OR 2-D spline functions without the ability to specify the end conditions. To complicate, I want the initial derivative (heading, dy/dx) to be infiniti. Are there tools in the MATLAB toolbox that will help me interpolate these points with specified derivatives at the endpoints?

 Accepted Answer

Hello Clay,
I understand that you want to fit a spline to points, and also specify the slopes at the end points. You can do this with the standard spline function in MATLAB.
The standard syntax allows for fitting all of the x-y points:
% x, y - data points, xx - x-values to evaluate at, yy - y-values at those points
yy = spline(x,y,xx);
If you add values at the ends of the y input, it allows you to specify the slope at the endpoints:
yy = spline(x,[startSlope y endSlope],xx);
However, infinite values are not allowed as slope. Therefore, you have to get a little trickier and use angles as the independent variable, and both x and y values as the dependent variables. Here is a full example, with infinite slope at the start, and 0 slope at the end, along with visualization:
% Sample data (from unit circle)
x = [-1 -1/sqrt(2) 0];
y = [0 1/sqrt(2) 1];
startSlope = [-1 ; 0];
endSlope = [0 ; -1];
% Get angles of data points
[theta, ~] = cart2pol(x,y);
% Create angles to evaluate the spline at
thetaEval = linspace(pi,pi/2,1000);
% Calculate the spline
spPts = spline(theta,[startSlope [x ; y] endSlope],thetaEval);
plot(spPts(1,:),spPts(2,:),'-b',x,y,'or'); axis equal
I hope this helps with what you need.
-Cam

4 Comments

Thanks Cam I see that one of the keys is choosing the right parameter or independent variable. You suggested angles, which works well for the circle, however I have a much more "curvy" trajectory. The order of the spline matters, but with angles that increase and decrease, I'm unable to achieve the ordering that I desire. Any other thoughts on a parameter that would help me to get the slope I desire (or help me if I'm misunderstanding)?
My method right now is to manipulate my spline points to get an initial derivative (heading) close to what I desire. This works right now, but I'm afraid will break as I continue my research. And, for convenience, for now, I am using cscvn, which uses "Eugene Lee's Centripetal Scheme", accumulated square root of chord length. as the parameter/independent variable.
Hello Clay,
I think that you can resolve the ordering issue with just using indexing instead of angles. Try this code instead (which also allows for more intuitive slope specification than when using angles):
% Sample data (from unit circle)
x = 0:10;
y = sin(x);
startSlope = [0 ; 1];
endSlope = [1 ; 0];
% Get indices of data points
idx = 1:length(x);
% Create index values to evaluate the spline at
idxEval = linspace(idx(1),idx(end),1000);
% Calculate the spline
spPts = spline(idx,[startSlope [x ; y] endSlope],idxEval);
figure
plot(spPts(1,:),spPts(2,:),'-r',x,y,'ob');
And you can compare the plot to the first example on the spline documentation page. Note the different starting and ending slopes.
-Cam
Yes! Thanks Cam, this worked for me. Because I was already using the cscvn command, I decided to use the parameterization that comes out of that function, Eugene Lee's Centripetal Scheme. I parameterized my data points to that scheme, then used the spline command as you recommended. I used the sine and cosine of the initial heading and final heading to determine the dy/dx at the endpoints. It's important to note that although you may ask for a return out of spline or cscvn that is evenly spaced, the even spacing is with respect to your parameterization. I make this comment, because I didn't realize it at first and took the even spacing to mean evenly spaced time units.
Can you explain why the startSlope takes value [-1; 0] denotes a infinite slope? And why endSlope takes [0; -1] denotes a 0 slope?

Sign in to comment.

More Answers (0)

Asked:

on 26 Aug 2015

Commented:

on 25 Dec 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!