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

Hello, The following code, which finds the optimal price a company should charge to maximize profit, works fine:
#!/usr/bin/perl #finalproject_1.pl use strict; our $p1 = 0; our $p2 = 0; our $pT = 10; our $a = 0.1; our $b = 0.1; our $k = 0.005; our $d2 = 0.5; our $d1i = 1000; our $ci = 200; our @profit; our @price; my $profit = &profit(); our $A; our $B; our $C; our $s; while ($p1 < 10){ $p1 += .01; $p2 = $p1; $A = ($a*($p1-$p2) + $b*($pT-$p1)/(-2)); $B = ($p1*($a*($p2-$p1) + $b*($pT-$p1)-$k*$d2-$d1i*$k)); $C = ($d1i - $ci); $s = (((-1)*$B + sqrt($B**2 - 4*$A*$C))/(2*$A)); $profit = &profit(); push @price, $p1; push @profit, $profit; } our @alias = @profit; my $maxprofit = pop @alias; foreach (@alias){ $maxprofit = $_ if $maxprofit < $_; } our $index = 0; ++$index until $profit[$index] == $maxprofit or $index > $#profit; print "The largest profit is $maxprofit and is obtained when price is +$price[$index] \n"; #Here thar be subroutines sub profit { (($A*$s**3)/3 + ($B*$s**2)/2 + $C); }
But when I try to get the optimal prices as demand changes, perl prints the same price over and over again:
#!/usr/bin/perl #finalproject_2.pl use strict; use Math::Complex; our $p1 = 0; our $p2 = 0; our $pT = 10; our $a = 0.1; our $b = 0.1; our $k = 0.005; our $d2 = 0.5; our $d1i = 0; our $ci = 200; our @profit; our @price; my $profit = &profit(); our $A; our $B; our $C; our $s; while ($d1i <= 9000){ while ($p1 < 10){ $p1 += .01; $p2 = $p1; $A = ($a*($p1-$p2) + $b*($pT-$p1)/(-2)); $B = ($p1*($a*($p2-$p1) + $b*($pT-$p1)-$k*$d2-$d1i*$k)); $C = ($d1i - $ci); $s = (((-1)*$B + sqrt($B**2 - 4*$A*$C))/(2*$A)); $profit = &profit(); push @price, $p1; push @profit, $profit; } our @alias = @profit; my $maxprofit = pop @alias; foreach (@alias){ $maxprofit = $_ if $maxprofit < $_; } my $index = 0; ++$index until $profit[$index] == $maxprofit or $index > $#profit; print "$d1i\t\t\t$price[$index]\t\t$index\n"; $d1i+=100; } #Here thar be subroutines sub profit { (($A*$s**3)/3 + ($B*$s**2)/2 + $C); } Please help!

Replies are listed 'Best First'.
Re: Bad While Loop
by pvaldes (Chaplain) on Oct 25, 2011 at 23:25 UTC
    our @alias = @profit; my $maxprofit = pop @alias; foreach (@alias){ $maxprofit = $_ if $maxprofit < $_; } our $index = 0; ++$index until $profit[$index] == $maxprofit or $index > $#profit;

    ...

    use List::Util qw(max); my $maxprofit = max @profit; our $index = grep { $profit[$_] eq $maxprofit } 0..$#profit;
      Thanks for the tip, but my original problem persists...it appears as if all variables remain unchanged throughout the loop

        lets see

        $A = ($a*($p1-$p2) + $b*($pT-$p1)/(-2)); $B = ($p1*($a*($p2-$p1) + $b*($pT-$p1)-$k*$d2-$d1i*$k));

        In your script $p1 = $p2, so this is the same as:

        $A = -0.5*$b*($pT-$p1); $B = $b*($pT-$p1)-$k*($d2-$d1i);

        What's the purpose of the var $p2?. Maybe you want to say:

        $p2 = $p1; $p1 += .01;

        Instead

         $p1 += .01; $p2 = $p1; ?

        Check that your equations are valid

Re: Bad While Loop
by pvaldes (Chaplain) on Oct 25, 2011 at 23:01 UTC
    while ($d1i <= 9000){

    $d1i pass unchanged inside the loop so is always 0 and this is always true, so I don't understand the purpose of this loop. Maybe you want a for (0..9000) loop?

        yes, fixed
Re: Bad While Loop
by pvaldes (Chaplain) on Oct 26, 2011 at 00:31 UTC
    two, check that the sub that you call before to define it is really available and that my $vars are the same inside and outside loops