The most efficient way to find the min and max of the sin / cos curve in C #

Background: I have a function in my program that takes a set of points and finds the minimum and maximum on the curve generated by these points. The thing is, it is incredibly slow since it uses a while loop to determine min / max based on an approximated error. I’m not quite sure what this formal method is, because I didn’t write it myself, but I know that we need a new and more effective one.

Question: My question is the best and most efficient method / algorithm for finding the minimum points on a curve using C #, which is also very accurate?

About the curve: I have a college numerical analysis book next to me, so all I need is a method name and a nudge in the right direction. I can generate as many points as I choose to approximate the curve, but I want the number of points to reach an effective minimum. The curve is always in the form of one segment of the Sin / Cos curve, but not always the same curve and will always be less than one period. Theta range is 0 ° up to 359.999 ... ° It has some phase and amplitude shift, and Y will never be negative. This function / algorithm should work quickly because it will run every few hundred milliseconds as the curve changes.

Any suggestions are welcome.

EDIT

Additional curve information: Points are generated by moving the mouse. Points are a set of points based on the length of a rubber belt in a drive design with a tensioning mechanism, such as a serpentine belt in a car. The idle position determines the length of the belt, and I get the curve [belt length (y) with respect to the intermediate position (x)]. The limit in this case is a rotary loafer and will have a constant circular motion. If a change in the drive circuit changes the curve, either due to a change in the length points, or because of the limited range of motion of the tensioner. The idle range is potentially 0 °; up to 359.999 ... ° and is theta, as stated above. For a slot with a slot, the maximum interval is 1/2 of the curve period (a simpler problem).

, , .

+3
7

, ( , ), , . , , .

, , (, ). , - . , , ​​ , ?

, ( ) ( ).

, , .

0

, , 0. ax ^ 2 + bx + c = 0, x = -b/2a.

opr, a. a > 0, a < 0, ( = 0, ).

, . , , ?

: , , . .

Edit2:

y = a sin (mx + t) + c. , . , , ( ).

+2

? - "" , ? , , , , ...
, - , Y- .

( - , (, , x-corrd?), , (y-coord))...

:, , "" Sin/Cos, , , > , , , "" ( ( Y ) . , , = , , ... x, . [ ]

, / , , , , , , ...

2nd EDIT: , ... , , .

PsuedoCode:

  Check Leftmost point to see slope (Up Down or Zero)
       If Zero, done
  Check RightMost Slope 
       If Zero - Done
  If two Slopes are same sign - Done 
        - pick Bigger of two points ( - or smaller if looking for min)
  Check point in the Middle slope
     If Zero, Done
     If slope has same sign as left pt, Change Left to this Point and repeat
     If slope has same sign as right pt, Change Right to this Point and repeat
0

(, , ), (, #, , ). , (, ). ( , ) . (pdf). - , .

0

, ( >= 4), , y = A cos(Bx+C)+D, , , . B, . .

0

X Y

"@: "

. , , {getMaxIndex}

    private void Test()
    {
        double[] X = SetLinearRange(0, Math.PI * 2, 1000);
        double[] Y = GetOutput(X);
        int MaxIndex = getMaxIndex(Y);
        double MaxX = X[MaxIndex];
        double MaxY = Y[MaxIndex];
    }
    private double[] SetLinearRange(double Start, double End, int Sample)
    {
        double Step = (End - Start) / Sample;
        double CurrentVaue = Start;
        double[] Array = new double[Sample];
        for (int Index = 0; Index < Sample; Index++)
        {
            Array[Index] = CurrentVaue;
            CurrentVaue += Step;
        }
        return Array;
    }
    private double[] GetOutput(double[] X)
    {
        double[] Array;
        Array = (from double Item in X select myFunction(Item)).ToArray();
        return Array;
    }
    private double myFunction(double x)
    {
        double y;
        //put any function
        y = 3 * Math.Sin(5 * x + 2);
        return y;
    }
    private int getMaxIndex(double[] Y)
    {
        double YM = Y.Max();
        int Index = Y.ToList().IndexOf(YM);
        return Index;
    }

, .

0

.

If you yourself generate points, why not just track the largest / smallest points when doing the generation?

If you have a function, for example, I'm sure others pointed out, just get the derivative and decide for 0. This will give you a min / max point.

0
source

Source: https://habr.com/ru/post/1753334/


All Articles