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

How can I get this code to substitute the new values of lambda_max and cro_max (at the end of the code) as initial values, and be able to print them for t = 0, t = 1, t = 2 etc. **update** Thank you everyone! Made some stupid mistakes because of being tired, sheeessh. Anyways, if there's a quick/easy way to take the values of cro_next and lambda_next and feed them into the loop all over so I don't have to do it manually, I would appreciate that. I want lambda_next and cro_next values for t = 1, t = 2, ... t = 200. Thanks again!

#!/usr/bin/perl $lambda_max = 1e-07; $cro_max = 0; $dna_max = 1e-07; $K1 = 1e+08; $K3 = 1e+05; $KC = 1e+08; $ks = 20; $kdeg = 0.1; $t_max = 200; for ($t=0; $t<=$t_max; $t++) { $error_best = 1e+12; for ($lambda=0; $lambda<$lambda_max; $lambda+=$lambda_max/100) + { for ($cro=0; $cro<$cro_max; $cro+=$cro_max/100) { $c1 = 1 + $K1 * $lambda + $K1**2 * $lambda**2; $c3 = 1 + $K3 * $lambda + $KC * $cro; $c = $dna_max / (($c1) * ($c3)); $r2 = $c * $K1 * $lambda; $r3 = $c * $K3 * $lambda; $r4 = $c * $K1**2 * $lambda**2; $r5 = $c * $K1 * $K3 * $lambda**2; $r6 = $c * $K1**2 * $K3 * $lambda**3; $r7 = $c * $KC * $cro; $r8 = $c * $K1 * $KC * $lambda * $cro; $r9 = $c * $K1**2 * $KC * $lambda**2 * $cro; $lamOR1 = $r2 + $r4 + $r5 + $r6 + $r8 + $r9; # $lamOR2 = $r4 + $r6 + $r9; # eq. 6 $lamOR3 = $r3 + $r5 + $r6; # eq. 6 $croOR3 = $r7 + $r8 + $r9; # eq. 6 $x1 = $lambda + $lamOR1 + $lamOR2 + $lamOR3 - $lambda_max; $x2 = $cro + $croOR3 - $cro_max; $error = abs($x1) + abs($x2); if ($error < $error_best) { $error_best = $error; $lambda_best = $lambda; $cro_best = $cro; } } } $c1_best = 1 + $K1 * $lambda + $K1**2 * $lambda**2; $c3_best = 1 + ($K3 * $lambda) + ($KC * $cro); $c_best = $dna_max / (($c1_best) * ($c3_best)); $r2_best = $c * $K1 * $lambda; $r3_best = $c * $K3 * $lambda; $r4_best = $c * $K1**2 * $lambda**2; $r5_best = $c * $K1 * $K3 * $lambda**2; $r6_best = $c * $K1**2 * $K3 * $lambda**3; $r7_best = $c * $KC * $cro; $r8_best = $c * $K1 * $KC * $lambda * $cro; $r9_best = $c * $K1**2 * $KC * $lambda**2 * $cro; # concentrations of lambda and/or cro at specific sites $lamOR1_best = $r2_best + $r4_best + $r5_best + $r6_best + $r8_bes +t + $r9_best; # eq. 6 $OR1_best = $dna_max - $lamOR1_best; $lamOR2_best = $r4 + $r6 + $r9; # eq. 6 $lamOR3_best = $r3 + $r5 + $r6; # eq. 6 $croOR3_best = $r7 + $r8 + $r9; # eq. 6 #max terms $lambda_max_best = $lambda + $lamOR1_best + $lamOR2_best + $lamOR3_ +best; # equation 1 $cro_max_best = $cro + $croOR3_best; # equation 3 # r4 term - lambda synthesis $rL1 = $K1**2 * $lambda**2; $rL2 = (1 + ($K1 * $lambda) + ($K1**2 * $lambda**2)) * (1 + ($K3 * +$lambda) + ($KC * $cro)); $rL3 = ($rL1/$rL2) * $dna_max; # next value of lambda- synthesis of more lambda - only by dna spec +ies r4 $lambda_next = ($lambda_max_best + ($ks * $rL3)) * (1 - $kdeg); # next value of cro- synthesis of more cro - only when OR1 is unbou +nd $cro_next = ($cro_max_best + ($ks * $OR1_best)) * (1 - $kdeg); } print "$lambda_next\n"; print "$cro_next\n";

Replies are listed 'Best First'.
Re: making code efficient
by davido (Cardinal) on Apr 18, 2012 at 04:14 UTC

    Are you aware of the following problems in your updated code?

    Name "main::r1" used only once: possible typo at mytest.pl line 23. Name "main::rL3" used only once: possible typo at mytest.pl line 59. Name "main::OR1" used only once: possible typo at mytest.pl line 63.

    These are the offending lines:

    $r1 = $c; # $r1 Used only once. # .... $lambda_next = ($lambda_max + ($ks * $rL3)) * (1 - $kdeg); # $rL3 used + only once. # .... $cro_next = ($cro_max + ($ks * $OR1)) * (1 - $kdeg); # $OR1 used only +once.

    The second and third cases are most alarming, since you're using $rL3 and $OR1 in computations while they (apparently) have undef as their values.

    Update: As for efficiency, you could completely eliminate the inner loop, since the condition $cro < $cro_max will never be true, as you set $cro_max to zero at the top of your script. ...I suppose that won't do much for computational efficiency; a loop that never runs steals very few cycles. But it will save a few keystrokes.


    Dave

Re: code not working - for loops and storing values
by NetWallah (Canon) on Apr 18, 2012 at 02:51 UTC
    $cro_max is ZERO, and $i_max is unspecified (undef, value zero).

    So, your loops are not doing much.

    Update:Please do not modify your posts after posting (Other than to correct minor typos)
    If you want to update information, post UPDATEs, like this one, that retain the context of the original post.

    To answer your new question - Use sub (Soubroutines), and pass parameters.

                 All great truths begin as blasphemies.
                       ― George Bernard Shaw, writer, Nobel laureate (1856-1950)

Re: making code efficient
by snape (Pilgrim) on Apr 18, 2012 at 04:25 UTC

    I am not able to understand your code. The $error has no value as $cro_max = 0 and therefore it is giving uninitialized value for error. Also r1 is not used after it has been initialized and two of your values $rL3 and $OR1 values are not initialized or defined.Please briefly state what are doing and what you want to do ?

Re: making code efficient
by snape (Pilgrim) on Apr 18, 2012 at 03:41 UTC

    Please look at the formatting tips before posting your questions. Could you please update ur question ?