First, you need to "fit" a curve to your data points. There are pretty standard methods for doing this (and they all boil down to linear approximation and they got really boring when I was studying them) by building a function out of a bunch of "basis" functions. Note that polynomials pretty much suck for this type of thing.

This process is called "fitting" a curve to a set of points. A quick CPAN search for fit turned up PDL::Fit modules that can do this. It looks like they require you to provide your own set of basis functions (or use polynomials which, unfortunately, suck), which is nice if they'd at least let you pick from a few of the classic sets that work well for fitting curves to data.

When I was getting out of this field, they were just realizing that "wave packet" functions were exceptionally good for "fitting". I'd think that a bit more research, probably in the direction of PDL, would turn up a good source of functions to fit to your data.

Once you've got your basis functions and have fit your data, you'll have a function that defines a curve.

Finding a local maximum of a curve is a classic problem. If you don't also have the derivative of your function, then a rather simple technique is to start with a $x and $step:

sub findMax { my( $f, $x, $step, $eps )= @_; while( 1 ) { my $y1= $f->( $x ); $x += $step; # March forward one more step. my $y2= $f->( $x ); if( $y2 < $y1 ) { # Oops, marched past it: $step /= -2; # Turn around, smaller steps. if( $step < $eps ) { # Close enough: return $x + $step; # Midpoint of last two steps. } } } } my $func= sub { ... }; my $x= findMax( $func, 63.3, 0.1, 0.000001 ); print "Max of ", $func->($x), " at $x.\n";
I suspect that other methods are provided by things like PDL, though I didn't run into any with a quick search.

                - tye

In reply to Re: mathsy question: finding max of curve from discrete points (two steps) by tye
in thread mathsy question: finding max of curve from discrete points by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.