[system] / trunk / pg / macros / PGdiffeqmacros.pl Repository:
ViewVC logotype

View of /trunk/pg/macros/PGdiffeqmacros.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3540 - (download) (as text) (annotate)
Sun Aug 21 05:21:07 2005 UTC (14 years, 5 months ago) by jj
File size: 25180 byte(s)
level_curve_check now forces the use of the old style (not Parser::Legacy)
answer evaluators resolving bug 820.

    1 #163macros.pl
    2 #macros for Prills 163 problems
    3 
    4 #!/usr/bin/perl -w
    5 #use strict;
    6 #use Carp;
    7 BEGIN {
    8   be_strict();#all variables must be declared local or global
    9 }
   10 
   11 #my @answer = oldivy(1,2,1,8,4);
   12 #print ("The old program says:\n");
   13 #print ($answer[0]);
   14 #print ("\n");
   15 #@answer = ivy(1,2,1,8,4);
   16 #print ("My program says:\n");
   17 #print ($answer[0]);
   18 #print ("\n");
   19 
   20 #the subroutine is invoked with arguments such as complexmult(2,3,4,5) or
   21 #complexmult(@data), where @data = (2,3,4,5)
   22 
   23 sub complexmult {
   24     my ($S,$T,$U,$V) = @_;
   25 #this line defines the input arguments as local variables
   26     my $R = $S *$U -$T * $V;
   27     my $I = $S *$V + $T * $U ;
   28     ($R,$I) ;#this returns ($R,$I) from the subroutine
   29 }
   30 ##########
   31 #sub addtwo adds two strings formally
   32 #An "indicator"  for a string is a
   33 # number ,e.g. coefficient,which indicates
   34 #whether the string is to be
   35 #added or is to be regarded as zero.
   36 #The  non-zero terms are formally added as strings.
   37 #The input is an array
   38 #($1staddend, $1stindicator,$2ndaddend,$2ndindicator)
   39 #The return is an array
   40 #(formal sum, indicator of formal sum)
   41 
   42 sub addtwo {
   43    my ($A,$a,$B,$b) = @_;
   44     my $out = "0";
   45     if ($a  != 0) {
   46   $out= $A;
   47     }
   48 
   49 
   50     if ($b  != 0) {
   51   if ($a  == 0) {
   52       $out= $B ;
   53   } else {
   54       $out = "$A + $B";
   55   }
   56     }
   57     my $ind = abs($a) + abs($b);
   58     ($out,$ind);
   59 }
   60 ########
   61 
   62 #sub add generalizes sub addtwo to more addends.
   63 #It formally adds the nonzero terms.
   64 # The input is an array of even length
   65 # consisting of each addend,a string,
   66 # followed by its indicator.
   67 
   68 sub add {
   69     # this function takes the first two terms, puts them together, and keep repeating until you have emptied the list.
   70     my @sum = ("0" ,0);
   71     my @list = @_;
   72     my $el = @list;
   73     my $x = "0";
   74     my $y = 0;
   75     while ($el > 0) {
   76   $x = shift(@list);
   77   $y = shift(@list);
   78   push(@sum,$x,$y);
   79   @sum = addtwo(@sum);
   80   $el = @list;
   81     }
   82     @sum ;
   83 }
   84 #######
   85 
   86 # sub diffop cleans up the typed expression
   87 #of a diff. operator.
   88 #input @diffop =($A,$B,$C) is the coefficients.
   89 #input is given as arguments viz difftop($A,$B,$C);
   90 #output is the diff. operator as a string $L in TEX
   91 
   92 sub diffop
   93 {
   94     my ($A,$B,$C) = @_ ;
   95     my ($LDD, $LD ,$LF) = ($A."y'' ",$B."y' ",$C."y ");
   96     # re-write 1y'' as y''.
   97     if ($A==1){
   98   $LDD = "y''";
   99     }
  100     # re-write 1y' as y'
  101     if ($B==1) {
  102   $LD = "y'";
  103     }
  104     # re-write -1y' as -y'
  105     elsif ($B==-1) {
  106   $LD = "-y'";
  107     }
  108     # re-write 1y as y
  109     if ($C==1) {
  110   $LF = "y";
  111     }
  112     # re-write -1y as -y
  113     elsif ($C==-1) {
  114   $LF = "-y";
  115     }
  116     my ($L,$ind) = add($LDD,$A,$LD,$B,$LF,$C);
  117     $L;
  118 }
  119 
  120 ########
  121 
  122 #sub rad simplifies (a/b)*(sqrt(c))
  123 #input is given as arguments on rad viz.: rad($a,$b,$c);
  124 #$a,$b,$c are integers and $c>=0 and $b is not zero.
  125 #output is an array =(answer as string,new$a,new$b, new$c)
  126 
  127 sub rad{
  128     # initalize primes
  129     my @p = (2,3,5,7,11,13,17,19,23,29);
  130     my ($a,$b,$c) = @_;
  131     my $s = "0" ;
  132     my $i = 0 ;
  133     # if a=0 then re-write as (0/1)*(sqrt(1)) = 0.
  134     if ($c*$a ==0){
  135   $a = 0;
  136   $b = 1;
  137   $c = 1;
  138     }
  139 
  140     # if b<0 then re-write the numerator as negative, denominator as positive
  141     if ($b < 0){
  142   $a = - $a ;
  143   $b = - $b ;
  144     }
  145 
  146     my $j = 1 ;
  147     while($j == 1){
  148   # can't reduce sqrt(1).
  149   if ($c == 1){
  150       last;
  151   }
  152   $j = 0;
  153   foreach $i (@p){
  154       # if you can factor a prime out of c, do it.
  155       # ie, sqrt(75) = 5*sqrt(3)
  156       if ( $c == $i*$i*int($c/($i*$i))){
  157     $a = $a *$i;
  158     $c = $c/($i*$i);
  159     $j=1;
  160       }
  161   }
  162     }
  163     $j = 1;
  164 
  165     # reduce fraction is lowest terms.
  166     while($j==1){
  167   # if the denominator is 1, then we're set.
  168   if ($b==1){
  169       last;
  170   }
  171   $j = 0;
  172   foreach $i (@p){
  173       # if you can factor a prime out of both numerator and denominator, do it.
  174       if ( abs($a) + $b == $i*int(abs($a) /$i) + $i*int($b/$i) ){
  175     $a = $a /$i;
  176     $b = $b /$i;
  177     $j=1;
  178       }
  179   }
  180     }
  181 
  182     # $s = answer string
  183 
  184     # if you have ($a/1)*sqrt(1) then the answer is simply "$a".
  185     if ($c == 1) {
  186   if ($b == 1){
  187       $s = "$a";
  188   }
  189   # if you have ($a/$b)*sqrt(1) then the answer is "$a/$b".
  190   else {
  191       $s = "$a/$b";
  192   }
  193     }
  194 
  195     if ($c > 1) {
  196   if ($a != 1){
  197       if ($b == 1) {
  198     # if denominator is 1, then answer is "$a*sqrt($c)".
  199     $s = "$a * sqrt($c)";
  200       }
  201       # if denominator is nonzero... answer is all three terms.
  202       else {
  203     $s = "$a *sqrt($c) /$b";
  204       }
  205   } else {
  206       # if you have "(1/1)*sqrt($c)" then the answer is "sqrt($c)".
  207       if ($b == 1) {
  208     $s = "sqrt($c)";
  209       }
  210       # if you have "(1/$b)*sqrt($c)" then answer is "sqrt($c)/$b".
  211       else {
  212     $s = "sqrt($c) /$b";
  213       }
  214   }
  215     }
  216 # return all four variables: answer string, reduced numerator, reduced denominator, squareroot with primes factored out
  217     my $rh_hash = {     displaystr  => $s,
  218                 num     => $a,
  219               denom     => $b,
  220                         root    => $c};
  221     $rh_hash;
  222 
  223 }
  224 
  225 #######
  226 sub frac {
  227     # use rad subroutine
  228     my ($a,$b) = @_;
  229     rad($a,$b,1);
  230 }
  231 ##########
  232 
  233 ####
  234 #sub exp simplifies exp($r*t) in form for writing perl
  235 #or tex.The input is exp($r,$ind); $ind indicates whether
  236 #we want perl or tex mode.$r is a string that represents
  237 #a number.
  238 #If $ind = 0  output is "exp(($r)*t)", simplified if possible.
  239 #If $ind = 1  output is "exp(($r) t)", simplified if possible.
  240 sub simpleexp {
  241     my $r = shift;
  242     my @rr = @_;
  243     my %options;
  244     if ($rr[0] eq 'mode') {
  245   $options{'mode'} = $rr[1];
  246     } elsif ( $rr[0] == 1) {
  247   $options{'mode'} = 'typeset';
  248     } elsif ($rr[0] == 0 ) {
  249   $options{'mode'} = 'standard';# means we use * for multiplication
  250   }
  251 
  252     my $y = "0";
  253     if ($r eq "0"){
  254   if ($options{'mode'} eq 'standard' ) {
  255       $y = "1";
  256   } elsif ($options{'mode'} eq 'typeset' ) {
  257       $y = "";  # multiplication by 1 is the identity
  258   } else {
  259       warn "simpleexp doesn't recognize the mode $options{'mode'}";
  260   }
  261 
  262   # change exp(1t) = exp(t)
  263     }elsif  ($r eq "1"){
  264   $y = "exp(t)";
  265     }
  266     # change exp(-1t) = exp(-t)
  267     elsif  ($r eq "-1"){
  268   $y = "exp(-t)";
  269     }
  270     # in typeset modeset you don't use the *
  271     # in standard modeset you use *
  272     else {
  273   if ($options{'mode'} eq 'typeset') {
  274       $y = "exp(($r)t)";
  275   } elsif ($options{'mode'} eq 'standard') {
  276       $y = "exp(($r)*t)";
  277   } else {
  278       warn "simpleexp doesn't recognize the mode $options{'mode'}";
  279   }
  280     }
  281     $y;
  282 }
  283 
  284 sub ivy {
  285     # $a*y'' + $b*y' + $c*y = 0.  y(0)=$m.  y'(0)=$n.
  286     my ($a, $b, $c, $m, $n) = @_;
  287     my $d = ($b*$b)-(4*$a*$c); # d is the discriminant
  288     my $c1 = "0";
  289     my $c2 = "0";
  290     my $r1 = "0";
  291     my $r2 = "0";
  292     my $answer = "";
  293 
  294     # c1 = first coefficient, c2 = second coefficient, rr1 = e^r1, rr2 = e^r2.
  295     # c1*rr1 + c2*rr2 = c1*e^r1 + c2*e^r2
  296 
  297     if ($d > 0) {
  298   # y(t) = [m/2 + sqrt(d)*(2An+Bm)/(2d)]e^[t*(-B+sqrt(d))/(2A)] + [m/2 - sqrt(d)*(2An+Bm)/(2d)]e^[t*(-B-sqrt(d))/(2A)]
  299   my $piece1 = frac($m,2);
  300   my $piece2 = rad(2*$a*$n+$b*$m,2*$d,$d);
  301   $c1 = "$piece1->{displaystr} + $piece2->{displaystr}"; # first coefficient:  "m/2 + sqrt(d)*(2An+Bm)/(2d)"
  302   $c2 = "$piece1->{displaystr} - $piece2->{displaystr}"; # second coefficient: "m/2 - sqrt(d)*(2An+Bm)/(2d)"
  303   $piece1 = frac(-$b,2*$a); # find (-B/(2A)) in lowest terms
  304   $piece2 = rad(1,2*$a,$d); # find (sqrt(B*B-4AC)/(2A)) in lowest terms
  305   $r1 = "$piece1->{displaystr} + $piece2->{displaystr}"; # r1: (-B+sqrt(d))/(2A)
  306   $r2 = "$piece1->{displaystr} - $piece2->{displaystr}"; # r2: (-B-sqrt(d))/(2A)
  307   my $rr1 = simpleexp($r1,0); # raise e^r1.
  308   my $rr2 = simpleexp($r2,0); # raise e^r2.
  309   $answer = "($c2) *$rr2 + ($c1) *$rr1";
  310 
  311     }
  312 
  313     if ($d == 0) {
  314   # y(t) = me^((-B/(2A)t) + [(n+mB)/(2A)]*t*e^((-Bt)/(2*A))
  315   my $piece1 = frac(-$b,2*$a); # find (-B/(2A)) in lowest terms
  316   my $piece2 = frac(2*$a*$n+$m*$b,2*$a); # find (2An+Bm)/(2A) in lowest terms
  317   $c1 = "$m";                 #  first coefficient: "m"
  318   $c2 = "$piece2->{displaystr}";         # second coefficient: "(n+mB)/(2A)"
  319   $r1 = "$piece1->{displaystr}";         # r1: (-B/(2A))
  320   $r2 = $r1;                  # r2: (-B/(2A))
  321   my $rr1 = simpleexp($r1,0); # rr1 = e^r1 = e^(-B/(2A))
  322   my $rr2 = simpleexp($r2,0); # rr2 = e^r2 = e^(-B/(2A))
  323   $answer = "($c1) *$rr1 + ($c2)*t *$rr2";
  324     }
  325 
  326     # if the descriminant is negative, then the roots are imaginary.
  327     # recall, e^x where x=a+ib then e^x = (e^a)*cos(bt) + (e^a)*sin(bt).
  328     if ($d<0){
  329   # y(t) = me^(-Bt/(2A))*cos(t*sqrt(4AC-B*B)/(2A))+(2An+Bm)*sqrt(4AC-B*B)/(4AC-B*B)*e^(-Bt/(2A))*sin(t*sqrt(4AC-B*B)/(2A))
  330   my $piece1 = rad (2*$a*$n+$b*$m,-$d,-$d); # find ((2An+Bm)*sqrt(4AC-B*B))/(4AC-B*B) in lowest terms
  331   my $piece2 = rad (1,2*$a,-$d);            # find (sqrt(4AC-B*B)/(2A)) in lowest terms
  332   $c1 = "$m";                      #  first coefficient: "m"
  333   $c2 = "$piece1->{displaystr}";              # second coefficient: "(2An+Bm)*sqrt(4AC-B*B)/(4AC-B*B)"
  334   my $cs1 = "cos(($piece2->{displaystr})*t)"; # cos(t*sqrt(4AC-B*B)/(2A))
  335   my $cs2 = "sin(($piece2->{displaystr})*t)"; # sin(t*sqrt(4AC-B*B)/(2A))
  336   my $piece3 = frac (-$b,2*$a);    # find (-B/(2A)) in lowest terms
  337   $r1 = "$piece3->{displaystr}";              # r1: (-B/(2A))
  338   $r2 = $r1;                       # r2: (-B/(2A))
  339   my $rr1 = simpleexp($r1,0);      # rr1 = e^r1 = e^(-B/(2A))
  340   my $rr2 = simpleexp($r2,0);      # rr2 = e^r2 = e^(-B/(2A))
  341   $answer = "($c1) *($rr1)*($cs1) + ($c2) *($rr2)*($cs2)";
  342     }
  343     $answer;
  344 }
  345 
  346 ############
  347 #sub ivy solves the initial value problem
  348 # $a*y'' + $b*y' + $c*y = 0, with  y(0) = $m,  y'(0) = $n
  349 
  350 #The numbers $a,$b,$c,$m,$n should be integers with $a not 0.
  351 #The inputs are given as arguments viz: ivy ($a,$b,$c,$m,$n).
  352 #The output is the solution as a string giving a function of t.
  353 
  354 
  355 
  356 sub undeterminedExp {
  357     my ($A,$B,$C,$r,$q0,$q1,$q2) = @_;
  358     my $P = "$A*x*x + $B *x + $C "; #characteristic poly.
  359     my $PP = "$A*2*x + $B ";#derivative of characteristic poly.
  360     my $exp = simpleexp($r,0);
  361     #$Pr = $P;
  362     #$Pr =~ s~~x~~$r~~g;
  363     #$Pr = PG_answer_eval("$Pr ");
  364     #$PPr = $PP;
  365     #$PPr =~ s~~x~~$r~~g;
  366     #$PPr = PG_answer_eval("$PPr ");
  367     my $Pr = $A *$r *$r + $B *$r + $C  ;
  368     my $PPr = 2* $A* $r + $B;
  369     #################
  370     if ($Pr  !=  0){
  371   #$r not a root of $P
  372   # $v0  = "($q0 /$Pr)  ";
  373   my $n1 = -$q1 * $PPr ;
  374   my $d1 = $Pr * $Pr;
  375   # $v1 = " ($q1 /$Pr)* t   +  ($n1 / $d1) ";
  376   my $n22 = $Pr *$Pr  *$q2;
  377   my $n21 =  (- 2 *$PPr ) *$q2;
  378   my $n20 =  (2*$PPr *$PPr - ($A*2*$Pr)) *$q2 ;
  379   my $d2 = $Pr **3;
  380   # $v2 = "($n22/$d2) *t*t  + ($n21/$d1) *t + ($n20/$d2) ";
  381   my $c00n = $q0*$d1 -$q1 *$PPr *$Pr + $n20;
  382   my $c00d = $d2;
  383   my $fraca = frac ($c00n ,$c00d );
  384   my $c00 = $fraca->{displaystr};
  385   my $c01n = $Pr *$q1 - 2 *$PPr *$q2;
  386   my $c01d = $d1;
  387   my $fracb = frac($c01n ,$c01d );
  388   my $c01 = $fracb->{displaystr};
  389   $c01 = "($c01) *t";
  390   my $c02n = $q2;
  391   my $c02d = $Pr;
  392   my $fracc = frac($c02n ,$c02d );
  393   my $c02 = $fracc->{displaystr};
  394   $c02 = "($c02 ) *t*t";
  395   my @adda = add ($c00,$c00n,$c01,$c01n,$c02,$c02n);
  396   my $outa = $adda [0];
  397         my $y = "( $outa ) * $exp " ;
  398   #############
  399     } elsif ($PPr != 0){
  400   #$r a simple root of $P
  401   my $q2m = -$q2;
  402   #$v0 = "( $q0/$PPr)*t ";
  403   my $q2d = 2*$q2;
  404   my $d10 = 2*$PPr;
  405   my $d11 = $PPr **2 ;
  406   my $q1m = -$q1;
  407   #$v1 = "($q1 / $d10)*t*t   +   ($q1m / $d11)*t ";
  408   my $d23 = 3*$PPr;
  409   my $d22 = $PPr *$PPr ;
  410   my $d21 = $PPr *$d22;
  411   #$v2 = "($q2 / $d23) *t*t*t + ($q2m/$d22) *t*t  + ($q2d/ #$d21) *t ";
  412   ######
  413   my $c10n = $q0 *$d22 -$q1*$A *$PPr + $A*$A*$q2d;
  414   my $c10d = $d21;
  415   my $fracd = frac($c10n ,$c10d );
  416   my $c10 = $fracd->{displaystr};
  417   #warn " c10 $c10";
  418   $c10 = "($c10 )*t";
  419   my $c11n = $PPr * $q1 - 2 *$A * $q2;
  420   my $c11d = 2*$PPr*$PPr;
  421   my $frace = frac($c11n ,$c11d );
  422   my $c11 = $frace->{displaystr};
  423   #warn " c11 $c11";
  424   $c11 = "($c11 )*t*t";
  425   my $c12n = $q2;
  426   my $c12d = 3*$PPr;
  427   my $fracf = frac ($c12n ,$c12d );
  428   my $c12 = $fracf->{displaystr};
  429   $c12 = "($c12 ) *t*t*t";
  430   my @addb = add($c10,$c10n,$c11,$c11n,$c12,$c12n);
  431   my $outb = $addb[0];
  432   my $y = "( $outb ) * $exp" ;
  433   ######
  434     } else {
  435         # $v2 =  "($q2 /12*$A) *t*t*t*t  ";
  436         #v1 =  "($q1 /6*$A) *t*t*t   ";
  437         #$v0 =  "($q0 /2*$A) *t*t  " ;
  438   my $c20n = $q0;
  439   my $c20d = 2*$A;
  440   my $fracg = frac($q0 ,$c20d );
  441   my $c20 = $fracg->{displaystr};
  442   $c20 = "($c20 ) *t*t";
  443   my $c21n = $q1;
  444   my $c21d = 6*$A;
  445   my $frach = frac($c21n ,$c21d );
  446   my $c21 = $frach->{displaystr};
  447   $c21 = "($c21)  *t*t*t";
  448   my $c22n = $q2;
  449   my $c22d = 12*$A;
  450   my $fraci = frac($c22n ,$c22d );
  451   my $c22 = $fraci->{displaystr};
  452   $c22 = "($c22 ) *t*t*t*t";
  453   my @addc = add($c20,$c20n,$c21,$c21n,$c22,$c22n);
  454   my $outc = $addc[0];
  455   my $y = "( $outc ) * $exp" ;
  456     }
  457 
  458 }
  459 #################
  460 
  461 # undeterminedSin is a subroutine to solve
  462 #undetermined coefficient problems that have
  463 #sines and cosines.
  464 #The input is an array ($A,$B,$C,$r,$w,$q1,$q0,$r1,$r0)
  465 #given as arguments on undeterminedSin
  466 # $L =$A y'' + $B y' + $C y
  467 # $rhs = ($q1 t + $q0) cos($w t)exp($r t) +
  468 #        ($r1 t + $r0) sin($w t)exp($r t)
  469 #The subroutine uses undetermined coefficients
  470 #to find a solution $y of $L = $rhs .
  471 #The output \is $y
  472 
  473 sub undeterminedSin {
  474     my ($A,$B,$C,$r,$w,$q1,$q0,$r1,$r0) = @_;
  475     my $Pr = ($A*$r*$r) + $B *$r + $C;
  476     my $PPr = (2*$A *$r ) + $B ;
  477     my $re = $Pr -$A* $w*$w ;
  478     my $im = $PPr * $w ;
  479     #If P(x) = A x^2 +Bx +C,
  480     #P($r+i*$w)=$re+i $im.
  481     my $D = $re **2 + $im **2 ;
  482     # $D = |P($r + i*$w)|^2
  483     my $reprime = $PPr;
  484     my $imprime = 2*$A*$w;
  485     #If PP(x) = derivative of P(x),
  486     #PP($r+i $w)=$reprime+$imprime.
  487     my $cos = "cos($w *t)";
  488     my $sin  =  "sin($w *t)";
  489     if ($w == 1){
  490   $cos = "cos(t)";
  491   $sin = "sin(t)";
  492     }
  493 
  494 
  495     my $exp = simpleexp($r,0);
  496 
  497     ############
  498     if ($D != 0){
  499   #We first handle case that$r+i$w not a root of $P(x)
  500   #This solution is based on:
  501   #Let L[y] = Ay'' +By'+C,z=r+iw,p=P(z),q=P'(z),S,T complex;p!=0.
  502   #Then L[((St+T-(Sq/p))/p)*e^{zt}]=(St+T)e^{zt}.
  503   #Take S=q1-i*r1;T=q0-i*r0.Then take real part.
  504   my ($renum1 ,$imnum1) = complexmult($q1,-$r1,$re, -$im);
  505 
  506   #S*(p conjugate)=   $renum1+i imnum1
  507   my ($renum2 ,$imnum2) = complexmult ($renum1,$imnum1,$reprime,$imprime);
  508   #The above are real and imag parts  of q*S*(p conjugate)
  509   my $first = ($D *$q0 ) - $renum2 ;
  510   my $second = (-$r0 *$D ) -$imnum2 ;
  511   my ($renum3, $imnum3 )  = complexmult( $first,$second,$re,-$im);
  512   #these are re and im of (DT-qS(p conj))*(p conj.)
  513   my $n1 = $renum1;
  514   my $n2 = $renum3;
  515   my $n3 = -$imnum1;
  516   my $n4 = -$imnum3;
  517   my $fraca  = frac($n1,$D );
  518   my $tcosp = $fraca->{displaystr};
  519   #$tcospart = "($tcosp )*t*$exp *$cos ";
  520   my $tcospart = "($tcosp)*t*$exp *$cos " ; #####################
  521   my $DD = $D *$D;
  522   my $fracb = frac($n2 , $DD );
  523   my $cospart = $fracb->{displaystr};
  524   $cospart = "($cospart )*$exp*$cos";
  525   my $fracc = frac($n3 , $D );
  526   my $tsinpart = $fracc->{displaystr};
  527   $tsinpart = "($tsinpart )*t*$exp*$sin";
  528   my $fracd = frac($n4 , $DD );
  529   my $sinpart = $fracd->{displaystr};
  530   $sinpart = "($sinpart )*$exp*$sin";
  531   my @suma = add($tcospart,$n1,$cospart,$n2,$tsinpart,$n3,$sinpart,$n4 );
  532   my $out = $suma[0];
  533   #The solution is $out
  534     } else{
  535   #We now handle case that $r+iw is a  root of $P
  536   #In this case $PPr = 0 and $PP($r + i$w) = 2*$A*i*$w
  537   #Solution based on
  538   #L[((S/2q)t*t -(AS/q*q)t +(T/q)t)e^{zt}]=
  539   #(St+T)e^{zt}.Notation as for 1st part.
  540   my $n3 = $q1 - (2*$w *$r0);
  541   my $n4 = $r1 + (2*$w *$q0 );
  542   my $n1 = -$r1 ;
  543   my $n2 = $q1 ;
  544   my $T2 = 4*$A *$w ;
  545   my $T1 = $w * $T2 ;
  546   my $frace = frac($n1 , $T2 );
  547   my $t2cospart = $frace->{displaystr};
  548   $t2cospart = "($t2cospart )*t*t*$exp *$cos ";
  549   my $fracf  = frac($n3 , $T1 );
  550   my $tcospart = $fracf->{displaystr};
  551   $tcospart = "($tcospart )*t*$exp *$cos ";
  552   my $fracg = frac($n2 , $T2 );
  553   my $t2sinpart = $fracg->{displaystr};
  554   $t2sinpart = "($t2sinpart )*t*t*$exp*$sin";
  555   my $frach = frac($n4 , $T1 );
  556   my $tsinpart = $frach->{displaystr};
  557   $tsinpart = "($tsinpart )*t*$exp*$sin";
  558   my @addb = add ($t2cospart,$n1,$tcospart,$n3,$t2sinpart,$n2,$tsinpart,$n4 );
  559   my $out = $addb[0];
  560     }
  561 
  562 }
  563 
  564 sub check_eigenvector {
  565 
  566     my $eigenvalue = shift;
  567     my $matrix = shift;
  568     my %options = @_;
  569     assign_option_aliases( \%options,   );
  570 
  571     set_default_options(  \%options,
  572         'debug'       =>  0,
  573             'correct_ans'     =>  undef
  574     );
  575 
  576 
  577     my @correct_vector = ();
  578     @correct_vector = @{$options{'correct_ans'}} if defined ($options{'correct_ans'});
  579 
  580     my $ans_eval = new AnswerEvaluator;
  581 
  582     $ans_eval->{debug} = $options{debug};
  583     my $corr_ans_points = "( " . join(", ", @correct_vector). " )" ;
  584     $ans_eval->ans_hash( correct_ans  => $corr_ans_points );
  585     $ans_eval->install_pre_filter(\&is_array);
  586     $ans_eval->install_pre_filter(\&std_num_array_filter);
  587 
  588     $ans_eval->install_evaluator(sub { my $rh_ans = shift;
  589                my %options  = @_;
  590                my @vector = @{$rh_ans->input()};
  591                return($rh_ans) unless @correct_vector == @vector;
  592             # make sure the vectors are the same dimension
  593 
  594                my $vec = new Matrix(2,1);
  595                $vec->assign(1,1, $vector[0]);
  596                $vec->assign(2,1, $vector[1]);
  597                my $out_vec = $matrix * $vec;
  598                my @diff;
  599                $diff[0] = $out_vec->element(1,1) - $vec->element(1,1)*$eigenvalue;
  600                $diff[1] = $out_vec->element(2,1) - $vec->element(2,1)*$eigenvalue;
  601                $rh_ans->{score} = zero_check(\@diff);
  602                $rh_ans;
  603 
  604            });
  605     $ans_eval->install_post_filter( sub {     my $rh_ans= shift;
  606               if ($rh_ans->error_flag('SYNTAX') ) {
  607                   $rh_ans->{ans_message} = $rh_ans->{error_message};
  608                   $rh_ans->clear_error('SYNTAX');
  609                   $rh_ans;
  610               }
  611                 });
  612 
  613     $ans_eval;
  614 }
  615 
  616 
  617 
  618 =pod
  619 
  620     rungeKutta4a
  621 
  622 
  623 =cut
  624 
  625 
  626 
  627 sub rungeKutta4a {
  628   my $rh_ans = shift;
  629   my %options = @_;
  630   my $rf_fun = $rh_ans->{rf_diffeq};
  631   set_default_options(  \%options,
  632           'initial_t'         =>  1,
  633           'initial_y'         =>  1,
  634           'dt'            =>  .01,
  635           'num_of_points'       =>  10,     #number of reported points
  636           'interior_points'     =>  5,      # number of 'interior' steps between reported points
  637           'debug'           =>  1,      # remind programmers to always pass the debug parameter
  638           );
  639   my $t = $options{initial_t};
  640   my $y = $options{initial_y};
  641 
  642   my $num = $options{'num_of_points'};  # number of points
  643   my $num2 = $options{'interior_points'};  # number of steps between points.
  644   my $dt   = $options{'dt'};
  645   my $errors = undef;
  646   my $rf_rhs = sub {  my @in = @_;
  647         my ( $out, $err) = &$rf_fun(@in);
  648         $errors .= " $err at ( ".join(" , ", @in) . " )<br>\n" if defined($err);
  649         $out = 'NaN' if defined($err) and not is_a_number($out);
  650         $out;
  651           };
  652 
  653   my @output = ([$t, $y]);
  654   my ($i, $j, $K1,$K2,$K3,$K4);
  655 
  656   for ($j=0; $j<$num; $j++) {
  657       for ($i=0; $i<$num2; $i++) {
  658     $K1 = $dt*&$rf_rhs($t, $y);
  659     $K2 = $dt*&$rf_rhs($t+$dt/2,$y+$K1/2);
  660     $K3 = $dt*&$rf_rhs($t+$dt/2, $y+$K2/2);
  661     $K4 = $dt*&$rf_rhs($t+$dt, $y+$K3);
  662     $y = $y + ($K1 + 2*$K2 + 2*$K3 + $K4)/6;
  663     $t = $t + $dt;
  664       }
  665       push(@output, [$t, $y]);
  666   }
  667   $rh_ans->{evaluation_points} = \@output;
  668   $rh_ans->throw_error($errors) if defined($errors);
  669   $rh_ans;
  670 }
  671 
  672 
  673 sub level_curve_check {
  674     my $diffEqRHS = shift;    #required differential equation
  675     my $correctEqn = shift;   # required answer in order to check the equation
  676     my %options = @_;
  677   my $saveUseOldAnswerMacros = main::PG_restricted_eval('$main::useOldAnswerMacros') || 0;
  678   main::PG_restricted_eval('$main::useOldAnswerMacros = 1');
  679     assign_option_aliases( \%options,
  680         'vars'      =>    'var',
  681         'numPoints'   =>    'num_of_points',
  682         'reltol'    =>    'relTol',
  683   );
  684     set_default_options(  \%options,
  685       'initial_t'   =>    0,
  686       'initial_y'   =>    1,
  687       'var'     =>    [qw( x y )],
  688       'num_of_points'         =>    10,
  689       'tolType'   =>    (defined($options{tol}) ) ? 'absolute' : 'relative',
  690       'relTol'    =>    .01,
  691       'tol'     =>    .01,
  692       'debug'     =>    0,
  693   );
  694 
  695     my $initial_t = $options{initial_t};
  696     my $initial_y = $options{initial_y};
  697     my $var = $options{var};
  698     my $numPoints = $options{num_of_points};
  699     my @VARS = get_var_array( $var );
  700     my ($tolType, $tol);
  701     if ($options{tolType} eq 'absolute') {
  702     $tolType = 'absolute';
  703     $tol = $options{'tol'};
  704     delete($options{'relTol'}) if exists( $options{'relTol'} );
  705     } else {
  706     $tolType = 'relative';
  707     $tol = $options{'relTol'};
  708     delete($options{'tol'}) if exists( $options{'tol'} );
  709     }
  710     #prepare the correct answer and check its syntax
  711     my $rh_correct_ans = new AnswerHash;
  712     $rh_correct_ans ->{correct_ans} = $correctEqn;
  713     # check and calculate the function defining the differential equation
  714     $rh_correct_ans->input( $diffEqRHS );
  715     $rh_correct_ans = check_syntax($rh_correct_ans);
  716     warn  $rh_correct_ans->{error_message},$rh_correct_ans->pretty_print() if $rh_correct_ans->{error_flag};
  717 
  718     $rh_correct_ans->{error_flag} = undef;
  719 
  720     $rh_correct_ans = function_from_string2($rh_correct_ans,
  721               ra_vars => [@VARS],
  722               store_in =>'rf_diffeq',
  723               debug=>$options{debug}
  724               );
  725     warn "Error in compiling instructor's answer: $diffEqRHS<br> $rh_correct_ans->{error_message}<br>\n$rh_correct_ans->pretty_print()"
  726   if $rh_correct_ans->{error_flag};
  727 
  728 
  729     # create the test points that should lie on a solution curve of the differential equation
  730     $rh_correct_ans = rungeKutta4a( $rh_correct_ans,
  731            initial_t => $initial_t,
  732            initial_y => $initial_y,
  733            num_of_points => $numPoints,
  734            debug=>$options{debug}
  735            );
  736     warn "Errors in calculating the solution curve $rh_correct_ans->{student_ans}<BR>\n
  737           $rh_correct_ans->{error_message}<br>\n",$rh_correct_ans->pretty_print() if $rh_correct_ans->catch_error();
  738     $rh_correct_ans->clear_error();
  739 
  740     # check and compile the correct answer submitted by the instructor.
  741     my ($check_eval) = fun_cmp('c',   vars  =>  [@VARS],
  742              params   =>  ['c'],
  743              tolType  =>  $options{tolType},
  744              relTol =>  $options{relTol},
  745              tol  =>  $options{tol},
  746              debug  =>  $options{debug},
  747              );  # an evaluator that tests for constants;
  748     $check_eval->ans_hash(evaluation_points => $rh_correct_ans->{evaluation_points});
  749     $check_eval->evaluate($rh_correct_ans->{correct_ans});
  750     if( $check_eval->ans_hash->{score} == 0 or (defined($options{debug}) and $options{debug})) {
  751   # write error message for professor
  752   my $out1 = $check_eval->ans_hash->{evaluation_points};
  753   my $rf_corrEq = $check_eval->ans_hash->{rf_student_ans};
  754   my $error_string = "This equation $correctEqn is not constant on solution curves of  y'(t) = $diffEqRHS\r\n<br>
  755                             starting at ( $initial_t , $initial_y )<br>
  756                             $check_eval->ans_hash->pretty_print()".
  757           "options<br>\n".pretty_print({  vars  =>  [@VARS],
  758                   params  =>  ['c'],
  759                   tolType =>  $options{tolType},
  760                   relTol  =>  $options{relTol},
  761                   tol   =>  $options{tol},
  762                   debug =>  $options{debug},
  763                     });
  764 
  765   for (my $i=0; $i<$numPoints;$i++) {
  766       my ($z, $err) = &$rf_corrEq( $out1->[$i][0], $out1->[$i][1] );
  767       $z = $err if defined $err;
  768       $error_string .= "F( ". $out1->[$i][0] . " , ". $out1->[$i][1] . " ) = $z <br>\r\n";
  769   }
  770   $error_string .= $rh_correct_ans->error_message();
  771   warn $error_string, $check_eval->ans_hash->pretty_print;
  772     }
  773 
  774     my ($constant_eval) = fun_cmp('c', vars => [@VARS],
  775           params  =>  ['c'],
  776           tolType =>  $options{tolType},
  777           relTol  =>  $options{relTol},
  778           tol   =>  $options{tol},
  779           debug =>  $options{debug},
  780           );  # an evaluator that tests for constants;
  781     $constant_eval->ans_hash(evaluation_points => $rh_correct_ans->{evaluation_points});
  782     my $answer_evaluator = new AnswerEvaluator;
  783     $answer_evaluator->ans_hash(  correct_ans     =>  $rh_correct_ans->{correct_ans},       # used for answer only
  784         rf_correct_ans    =>  sub { my @input = @_; pop(@input); },
  785         # return the last input which is the constant parameter 'c';
  786         evaluation_points =>  $rh_correct_ans->{evaluation_points},
  787         ra_param_vars     =>  ['c'],                                # compare with constant function
  788         ra_vars       =>  [@VARS],
  789         type        =>  'level_curve',
  790         );
  791     $answer_evaluator->install_evaluator(sub { my $ans_hash = shift;
  792                  my %options = @_;
  793                  $constant_eval->evaluate($ans_hash->{student_ans});
  794                  $constant_eval->ans_hash;
  795              });
  796 
  797     $answer_evaluator->install_post_filter( sub { my $ans_hash = shift; $ans_hash->{correct_ans} = $correctEqn; $ans_hash; } );
  798     $answer_evaluator->install_post_filter( sub {     my $rh_ans= shift;
  799                 my %options = @_;
  800                 if ($rh_ans->catch_error('SYNTAX') ) {
  801                     $rh_ans->{ans_message} = $rh_ans->{error_message};
  802                     $rh_ans->clear_error('SYNTAX');
  803 
  804                 }
  805                 $rh_ans;
  806                   });
  807 
  808   main::PG_restricted_eval('$main::useOldAnswerMacros = '.$saveUseOldAnswerMacros);
  809     $answer_evaluator;
  810 
  811 }
  812 
  813 
  814 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9