Interpolate points on 2D mapping
38 views (last 30 days)
Show older comments
I am taking data that essentially generates a mapping between two, 2D sets of variables, in my case voltage (v1,v2) and position (x,y). Each pair of (v1,v2) values maps uniquely to an (x,y) pair, but does so in a non-linear fashion. I do not have an analytic function that follows this map, so what I do instead is I output a evenly spaced array of voltage values and record the resultant (x,y) positions for each voltage pair. Thus I build a numerical map between the two variable sets. My problem now is, I would like to use an interpolation method (e.g. spline, bilinear, or whatever) to artificially resolve the mapping of intermediate voltages to positions.
An example mapping might look like:
(v1,v2) -> (x,y)
(-1,0) -> (-14,-2)
(0,0) -> (0,0)
(1,0) -> (13.5,-1)
(-1,1) -> (-13,7)
(0,1) -> (0.5,8)
(1,1) -> (14,7)
(-1,2) -> (-14,12)
(0,2) -> (1,11)
(1,2) -> (14.5,11)
Then I would want to somehow interpolate this map to get what the positions for intermediate voltages would be, e.g. every 0.5 volt step.
Intuitively, this seems like a trivially easy problem. I have the map already and MATLAB has a million fitting functions, so I would think this is a three-lines-of-code sort-of problem. However, I just can't seem to find what I'm looking for. When I just spline fit the average x and y behavior separately (i.e. x(v1) averaged over horizontal slices and y(v2) averaged over vertical slices), I am able to match the map OK, but it doesn't catch the subtle variation over the map. The interp2 function kind of does what I want but it is for functions of two variables, i.e. Z(X,Y), not for a 2D mapping between variables (v1,v2)<->(x,y). So I tried interpolating y(v1,v2) and x(v1,v2) using interp2, but the output is just a 1D vector that seems very similar to my averaged 1D interpolation.
Is there something I am missing or simply doing wrong? Any help would be greatly appreciated, and I can provide more detail wherever things are unclear.
Thanks
0 Comments
Answers (2)
Star Strider
on 12 Nov 2014
First, I did a (linear) regression on your data with lsqcurvefit and got a decent fit to your data. (I used lsqcurvefit because I thought it might be nonlinear and I wanted to be able to change the model easily to reflect that.) My objective function produces a (Nx2) vector to match ‘P’.
The code:
V = [-1 0; 0 0; 1 0; -1 1; 0 1; 1 1; -1 2; 0 2; 1 2];
P = [-14 -2; 0 0; 13.5 -1; -13 7; 0.5 8; 14 7; -14 12; 1 11; 14.5 11];
PV = @(b,V) [[(b(1).*V(:,1)+b(2))] [(b(3).*V(:,2)+b(4))]]; % Objective Function
[B, rn, r] = lsqcurvefit(PV, ones(4,1), V, P);
Pc = PV(B,V);
Second, the only interpolation functions I can think of that might work with your data are either griddatan or interpn.
I suggest you experiment with them, as well as with my objective function and lsqcurvefit, especially as you gather more data. You know your system and its dynamics better than I do. If you have a state space model of your system, that will likely do better than my regression to model its behaviour.
0 Comments
See Also
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!