Hi Ralph,

If you run your program with "use strict;", it'll catch the problem --
when you assign to $xdata[$i], that's the $i-th element of the Perl
array @xdata, not the pdl value $xdata.

To access an individual value, you need to use at, set, or the slicing
functions in PDL::NiceSlice.  So you can replace your loop with
something like:

for $i (0..15) {
   $xdata->set($i,$i);
   $ydata->set($i,$i * $i);
   print "i ", $i, " x ", $xdata->at($i), " y ", $ydata->at($i), "\n";
}

or

use PDL::NiceSlice;
for $i (0..15) {
   $xdata($i)=$i;
   $ydata($i)=$i * $i;
}
print "x: $xdata\ny: $ydata\n";

or you can do it with no loop at all:

$xdata=sequence float, 16;
$ydata=$xdata*$xdata;

Best,
Robert

On Wed, Aug 10, 2011 at 12:30 PM, Ralph Castain <[email protected]> wrote:
> Hi folks
>
> Afraid I have to ask an embarrassingly noobie question. I'm trying to get a 
> fitting program to work, and thought I would use the fitpoly1d function to do 
> it. However, I'm having trouble getting a very basic example to work, 
> probably because I'm not understanding the PDL data system.
>
> I thought I would just try a simple quadratic fit for the example. The usual 
> perl methods for generating the data arrays don't seem to work, but having 
> perused the PDL site plus various examples I found on the web, I managed to 
> get something to at least run (see below).
>
> The problem is that the fit comes out as linear, and I can't seem to access 
> the output values. I'm sure I'm just being stupid, but can someone point out 
> the obvious mistake?
>
> Thanks
> Ralph
>
> #!/opt/local/bin/perl
> #
>
> use PDL;
> use PDL::MATH;
> use PDL::Fit::Polynomial;
>
> my $xdata = sequence float, 16;
> my $ydata = sequence float, 16;
>
> my $i = 0;
> while ($i < 16) {
>    $xdata[$i] = $i;
>    $ydata[$i] = $i * $i;
>    print "i ", $i, " x ", $xdata[$i], " y ", $ydata[$i], "\n";
>    $i++;
> }
>
> ($yfit, $coeffs) = fitpoly1d $xdata, $ydata, 3;
>
> print "Fit complete with coeffs ", $coeffs, "\n";
>
> print "Fitted values ", $yfit, "\n";
>
> $i = 0;
> while ($i < 16) {
>    print "i ", $i, " yfit ", $yfit[$i], "\n";
>    $i++;
> }
>
>
> Output:
>
> $ ./pdl.pl
> i 0 x 0 y 0
> i 1 x 1 y 1
> i 2 x 2 y 4
> i 3 x 3 y 9
> i 4 x 4 y 16
> i 5 x 5 y 25
> i 6 x 6 y 36
> i 7 x 7 y 49
> i 8 x 8 y 64
> i 9 x 9 y 81
> i 10 x 10 y 100
> i 11 x 11 y 121
> i 12 x 12 y 144
> i 13 x 13 y 169
> i 14 x 14 y 196
> i 15 x 15 y 225
> Fit complete with coeffs [-2.6645353e-14          1 -1.4210855e-15]
> Fitted values [-2.6645353e-14  1.0000001  2.0000001          3  4.0000002  
> 5.0000001  6.0000001          7  8.0000004  9.0000004         10         11   
>       12         13         14         15]
> i 0 yfit
> i 1 yfit
> i 2 yfit
> i 3 yfit
> i 4 yfit
> i 5 yfit
> i 6 yfit
> i 7 yfit
> i 8 yfit
> i 9 yfit
> i 10 yfit
> i 11 yfit
> i 12 yfit
> i 13 yfit
> i 14 yfit
> i 15 yfit
>
>
>
> _______________________________________________
> Perldl mailing list
> [email protected]
> http://mailman.jach.hawaii.edu/mailman/listinfo/perldl
>

_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

Reply via email to