| 1 | use strict; |
| 2 | use warnings; |
| 3 | use Test::More; |
| 4 | use PDL::LiteF; |
| 5 | use PDL::IO::Misc 'rcols'; |
| 6 | use PDL::Fit::LM; |
| 7 | |
| 8 | my ($t,$count,$sigma)=rcols(\*DATA,0,1,2); |
| 9 | my $initp = pdl(10,900,80,27,225); |
| 10 | my $gnuplot_pf_unweighted = pdl(7.96, 282.5, 70.0, 28.5, 117.7); |
| 11 | my $gnuplot_pf_weighted = pdl(5.53, 290.7, 46.6, 33.3, 162.7); |
| 12 | |
| 13 | my ($yf,$pf,$cf,$if) = lmfit($t, $count, 1, \&const_2exp, $initp); |
| 14 | ok(all(abs(log10($pf/$gnuplot_pf_unweighted))<0.02),"Unweighted fit"); |
| 15 | |
| 16 | ($yf,$pf,$cf,$if) = lmfit($t, $count, $sigma, \&const_2exp, $initp); |
| 17 | ok(all(abs(log10($pf/$gnuplot_pf_weighted))<0.02),"Weighted fit"); |
| 18 | |
| 19 | done_testing; |
| 20 | |
| 21 | sub const_2exp{ |
| 22 | #constant plus 2 exponentials |
| 23 | my ($x,$par,$ym,$dyda) = @_; |
| 24 | my ($a1,$a2,$a3,$a4,$a5) = map { $par->slice("($_)") } (0..4); |
| 25 | $ym .= $a1 + $a2*exp(-$x/$a4) + $a3*exp(-$x/$a5); |
| 26 | my (@dy) = map {$dyda -> slice(",($_)") } (0..4); |
| 27 | $dy[0] .= 1; |
| 28 | $dy[1] .= exp(-$x/$a4); |
| 29 | $dy[2] .= exp(-$x/$a5); |
| 30 | $dy[3] .= $a2 * $x * exp(-$x/$a4)/$a4/$a4; |
| 31 | $dy[4] .= $a3 * $x * exp(-$x/$a5)/$a5/$a5; |
| 32 | } |
| 33 | |
| 34 | __DATA__ |
| 35 | # $Id: silver.dat,v 1.1.1.1 1998/04/15 19:16:42 lhecking Exp $ |
| 36 | # This sample data was distributed with Gnuplot, which contains the following notice: |
| 37 | # Copyright (C) 1986 - 1993, 1998, 2004, 2007 Thomas Williams, Colin Kelley |
| 38 | # Permission to use, copy, and distribute this software and its |
| 39 | # documentation for any purpose with or without fee is hereby granted, |
| 40 | # provided that the above copyright notice appear in all copies and that |
| 41 | # both that copyright notice and this permission notice appear in |
| 42 | # supporting documentation. |
| 43 | 10.000000 280.000000 16.733201 |
| 44 | 20.000000 191.000000 13.820275 |
| 45 | 30.000000 152.000000 12.328828 |
| 46 | 40.000000 150.000000 12.247449 |
| 47 | 50.000000 104.000000 10.198039 |
| 48 | 60.000000 77.000000 8.774964 |
| 49 | 70.000000 69.000000 8.306624 |
| 50 | 80.000000 60.000000 7.745967 |
| 51 | 90.000000 60.000000 7.745967 |
| 52 | 100.000000 51.000000 7.141428 |
| 53 | 110.000000 41.000000 6.403124 |
| 54 | 120.000000 34.000000 5.830952 |
| 55 | 130.000000 35.000000 5.916080 |
| 56 | 140.000000 34.000000 5.830952 |
| 57 | 150.000000 24.000000 4.898979 |
| 58 | 160.000000 24.000000 4.898979 |
| 59 | 170.000000 19.000000 4.358899 |
| 60 | 180.000000 21.000000 4.582576 |
| 61 | 190.000000 20.000000 4.472136 |
| 62 | 200.000000 18.000000 4.242641 |
| 63 | 210.000000 21.000000 4.582576 |
| 64 | 220.000000 15.000000 3.872983 |
| 65 | 230.000000 19.000000 4.358899 |
| 66 | 240.000000 12.000000 3.464102 |
| 67 | 250.000000 20.000000 4.472136 |
| 68 | 260.000000 20.000000 4.472136 |
| 69 | 270.000000 18.000000 4.242641 |
| 70 | 280.000000 18.000000 4.242641 |
| 71 | 290.000000 20.000000 4.472136 |
| 72 | 300.000000 12.000000 3.464102 |
| 73 | 310.000000 26.000000 5.099020 |
| 74 | 320.000000 17.000000 4.123106 |
| 75 | 330.000000 8.000000 2.828427 |
| 76 | 340.000000 6.000000 2.449490 |
| 77 | 350.000000 8.000000 2.828427 |
| 78 | 360.000000 10.000000 3.162278 |
| 79 | 370.000000 20.000000 4.472136 |
| 80 | 380.000000 14.000000 3.741657 |
| 81 | 390.000000 8.000000 2.828427 |
| 82 | 400.000000 10.000000 3.162278 |
| 83 | 410.000000 9.000000 3.000000 |
| 84 | 420.000000 8.000000 2.828427 |
| 85 | 430.000000 10.000000 3.162278 |
| 86 | 440.000000 13.000000 3.605551 |
| 87 | 450.000000 9.000000 3.000000 |
| 88 | 460.000000 5.000000 2.236068 |
| 89 | 470.000000 7.000000 2.645751 |
| 90 | 480.000000 11.000000 3.316625 |
| 91 | 500.000000 7.000000 2.645751 |
| 92 | 510.000000 9.000000 3.000000 |
| 93 | 520.000000 12.000000 3.464102 |
| 94 | 530.000000 4.000000 2.000000 |
| 95 | 540.000000 7.000000 2.645751 |
| 96 | 550.000000 10.000000 3.162278 |
| 97 | 560.000000 9.000000 3.000000 |
| 98 | 580.000000 8.000000 2.828427 |
| 99 | 590.000000 9.000000 3.000000 |
| 100 | 600.000000 5.000000 2.236068 |