riceboyyy has asked for the wisdom of the Perl Monks concerning the following question:
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_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); }
#!/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 | |
by riceboyyy (Novice) on Oct 25, 2011 at 23:34 UTC | |
by pvaldes (Chaplain) on Oct 25, 2011 at 23:56 UTC | |
by riceboyyy (Novice) on Oct 26, 2011 at 00:25 UTC | |
|
Re: Bad While Loop
by pvaldes (Chaplain) on Oct 25, 2011 at 23:01 UTC | |
by riceboyyy (Novice) on Oct 25, 2011 at 23:14 UTC | |
by pvaldes (Chaplain) on Oct 25, 2011 at 23:30 UTC | |
|
Re: Bad While Loop
by pvaldes (Chaplain) on Oct 26, 2011 at 00:31 UTC |