0

I am trying to determine whether a line drawn connecting a number of points is convex or not. The points can be plotted on x,y coordinates. Can this be done in a way other than essentially connecting each point to every other point and seeing if all of those lines lie above the curve? Thanks! Here are sample points:

X           Y
1191.06     0.9655265 
1192.36     0.9644738 
1193.75     0.9633508 
1194.98     0.9623592 
1196.49     0.9611447 
1197.78     0.9601095
1199.02     0.9591166 
1200.29     0.9581017 
1201.56     0.9570891 
1202.77     0.9561263 
1204.01     0.9551415 
1205.26     0.9541510  
Neverever
  • 15,890
  • 3
  • 32
  • 50

1 Answers1

0

Convex function

A differentiable function of one variable is convex on an interval if and only if its derivative is monotonically non-decreasing on that interval. If a function is differentiable and convex then it is also continuously differentiable. For the basic case of a differentiable function from (a subset of) the real numbers to the real numbers, "convex" is equivalent to "increasing at an increasing rate".

You can iterate over the points and check that the slope between each pair of successive points in the sequence is strictly non-decreasing. You don't have to connect each point to every other point.

Pseudo code:

boolean isConvex(float[] x, float[] y, int length)
{
    float previousSlope = 0;
    for(int i = 1; i < length; i++)
    {
        if(x[i] == x[i-1])
        {
            // handle infinite gradients to avoid div by zero
            // if you are not going to exclude this case from your data
        }
        float slope = (y[i] - y[i-1]) / (x[i] - x[i-1]);
        // only compare the slope after the first iteration:
        if((i > 1) && (slope < previousSlope))
            return false;
        previousSlope = slope;
    }
    return true;
}

This assumes that y is a function of x, and that the values in the arrays are sorted into ascending order based on x and x is monotonically increasing.

samgak
  • 23,944
  • 4
  • 60
  • 82