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!

In reply to Bad While Loop by riceboyyy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.