################################################################ # # A more complex example showing how to use the Parser to create # functions you can call from perl, to substitute values into a # formula, and to convert a formula to a form that can be used in # graphics generated on the fly. # DOCUMENT(); # This should be the first executable line in the problem. loadMacros( "PGbasicmacros.pl", "PGanswermacros.pl", "PGgraphmacros.pl", "PGauxiliaryFunctions.pl", "Parser.pl", "parserUtils.pl", ); TEXT(&beginproblem); ############################################## # # The setup # Context('Vector'); Context()->variables->add(a => 'Real', b => 'Real'); $c = non_zero_random(-1,1,1); $a = $c*random(2,5,1)/2; $b = -$c*random(2,5,1)/2; # # The function to plot # $f = Formula("a x^2 y + b x y^2"); $f->substitute(a=>$a,b=>$b)->perlFunction('f'); # # Traces to show # $x1 = non_zero_random(-2,2,1); $x1 /= 2 if (abs($b) >= 2 && abs($x1) == 2); $x2 = non_zero_random(-2,2,1); $x2 /= 2 if (abs($a) >= 2 && abs($x2) == 2); $x = max(.5,min(3,round(-2*$b*$x2/$a)/2)); $y = max(.5,min(3,round(-2*$a*$x1/$b)/2)); # # Points to show # $xv = round(-$b*$y/$a/2); $xv = 1 if ($xv == 0); $fxv = f($xv,$y); if (abs($fxv) < .75) {$xv = -$xv; $fxv = f($xv,$y)} $yv = round(-$a*$x/$b/2); $yv = -1 if ($yv == 0); $fyv = f($x,$yv); if (abs($fyv) < .75) {$yv = -$yv; $fyv = f($x,$yv)} $M = int(max(abs($fxv),abs($fyv),4))+1; # # Graph size # ($xm,$xM) = (-3,3); ($ym,$yM) = (-3,3); ($zm,$zM) = (-$M,$M); $size = [200,250]; $tex_size = 350; ############################################## # # The plot defaults # @Goptions = ( $ym,$zm,$yM,$zM, # dimensions of graph axes => [0,0], grid => [$yM-$ym,$zM-$zm], # number of grid lines size => $size # pixel dimension ); @imageoptions = (size=>$size, tex_size=>$tex_size); $plotoptions = "using color:red and weight:2"; # # Make the traces # $fx = $f->substitute(x => x, a => $a, b => $b, y => 'x')->reduce; $Gx = init_graph(@Goptions); plot_functions($Gx, "$fx for x in <$ym,$yv] $plotoptions", "$fx for x in <$yv,$yM> $plotoptions", ); $Xtrace = Image($Gx,@imageoptions); $fy = $f->substitute(y => $y, a => $a, b => $b)->reduce; $Gy = init_graph(@Goptions); plot_functions($Gy, "$fy for x in <$xm,$xv] $plotoptions", "$fy for x in <$xv,$xM> $plotoptions", ); $Ytrace = Image($Gy,@imageoptions); Context()->texStrings; # # Make the table of images # @rowopts = (indent=>0, separation=>30); $Images = BeginTable(). AlignedRow([$Xtrace,$Ytrace], @rowopts). AlignedRow(["Trace for \(x=$x\) has","Trace for \(y=$y\) has"], @rowopts). AlignedRow(["a point at \(($yv,$fyv)\).","a point at \(($xv,$fxv)\)."], @rowopts). EndTable(); ############################################## BEGIN_TEXT The graphs below are traces for a function \(f(x,y)\) at \(x=$x\) and \(y=$y\). $PAR $Images $PAR If \(f(x,y) = \{$f->TeX\}\) then \(a\) = \{ans_rule(6)\} and \(b\) = \{ans_rule(6)\}. END_TEXT Context()->normalStrings; ################################################## ANS(std_num_cmp($a)); ANS(std_num_cmp($b)); ################################################## ENDDOCUMENT(); # This should be the last executable line in the problem.