Spline interpolation, given a data set representing 6 points (xy-axis) of a snake's position at a given time

12 views (last 30 days)
I'm currently working on a spline approximation. I am given t = t1, t2, t3, t4, where I am given a data set representing 6 points (xy-axis) of a snake's position at a given time. My assignment is to plot these 6 points using a spline approximation (interpolation).
My code currently look like this:
% t=[30,170,230 ,385]
x30 = xsnake(30,:);
x170 = xsnake(170,:);
x230 = xsnake(230,:);
x385 = xsnake(385,:);
x = [x30, x170, x230, x385]';
y30 = ysnake(30,:);
y170 = ysnake(170,:);
y230 = ysnake(230,:);
y385 = ysnake(385,:);
y = [y30, y170, y230, y385]';
xx = linspace(0,3.2,100);
plot(xx,csapi(x,y,xx),'k-',x,y,'ro')
The problem is however that I was now told to split this up to 4 different "plot" but on the same graf--I would like to have 4 "spline approximations" on the same graph in different colors. How do I do this? My first attempt was something like this:
plot(xx,csapi(x30,y30,x170,y170,x230,y230,x385,y385,xx),'k-',x,y,'ro')
I have however never used csapi before so I am unfamiliar how to apply it or how to even separate the lines with different colors. My attempt was clearly wrong as I got the error message "too many inputs". Any feedback/suggestions?
Regards, Christoffer

Accepted Answer

dpb
dpb on 19 Nov 2015
Edited: dpb on 19 Nov 2015
I've never used csapi, either, but it seems to be the anomaly in Matlab--it operates by row instead of by column as almost everything else does, such as plot here where different columns in the X, Y arrays are treated as different observations.
Anyway, with that in mind it appears you can do what you wish as
t=[30,170,230 ,385]; % indices into x,y arrays wanted
x=xsnake(t,:); % x, y positions array by row for each time
y=ysnake(t,:);
snake=csapi(x,y); % get the spline ppform object for them
xx=linspace(0,3.2,100); % points at which to interpolate spline
plot(xx.',fneval(snake,xx).','-',x.',y.','*')
legend(num2str([1:length(t)].','Snake %d'))
NB: the arrangement by column in plot and the default color cycling with the spline fitted values represented by a solid line and the measured points by the asterisk. The legend will reflect the colors only unless you do some extra work to match the two line styles.
ADDENDUM
Actually, the fixup for the legend isn't bad...save the object handles when creating it and then add the markers. legend ends up drawing eight lines even though they're hidden looking like only four since plot put two sets of four on the axes. The first object handles are the text for the labels, though so the line handles begin at 5.
[~,hO]=legend(num2str([1:length(t)].','Snake %d')); % save object (text,line) handles
set(hO(length(t)+1:2:end),'marker','*') % set the alternate line marker to match
  6 Comments
Christoffer Thornvall
Christoffer Thornvall on 22 Nov 2015
oh okay my bad, I see the problem. Is there any other function (instead of csapi) that you would have used instead? Because I have tried reading up on csapi but I don't really understand how to apply it for my purpose. Thank you so much btw
dpb
dpb on 22 Nov 2015
Well, that goes back to what the real data are and what is wanted/needed. You can use the above for the segments as given simply by inserting them in the loop if that's the intent to cover the segments individually...

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!