WeBWorK Problems

Filling a table with Numeric and Point

Filling a table with Numeric and Point

by Teresa Adams -
Number of replies: 1
I am trying to build a table and the table has answers that are y-values, ordered pairs, and the secant of a line. I will be quite honest, I am really not sure what I am doing. This is what I have so far:

###########################
# Initialization

DOCUMENT();

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"AnswerFormatHelp.pl",
"PGML.pl",
"PGcourse.pl",
"PGgraphmacros.pl",
"scaffold.pl",
"contextLimitedPoint.pl",
);


TEXT(beginproblem());
$showPartialCorrectAnswers = 1;

###########################
# Setup
# $x0 is the x-value in the ordered pairs P(x, y)
# $d is the vertical translation of the function
# $y0 is the y-value in the ordered pairs P(x, y)
# $seq fills in the table values for x
# $df is the derivative of the function
# $df_x0 is the derivative of the function evaluated at x0
# $answer[13] is the derivative of the function evaluated at x0
# $b is the y-intercept of the tangent line
# $tanline is the equation of the line tangent at the point P(x, y)

Context("Numeric");
Context()->flags->set(tolerance => 0.01);
Context()->{format}{number} = "%.4f#";

$x0 = Real(1);

@seq = ();
foreach my $i (1..4) {
$seq[$i] = $x0+1/10**$i;
}

$d = random(-5, 5, 1);

$f = Formula("x^2+$d");

$y0 = $f->eval(x=>$x0);

$df = $f->D('x');

$df_x0=$df->eval(x=>$x0);

$b=$y0-$df_x0*$x0;

$tanline=Formula("$df_x0*x+$b");

@answer = ();
for $i (0..11) {
if ($i==(0, 9, 3)) {
$k=$i/3+1;
$answer[$i] = $f->eval(x=>$seq[$k]);
} elsif ($i==(1, 10, 3)) {
Context("LimitedPoint");
$k=($i-1)/3+1;
$answer[$i]= Point("($seq[$k]","$answer[$k-1])");
} else {
Context("Numeric");
$k=($i-2)/3+1;
$deltay=$y0-$answer[$k-1]
$deltax=$x0-$seq[$k]
$answer[$i]=$deltay/$deltax;
}
}


$answer[13]=$df_x0;

$answer[14]=$tanline;

$table = $BCENTER.begintable(5) .
row( "`x`", "`y`", "`Q(x,y)`", "`m_sec`" ) .
row( "$seq[1]", ans_rule(8), ans_rule(8), ans_rule(8)).
row("$seq[2]", ans_rule(8), ans_rule(8), ans_rule(8)).
row("$seq[3]", ans_rule(8), ans_rule(8), ans_rule(8)).
row("$seq[4]", ans_rule(8), ans_rule(8), ans_rule(8)).
endtable().$ECENTER;

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

# Main text

Scaffold::Begin();

BEGIN_PGML

The points [`P([$x0],[$y0])`] and [` Q(x,y)`] are on the graph of the function [` f(x) = [$f] `]. Complete the table with the appropriate values for [`y`], [`Q(x,y)`], and the slope of the secant line passing through points [`P`] and [`Q`]. Round to four decimal places.
[@ AnswerFormatHelp("numbers") @]*

[@ $table @]***

END_PGML

############################
# Part 1 Answer evaluation

for $i (0..11) {
ANS( $answer[$i]->cmp() );
}


###########################
# Part 2

Section::Begin("Part 2");

BEGIN_PGML

Using the values in the right column of the table, the estimated value of the slope of the line tangent to [`f`] at [`x=[$x0]`] is [_________]

END_PGML


############################
# Part 2 Answer evaluation

ANS( $answer[13]->cmp());

Section::End();

###########################
# Part 3

Section::Begin("Part 3");

BEGIN_PGML

The equation of the tangent line at point [`P`] is [`y=`] [_________]

END_PGML


############################
# Part 3 Answer evaluation

ANS( $answer[14]->cmp());


Section::End();

Scaffold::End();

The error message I am getting is:
If someone could help me, I would greatly appreciate it.
In reply to Teresa Adams

Re: Filling a table with Numeric and Point

by Teresa Adams -
I have it kind of working. But I do have another question I will post with a different heading.