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

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

 Risposta accettata

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 Commenti

It's kinda' neat what it does...fortunately apparently my variable name change from snake where computed the ppform to pp from the csapi doc for it didn't confuse too much... :) I did fix that and added a note on how to fixup the legend lines...
HI, I did try this code but I got some error messages I never seen before. When using the code:
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.','*')
[~,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
where xsnake and ysnake are 501x6 matrices.
After running this I got the error message:
Error using chckxywp (line 58) X must be a vector.
Error in csapi1csapi1 (line 90) [xi,yi,sizeval] = chckxywp(x,y);
Error in csapi (line 75) pp = csapi1(x,y);
Error in U2 (line 34) snake=csapi(x,y); % get the spline ppform object for them
Any ideas?
Regards, Christoffer
Oh--as said, I'd never used csapi before, either. I had done the testing here on some dummy data where just used a single x-vector and a random set of y values to figure out the orientation it expected data and had simply presumed that if given multiple x vectors in a 2D array it would do a single spline on each corresponding pair. Bad assumption; it does multiple fits for each row of a y array but only for a single x vector at a time. So, in your case you'll have to use a loop over 1:length(t) and select each x(t(i)) and y(t(i)) pair for each time of interest.
PS. I did the one x as you had a constant x over which you were evaluating so I had presumed that would be a constant but I suppose it probably was intended to cover the full range of x seen in the raw data, perhaps, so you'd have the disjointed sections? If the intent is to fill in between sections, this probably isn't the way as these section won't be continuous. I'd have to do more reading up on just what csapi actually does...
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
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...

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by