+2  A: 

If you have a quadratic equation then the maximum or minimum will always be at the point when the differential of the equation is 0. If your quadratic equation has a formula of ax^2 + bx + c = 0 then this point will be when x = -b/2a.

Whether it is a maximum opr minimum can be determined by looking at a. If a > 0 then its a minimum and if a < 0 then its a maximum (if a = 0 then its not a quadratic).

I hope that helps. If you haven't got the equation of the curve in this sort of form could you say what you have got to work from?

Edit: question has changed so that the curve is a section of a sine curve and not a quadratic any more. This answer is therefore no longer appropriate.

Edit2:

With a sine curve the general equation will be y = a sin(mx+t) + c. You will never be able to exactly determine the original equation because for any solution there will be a higher frequency solution that also matches. I'm unsure currently how many points are needed to precisely calculate what a would be (which would give the min and max of the curve).

Chris
all he has is a set of points....
Charles Bretana
I have a set of points to work from. As I mentioned I can generate as many of these points as I want, but I need to keep it to a minimum to keep the function efficient.
Mike Webb
He said that he could generate as many points as he wanted so from that I inferred he had an equation for the curve. You can get the equation for the curve from three points on that curve though so with three points you should then be able to use this method. I just need to work out how to solve the simultaneous equations in code...
Chris
The formula for the curve is an n-1 degree equation. For five points it's a fourth degree formula like y=ax^4+bx^3+cx^2+dx+e. You just have to derive it and determine where that is zero to find the min and max points.
Guffa
You have `y = ax^2 + bx + c` Plug in 3 points `(x1, y1), (x2, y2), (x3, y3)`, find out `a`, `b` and `c` and then this answer should be what you need.
IVlad
I was thinking more about the curve generated by the points I have and found that it will be in the shape of a sin/cos curve, not a quadratic curve. It will always be in this shape and will always be less than one phase. I edited my post accordingly.
Mike Webb
Less that one period, I mean.
Mike Webb
@Chris, The equation you get from just three points is only valid for those three points. It is not necessarly valid for any of the other points not used to generate the equation.
Charles Bretana
If all the points lay on a quadratic (which is what was being discussed when I said you only need three points) then the equation you get from three points is valid for all other points on that curve. With the curve now being a segment of an arbitrary sine curve it is a completely different situation and I've not thought hard enough yet about how to solve it thought you are right that three points will no longer be sufficient. And the more I think about it the more I think that for any finite set of points you *could* create a sine curve of arbitrary magnitude that would fit all of the points.
Chris
`With a sine curve the general equation will be y = a sin(mx+t) + c. You will never be able to exactly determine the original equation because for any solution there will be a higher frequency solution that also matches. ` regardless of the frequency, the min/max are c-a and c+a respectively
BlueRaja - Danny Pflughoeft
@BlueRaja - Danny Pflughoeft: Yes, the min/max is c-a and c+a but that isn't really an answer until you know what c-a and c+a are. And the frequency can make a difference. For any set of points and solution there is a higher frequency solution with a greater amplitude. I have too much maths scribbled down on paper in front of me to transcribe though. ;-)
Chris
A: 

Is all you have available the set of points? And are there no restrictions on the "shape" of the function these points represent? If so, then you're probably stuck, iterating through the points will be your best bet...
Although if you have any other work to do on this set, you might want to sort it by the Y-coord for use by future processing.

