PolarGraph1

From WeBWorK_wiki
Revision as of 05:19, 18 July 2023 by Pstaabp (talk | contribs) (add historical tag and give links to newer problems.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
This article has been retained as a historical document. It is not up-to-date and the formatting may be lacking. Use the information herein with caution.

This problem has been replaced with a newer version of this problem

Graphing a Parametric or Polar Curve

Click to enlarge

This PG code shows how to graph a parametric curve or polar curve with a shading (a filled region).


Templates by Subject Area

PG problem file Explanation

Problem tagging data

Problem tagging:

DOCUMENT();      

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGgraphmacros.pl",
"AnswerFormatHelp.pl",
"unionTables.pl",
);

TEXT(beginproblem());

$refreshCachedImages = 1;

Initialization: We use PGgraphmacros.pl to generate the graph, and unionTables.pl to put the text and the graph side-by-side. We should set $refreshCachedImages = 1; so that changes in the graph will show up (not get stuck by old images in the browser cache).

Context("Numeric")->variables->are(t=>"Real");

$gr = init_graph(-1.1,-1.1,1.1,1.1,axes=>[0,0],size=>[300,300]);

#
#  Define some useful colors
#
$gr->new_color("lightblue", 198,217,253); # RGB
$gr->new_color("darkblue",   77,137,249);
$gr->new_color("lightred",  255,127,127);
$gr->new_color("darkred",   255, 55, 55);
$gr->new_color("lightorange",  255,204,127);
$gr->new_color("darkorange",   255, 153, 0);
$gr->new_color("lightgreen", 187, 255, 153); 
$gr->new_color("darkgreen",    0, 208, 0);

#
#  For a polar curve r = f(t),
#  x = r cos(t) = f(t) cos(t)
#  y = r sin(t) = f(t) sin(t)
#
$x = Formula("cos(5*t) * cos(t)");
$y = Formula("cos(5*t) * sin(t)");


$f = new Fun( $x->perlFunction, $y->perlFunction, $gr );
$f->domain(0,3.14);
$f->steps(90);
$f->weight(2);
$f->color('darkgreen');

$gr->fillRegion([0.5,0.1,'lightgreen']);

Setup: We initialize a graph object named $gr. We define several new named colors which you can use if you want. We construct MathObjects formulas $x and $y for the x- and y-coordinates in terms of the parameter t. Then, we pass these formulas to the Fun routine, converting them to perl subroutines via ->perlFunction, and attach them to the graph object $gr. Then, we set some of the options for the graph of the parametric curve $f. Finally, we fill the region enclosing the point (0.5,0.1) with the color light green.

Context()->texStrings;
BEGIN_TEXT
\{
ColumnTable(
"Find the area enclosed by one petal of the 
rose curve \( r = f(\theta) = \cos(5\theta) \).
$BR
$BR
Area = ".
ans_rule(20).$SPACE.
AnswerFormatHelp("numbers")
,
$BCENTER.
image( insertGraph($gr), width=>300, height=>300 ).
$PAR.
"Graph of \( r = \cos(5\theta) \)".
$ECENTER
,
indent => 0, separation => 30, valign => "TOP"
); 
\}
END_TEXT
Context()->normalStrings;

Main Text: We use the ColumnTable(column 1, column 2, options) to put the text and graph side-by-side. We join (Perl) strings " " to common PG commands like ans_rule(20) using the string concatenation operator . which is a period. Notice that the commas between column 1, column 2, and the options do not have any periods before them.

$showPartialCorrectAnswers = 1;

# intentionally incorrect
ANS( Compute("pi")->cmp() );

Answer Evaluation:

Context()->texStrings;
BEGIN_SOLUTION
Solution explanation goes here.
END_SOLUTION
Context()->normalStrings;

COMMENT('MathObject version.');

ENDDOCUMENT();

Solution:

Templates by Subject Area