Features & Development

Graphing functions like abs(x) with MathObjects

Graphing functions like abs(x) with MathObjects

by Robin Cruz -
Number of replies: 4

I cannot  get the absolute value function to graph.  I get a warning message:

Warning messages

  • syntax error in answer:(|x|) 
    Illegal character at position 2 at line 454 of [PG]/macros/PGgraphmacros.pl
  • syntax error in answer:(|x|) 
    Illegal character at position 2 at line 458 of [PG]/macros/PGgraphmacros.pl

Here is the code:-------------------------------------------------------------------

DOCUMENT(); # This should be the first executable line in the problem.

loadMacros(
  "PGstandard.pl",
  "MathObjects.pl",
  "contextInequalities.pl",
  "parserUtils.pl"
);

TEXT(beginproblem);

######################################
#  Setup

#----Define the functions
$f = Formula("abs(x)");

$a = random(-4,-2,1);
$g = Formula("$a");

#---Define the graph
($xm,$xM) = (-6,6);  #min and max for x
($ym,$yM) = (-6,6);  #min and max for y
$size = [300,300];
$tex_size = 600;

@slice = NchooseK(3,3);
@colors = ("blue", "red", "green");
@sc = @colors[@slice]; #scrambled colors

@Goptions = (
  $xm,$ym,$xM,$yM,
  axes => [0,0],
  grid => [$xM-$xm,$yM-$ym],
  size => $size
);
@imageoptions = (size=>$size,tex_size=>$tex_size);

$xdomain = "x in <$xm,$xM>";
$f_options = "using color:$sc[0] and weight:3";
$g_options = "using color:$sc[1] and weight:3";

$graph = init_graph_no_labels(@Goptions);
plot_functions($graph,
               "$f for $xdomain $f_options",
               "$g for $xdomain $g_options");
$labelf = new Label(-3,5, "f(x) = $f->string", 'black','center'); 
$labelg = new Label(3,$a, "y = $a",'black','center'); 
$graph->lb($labelf,$labelg);

$Image = Image($graph,@imageoptions);

######################################
#  Main text

BEGIN_TEXT
$Image
$BR $BR
The graph of \( f(x) = \{$f->TeX\} \) is given above with the line \( y = $a \).
For what values of \(x\) is the following true?
$PAR
\( |x| = $a  \)

$BR
Answer: \{ans_rule(20) \}

END_TEXT

######################################
#  Answer

Context("Inequalities");

$ans = Compute("NONE");
ANS($ans->cmp);
$showPartialCorrectAnswers = 1;

######################################

ENDDOCUMENT();
 -----------------------END CODE----------------------------------------------

Thanks for your help -- rac

In reply to Robin Cruz

Re: Graphing functions like abs(x) with MathObjects

by Davide Cervone -
This is the same issue you had previously when trying to use MathObject with the graph macros. The graph macros use the old AlgParser rather than MathObjects, and not all the syntax that is available with MathObject works with that older parser. In particular, the absolute values.

You need to use lower-level calls to the graph objects to use MathObjects with them. See

 
   http://wwrk.maa.org/moodle/mod/forum/discuss.php?d=1955&parent=3805
for an example of how this is done.

I suspect that the following would work:

   $f_graph = new Fun($f->perlFunction,$graph);
   $f_graph->color($sc[0]); $f_graph->weight(3);

   $g_graph = new Fun($g->perlFunction,$graph);
   $g_graph->color($sc[1]); $g_graph->weight(3);
in place of
    plot_functions($graph,
               "$f for $xdomain $f_options",
               "$g for $xdomain $g_options");
and you could eliminate
    $xdomain = "x in ";
    $f_options = "using color:$sc[0] and weight:3";
    $g_options = "using color:$sc[1] and weight:3";
Hope that helps.

Davide

PS, the graph macros really need to be updated to use MathObjects. One of the projects from the AIM conference was to work on a more modern version of the graph macros, but that will be some time in coming, I expect. In the short term, it would probably not be too hard to update the current macros to use MathObjects rather than the old parser. When I have time, I'll look into it.

In reply to Davide Cervone

Re: Graphing functions like abs(x) with MathObjects

by Michael Gage -
I've made a small change to PGgraphmacros.pl which should help alleviate this problem for now. As Davide says, full blown integration of GraphObjects into the MathObjects scheme will take a bit longer (and may not be fully backward compatible).

Make this change in the file PGgraphmacros.pl inside the
subroutine plot_functions:

 my $subRef = Formula($rule)->perlFunction(undef,[$var]);
 # my $subRef = string_to_sub($rule,$var);

the second line is the old method for translating the string into a function -- comment it out and replace it by the line above which uses a Formula object to make the same translation.



I've made this change to the current version of PGgraphmacros.pl in the CVS



--Mike

In reply to Michael Gage

Re: Graphing functions like abs(x) with MathObjects

by Davide Cervone -
I thought it would be something like that.

There are a couple of things to think about with this change, however. First, the $var variable might not be in the current context, so it might be worth making a copy of the current context and adding the variable (if it is not already there) and then create the Formula in that context instead.

Second, I think there will be a difference between this approach and the original one when the function to be plotted is not defined over the whole domain of the plot. The original would not plot anything on the region where the function is undefined, but this version will probably generate an error message.

You might be able to use something like this:

    my $context = Context()->copy;
    $context->variables->add($var=>'Real') unless $context->variables->get($var);
    my $f = Formula($context,$rule);
    my $subRef = sub {my $x = shift; Parser::Evaluate($f,$var=>$x)}
which uses Parser::Evaluate to do error trapping on the evaluation. It is a little less efficient, but should prevent error messages when the function is plotted on domains where it is not defined.

Davide

In reply to Davide Cervone

Re: Graphing functions like abs(x) with MathObjects

by Michael Gage -
Thanks for the suggestions. I've modified the code in PGgraphmacros.pl
to implement them. I've also called Context and Formula directly from
the modules so that MathObjects.pl does not need to be loaded. Loading
MathObjects caused confusion between the various forms of matrices.
(Matrix, MatrixReal1, and Value::Matrix).

The new version of the file is in the CVS.

-- Mike