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

$altText =
    "Graph of a downward opening parabola. "
    . "The graph is at a height of $c when x is 0, "
    . "and is at a height of 0 when x is -$a or $c.";

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 commands in the BEGIN_TIKZ/END_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.

[![$altText]!]{$graph}{400}

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

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

Statement

Note that the TikZ graph in $graph is inserted into the problem text using the PGML image syntax which is [!alt text!]{image object}. For accessibility purposes, all images inserted into a problem should be provided with alternate text that provides screen reader users the information they would need to work the problem.

BEGIN_PGML_SOLUTION
Solution explanation goes here.
END_PGML_SOLUTION

ENDDOCUMENT();

Solution

A solution should be provided here.