(Keep both arrays around - the one you were fed as input (it's probably sorted by x-corrd ?) and the one sorted by function value(y-coord) ) ...

Edit: if you know that the curve will always be shaped "like" a portion of Sin/Cos curve, then if you know the smallest period that might be represented, you can do some optimizations by using binary search algorithm to "look" for the inflection points (where the slope (Change in Y to the left and to the right ) are of different signs. For each point examined on the left, move to the right by chunks = half the allowable period until you find the point of inflection, or the slope changes sign... Then move back by half the last change in x, until you find the point of inflection. [Do the opposite for the points on the right]

A recursive routine that examines/finds the first and last inflection point, compares them to determine which is greatest, and then recursively examines and finds, the inlfection point halfway between them, until the two points involved are less than the smallest allowable period aprt from one another, will produce some performance gains...

2nd EDIT: Since I read in yr other comment that set will never contain more than one inflection point... If this so, then just do binary search to find it.

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
Charles Bretana
The shape will always be in the shape of some segment of a sin/cos curve, although it will always be less that one phase. I edited my post to include this info.
Mike Webb
Less that one period, I mean.
Mike Webb
@Mike, see my edit...
Charles Bretana
@Charles, this is what the algorithm does right now. The evaluation is good, but not efficient enough for what I need and creates lag in my program when continuously updating.
Mike Webb
Just finished edit. Is this what you already have ?
Charles Bretana
Yes, this method is what I already have.
Mike Webb
@Mike, Then I'm afraid I have no other better ideas... sorry!
Charles Bretana
@Charles Bretana, no worries. Thanks for the help.
Mike Webb
A: 

Since the curve is always quadratic (and thus always convex), there should be a lot of methods available (though since I don't program in c#, I don't know if source is available). Newton's method comes to mind first but there are others (like interior point methods). For a mathematical background of these algorithms (but not their implementations, unfortunately), see this textbook (pdf). If you do use any of these methods, they'll work for other convex curves as well.

Jan Gorzny
Actually, I didn't notice you only had a set of points. Not sure how applicable the answer is in this case, but check out the link if you're interested for convex optimization techniques in general.
Jan Gorzny
Now that I think about it it can possibly wind up being in the shape of a segment of a sin/cos curve, but always less than one whole phase. I've edited my post accordingly.
Mike Webb
In this case, if you can restrict the domain to a set where the function is convex (e.g. from pi to 2*pi on the standard sine graph), and find a convex optimization technique that applies to you, you can find a minimum. (And you can do a similar trick for finding the maximum.)
Jan Gorzny
Less than one period, I mean.
Mike Webb
If it's significantly less than one period and doesn't contain a point of inflection, the curve segment will always be either convex or concave.
Jan Gorzny
It may potentially contain a point of inflection, but never more than one. The range of theta is 0-359.999... The curve has some phase and amplitude shift and the Y point will never be negative.
Mike Webb
A: 

The algorithm you should use (and the parameters you give it) depends on what your data set looks like. It sounds like you are evaluating a waveform of some physical measurement that is acquired continuously.

If so, then you need to decide whether you want to ignore local minima and maxima (eg spikes from noise in the signal). Also, you'll want some way to handle the edges of your data set. In other words, does the beginning of your data count as a maxima if it is the highest point in the current dataset but is merely coming down from a big peak in the previous one?

Canned peak detection algorithms will typically have some way to specify a threshold and also a width (to control sensitivity to spikes) and a buffer size (to handle really gradual peaks).

There are plenty of algorithms out there, just pick one or two and tweak parameters until it gives the results you expect.

Angelo
Could you direct me to somewhere I can find them? Should I just google "Peak Detection Algorithms"? Also, in answer to your question, the set is very constrained. It is a waveform generated by the points [belt length vs idler position] for a belt drive with an idler attached, much like the serpentine belt drive in a car engine.
Mike Webb
The need for continuous update comes from the need to update the min and max when a user of my program alters the drive design on a mouse-move event.
Mike Webb
Also, the beginning and end both count, as you asked above.
Mike Webb
AFAIK peak detection algorithms include stuff similar to what bretana described in the previous post. However, a critical step that is usually performed is some type of filtering before the search. You'll want to filter the waveform just enough so that peaks which are much narrower (or much wider) than your expected peak get ignored. This way, it is possible to skip a larger number of points when taking slope measurements in your data set. Being able to take large skips in the search will make it faster.
Angelo
A: 

After you collected a few points (>=4) you could use a form of local search to match your points to a sine curve y = A cos(Bx+C)+D then use a simple formula based on the derivative to find the minimum. While searching, you should keep B as little as possible to avoid redundant hi-frequency solutions. Just an idea, may be very inefficient.

Mau
A: 

From the comment the input X and the output Y are arrays

"@Mike:I generate the values and put them in an array"

I suggest to use this approach. All what you need from my code is {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;
    }

I hope that will be fast.

Waleed A.K.
A: 

I'm a little confused.

If you yourself are generating the points, why not just keep track of the largest/smallest points when doing the generating?

If you have a function, like I'm sure others have pointed out, just get the derivative and solve for 0. This will give you the point(s) of min/max.

Noon Silk