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

23 views (last 30 days)
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

Cam Salzberger
Cam Salzberger on 2 Sep 2015
Edited: Cam Salzberger on 3 Sep 2015
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
Clay
Clay on 11 Sep 2015
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.
Donald Yang
Donald Yang on 25 Dec 2021
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)

Community Treasure Hunt

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

Start Hunting!