in reply to mathsy question: finding max of curve from discrete points

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
  • Comment on Re: mathsy question: finding max of curve from discrete points (two steps)
  • Download Code

Replies are listed 'Best First'.
Re: Re: mathsy question: finding max of curve from discrete points (two steps)
by tall_man (Parson) on May 07, 2003 at 19:48 UTC
    Thanks for the pointer to the PDL package, tye. Here is the polynomial fit I did above converted to use piddles. But as you say, polynomials may not be the best thing to use.
    use strict; use PDL::LiteF; use PDL::Fit::Polynomial; my $xp = pdl (62.6, 62.8, 63, 63.3, 63.5, 63.7, 64) +; my $yp = pdl (7.476, 7.5219, 7.5464, 7.5516, 7.5504, 7.5372, 7. +518); my ($yfit,$coeffs) = fitpoly1d($xp,$yp,3); my $diffp = $yp - $yfit; my @theta = list $coeffs; print "Coefficients: ",join(",",@theta),"\n"; my $c = $theta[0]; my $b = $theta[1]; my $a = $theta[2]; print "\nComputed results:\n"; my $xtop = $xp->nelem - 1; for my $i (0..$xtop) { my $x = at($xp,$i); my $y = at($yfit,$i); my $ytrue = at($yp,$i); my $diff = at($diffp,$i); print "$x\t$y\t($ytrue)\t$diff\n"; } ## Solve for the derivative being 0. my $maxx = -$b/(2.0*$a); my $maxy = ($a*$maxx + $b)*$maxx + $c; print "\nPeak point:\n"; print "$maxx\t$maxy\n";