Dynamic Graph

Dynamically generated graph of a function

Complete Code

Download file: DynamicGraph.pg

POD for Macro Files

PG problem file

Explanation

DOCUMENT();

loadMacros('PGstandard.pl', 'PGML.pl', 'PGtikz.pl', 'PGcourse.pl');

Preamble

The dynamic graph is generated with PGtikz.pl, so this is needed.

$a = random(1, 4);    # negative of left x-intercept
$b = random(2, 4);    # right x-intercept
$c = random(2, 6);    # y-intercept

$k = -$c / ($a * $b);

$graph = createTikZImage();
$graph->tikzLibraries('arrows.meta');
$graph->BEGIN_TIKZ
\tikzset{>={Stealth[scale=1.5]}}
\filldraw[
    draw=LightBlue,
    fill=white,
    rounded corners=10pt,
    thick,use as bounding box
] (-6,7) rectangle (6,-1);
\draw[->,thick] (-6,0) -- (6,0) node[above left,outer sep=3pt] {\(x\)};
\foreach \x in {-5,...,-1,1,2,...,5}
    \draw(\x,5pt) -- (\x,-5pt) node [below] {\(\x\)};
\draw[->,thick] (0,-1) -- (0,7) node[below right,outer sep=3pt] {\(y\)};
\foreach \y in {1,...,6}
    \draw (5pt,\y) -- (-5pt,\y) node[left] {\(\y\)};
\draw[blue,ultra thick] plot[domain=-6:6,smooth] (\x,{$k*(\x+$a)*(\x-$b)});
END_TIKZ

Setup

The code between $graph->BEGIN_TIKZ and END_TIKZ are tikz commands. Information on tikz can be found at the homepage for tikz and details on using tikz within pg problems can be found in PGtikz.pl.

This problem creates a parabola with random intercepts.

Some notes about the command in the TIKZ block:

  • The first \filldraw command produces a frame around the plotting region
  • The first two \draw commands draw the axes as well as the axis labels. The -> gives the lines arrows in that direction and the thick makes the lines a bit thicker.
  • The two \foreach commands produce the tick marks and labels.
  • The last \draw command produces the graph of the function. The domain option gives the plotting domain and the smooth attempts to make the resulting graph smooth. Lastly, the function itself needs to be in {} in order for the function to be computed correctly.
BEGIN_PGML
Use the graph to find the missing values. There may be more than one correct
answer, in which case you should enter your answers as a comma separated list.
If there are no correct answers, enter NONE.

[@ image($graph, width => 400, tex_size => 600) @]*

a) [`f(0) =`] [__]{$c}

b) [`f\Big(`] [__]{List(-$a, $b)} [`\Big) = 0`]
END_PGML

Statement

Note that the tikz graph in $graph to be shown is placed in the image function and since this is a function, it must go in a [@ ... @]* block.

BEGIN_PGML_SOLUTION
Solution explanation goes here.
END_PGML_SOLUTION

ENDDOCUMENT();

Solution

A solution should be provided here.