Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi monks, I am trying to write a program which basically reads in an excel file containing the coordinates of a graph. I am then trying to get the values corresponding to the peak of the graph. So far my program can do this for any given coordinate. However, how can I calculate the peak value on a graph if I do not know the coordinates of it??? The graphs I'm dealing with look like the ones below. If the '*' at the peak of the graph wasn't there how would I decipher the x-axis value for it?? thanks..
| * | * * | * * | * * |* * |_____________________

Replies are listed 'Best First'.
Re: graphs
by UnderMine (Friar) on Dec 05, 2002 at 16:38 UTC
    If I have understood this correctly what you are asking is it there an easy way to find an line of best approxation and then find the max point of that line.

    This is really a maths problem and finding the correct Math::* module is a pain as Math::Brent shows the documentation on CPAN is very bad.

    PS:Math::Brent from the POD
    This is an implementation of Brents method for One-Dimensional minimisation of a function without using derivatives. This algorithm cleverly uses both the Golden Section Search and parabolic interpolation.

    Now where is a module that matches a polynomial function to a dataset? Was it Math::Polynimial or is that something different?

    PS:Math::Polynomial from the POD
    This module implements single variable polynomials using arrays. It also implements some useful functionality when working with polynomials, such as adding, multiplication, etc.

    Nope that doesn't help find the polynomial but could be used to find points of inflections.

    Think I have got it Math::Bezier (Wow some docs) to map your graph to a curve and then use Math::Brent to find the max value of y.

    Hope this helps
    UnderMine

    Updated Added Math::Polynomial from pod.

    Updated Added Math::Beziar

Re: graphs
by BrowserUk (Patriarch) on Dec 05, 2002 at 18:13 UTC

    If its important that the value you derive relates to the peak as displayed on the graph generated by Excel, then deriving that value mathematically will require you to effectively reverse engineer the curve fitting algorithm that Excel uses. Unless this happens to be well documented, you are likely to have quite a hard time.

    Given that the point of interest is being interpolated from sets of discrete inputs, it is at best, a guess. So using that instead of say the nearest, highest X value from the data is really just deriving false accuracy.

    However, if you still wish to do this, there is a way that you could do it without needing to redo all the math.

    The approach would be to import the graph itself from Excel as a bitmap, and then the scan the pels from the top, line by line until you encounter 1 or more pels set to the line color. You would need to skip over any pels on the left that represent the Y-Axis and Y-axis labels, but that should just mean starting at a constant offset.

    Importing the bitmap from Excel using Win32:::OLE should be a snap. You could probably use GD and $image->getPixel($x,$y) to inspect and detect the pel(s) of interest.

    One problem is that I don't see any GD methods for importing Windows style bitmaps. You can import JPG's and PNG's, but both are lossy anyway, and as GD operates on 8-bit pixels the conversion from 24-bit could muddy the waters somewhat.

    Just another of my lateral thinking exercises. Good luck.


    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: graphs
by gjb (Vicar) on Dec 05, 2002 at 16:42 UTC

    I'm not sure I get your question right, but I think you want to fit a curve through the data points you have and then find the maximum of that continuous function. If so, you'd best consult a book on numerical methods such as Numerical recipes in C (online).

    Here you'll find curve fitting methods (more than you care) as well as methods to find a function's extrema.

    Hope this helps, -gjb-

Re: graphs
by Callum (Chaplain) on Dec 05, 2002 at 16:20 UTC
    I may be mis-interpreting your query but surely your graph co-ordinates must comprise some mapping of y-coordinate to x-coordinate, probably simple x,y pairs, just look to see what the highest value of y is and then take the corresponding x value.

      i've tried that, but the highest y-value is not neccessarily the highest point on the graph. e.g the graph contains temperature readings which are taken by a machine e.g. every 0.5 degrees, the graph that is printed out doesn't necessarily have coordinates for the peak on the graph. ;-)