MATLAB Answers

Interpolating vectors at specific points

26 views (last 30 days)
Christopher on 15 Sep 2013
Hello all,
I am trying to do some interpolation with 3 column vectors. The first vector v, is a velocity column vector. The next 2 vectors are the x and y coordinates corresponding to that velocity. I am trying to interpolate the velocity vector where it changes from positive to negative and return the corresponding x and y coordinates. I will illustrate this below the best I can.
From these 3 column vectors I would like to find x and y at V=0. After they are found I would like to insert them back into the vectors at there respective positions. So for velocity a 0 would be inserted between the change from positive to negative:
Then the x and y points corresponding to V=0 would be put back in the x and y column vectors at the right position.
The numbers I inserted are just to show how it would work and may not be the correct interpolated values.

Answers (1)

Sven on 15 Sep 2013
Hi Christopher, here's a solution that does the job. If you can guarantee that you only have one crossing in your vector V, then you don't need the loop (or the break command).
V = [.8;1;.8;.3;-.3;-.8;-1];
x = [1;.8;.5;.4;.3;.2;.1];
y = [1;.5;.2;-.4;-.5;-.6;-.7];
xy = [x y]; % I concat x and y for convenience
while 1
% Find the first available zero-crossing index of V
idx = find(abs(diff(sign(V)))==2, 1);
if isempty(idx)
% Interpolate xy at that zero
newXY = interp1(V(idx:idx+1),xy(idx:idx+1,:),0);
% Concat the results into xy and V
xy = [xy(1:idx,:); newXY; xy(idx+1:end,:)];
V = [V(1:idx); 0; V(idx+1:end)];

Community Treasure Hunt

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

Start Hunting!