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

Hello Monks- I have the following script to generate a 100 trial solutions of A and B, uniformly distributed between 0 and A_max and 0 and B_max, respectively. I want to choose a trial pair of A and B which will minimize a certain error term. What I don't understand is how to "store" the 10000 trial solution pairs so they can be referred to by the script. Also, if I wanted to print the 100 solutions, e.g. for A, how can that be done? I only get 1 solution for A when I run just the t and A loops and print $A. Here's the (incomplete) code, I put a space between the lines to help readability:

#!/usr/bin/perl $A_max = 1e-05, $B_max = 1e-04, $t_max = 200, $z_best = 1e+12; for ($t=0; $t<=$t_max; $t++) { for ($A=0; $A<=$A_max; $A+=$A_max/100) { for ($B=0; $B<=$B_max; $B+=$B_max/100) { #something here that should refer to $i_max; $i_max = 10000 because I expect 10000 pairs of A and B solution pairs. #somehow use the 10000 trial solution pairs of $A and $B to sub +stitute into the equation, $er_max = |A| + |B|. #set $er_max = $z, corresponding to the error calculated for each + trial solution pair. for ($i=0; $i<$i_max; $i++) { if ($z < $z_best) { $z_best = $z; $A_best = $A; $B_best = $B; } } } } } print "$A = $A_best\n"; print "$B = $B_best\n";

Replies are listed 'Best First'.
Re: Expecting a 100 solutions, getting just 1
by moritz (Cardinal) on Apr 17, 2012 at 08:35 UTC

    It's hard to understand what you are trying to do. What is a "trial solution"? Is there some randomness involved?

    If you want to store multiple somethings, you best use an array:

    my @interesting_values; for (1...1000) { my $r = rand; if ($r > 0.5) { push @interesting_values, $r; } }

    See also: perldata, perlintro.

    This part of your code:

    for ($i=0; $i<$i_max; $i++) { if ($z < $z_best) {

    seems to be entirely useless, because you never set $z to anything, and there's no reason to iterate over that if-statement, since nothing in there is random or depends on $i.

    Also please Use strict and warnings and declare your variables with my.

      There is randomness involved because 1 value of $A is a guess. The objective is to choose a pair of guesses A and B which minimize the error term. Also, will declaring the variables with my get around the scalar property of $? Thanks for the tip about the warnings.
        There is randomness involved

        Where? I don't see any calls to rand, or other sources of randomness in your code

        because 1 value of $A is a guess.

        A guess for what?

        The objective is to choose a pair of guesses A and B which minimize the error term.

        What's the error term? How is it calculated?

        Also, will declaring the variables with my get around the scalar property of $?

        No. You're not supposed to work around it, you are supposed to use variables starting with @ if you want multiple values in one variable.

Re: Expecting a 100 solutions, getting just 1
by Anonymous Monk on Apr 17, 2012 at 08:44 UTC
Re: Expecting a 100 solutions, getting just 1
by brx (Pilgrim) on Apr 17, 2012 at 09:33 UTC
     $A_max = 1e-05, $B_max = 1e-04, $t_max = 200, $z_best = 1e+12;

    Wrong separator ! Should be my $A_max = 1e-05; my $B_max = 1e-04; my $t_max = 200; $z_best = 1e+12;

    You have to use strict; use warnings;

    Perlish loops are simple to read

     for my $t (0 .. $_tmax) {...}

     for my $A_part (0 .. 100) { $A=$A_max*$A_part/100; ...}

    Probably better because of floating point errors (when you add a lot of non-interger numbers)