Search code examples
arraysmatlabareanumerical-integration

Matlab - Distances of two lines


I have two lines, one straight and one curvy. Both have an arbitrary number of x and y values defining the lines - the number of x and y values are not the same for either line. I am attempting to get separate distances of points between the curved line coordinates and the straight line coordinates. You can think of discrete integration to get a better picture of what I'm talking about, something along the lines of this: http://www.scientific-solutions.ch/tech/origin/products/images/calculus_integral.gif

By adding the different distances, I would get the area. The part on which I am stuck is the actual synchronization of the points. I can simply compare the x and y values of the straight and curve coordinates every ten indices for example because the curved coordinates are time dependent (as in the points do not change at a general rate). I need a way to synchronize the actual coordinates of the two sets of points. I thought about interpolating both sets of points to a specific number of points, but again, the time dependence of the curved set of points makes that solution void.

Could someone please suggest a good way of doing this, outlining the basics? I really appreciate the help.

Code to be tried (pseudo):

xLine = [value1 value2 ...]
yLine = [value1 value2 ...]
xCurve = [value1 value2 ...]
yCurve = [value1 value2 ...]

xLineInterpolate = %interpolate of every 10 points of x until a certain value. same with yLineInterpolate, xCurveInterpolate and yCurveInterpolate.

Then, I could just take the same index from each array and do some algebra to get the distance. My worry is that my line values increase at a constant rate whereas my curve values sometimes do not change (x and y values have different rates of change) and sometimes do. Would such an interpolation method be wrong then?


Solution

  • If I understand correctly, you want to know the distance between a straight line and a curve. The easiest way is to perform a coordinate transformation such that the straight line is the new x-axis. In that frame, the y-values of the curved line are the distances you seek.

    This coordinate transformation is equal to a rotation and a translation, as in the following:

    % estimate coefficients for straight line
    sol = [x1 ones(size(x1))] \ y1;
    m = sol(1); %# slope
    b = sol(2); %# y-offset at x=0
    
    % shift curved line down by b 
    % (makes the straight line go through the origin)
    y2 = y2 - b;
    
    % rotate the curved line by -atan(m)
    % (makes the straight line run parallel to the x-axis)
    a = -atan(m);
    R = [+cos(a) -sin(a)
         +sin(a) +cos(a)];    
    XY = R*[x2; y2];
    
    % the distances are then the values of y3. 
    x3 = XY(1,:);
    y3 = XY(2,:);