WeBWorK Problems

epsilon delta problem

by Darwyn Cook -
Number of replies: 3
Brian asked me about some epsilon/delta problems that I had written. Since I have yet to get my problems up into the NPL I thought I would include the source code here for one of them.
This problem mimics a problem from the text where the student is given epsilon and has to get the graph to exit the corners of the graphing window to find delta. Unfortunately the real trouble with this problem for my students seems to be the decimal arithmetic, not the epsilon/delta concept.

##DESCRIPTION
#
# File Created: 7/10/2008
# Problem Author: Darwyn Cook
# WeBWorK Entry:
# Location: Alfred University
#
##ENDDESCRIPTION

##KEYWORDS('limits' 'epsilon' 'delta' 'rigorous')
##

## DBsubject('Calculus')
## DBchapter('Limits and Derivatives')
## DBsection('Limits Rigorously')
## Date('7/10/2008')
## Author('Darwyn Cook')
## Institution('Alfred University')
## TitleText1('Calculus: with Early Transcendentals')
## EditionText1('8')
## AuthorText1('Anton')
## Section1('2.4')
## Problem1('')

DOCUMENT(); # This should be the first executable line in the problem.
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGauxiliaryFunctions.pl",
"PGgraphmacros.pl",
"MathObjects.pl");

TEXT(beginproblem());
$showPartialCorrectAnswers = 0; TEXT($BEGIN_ONE_COLUMN);
$refreshCashedImages= 1; Context()->strings->are(blue=>{},red=>{},black=>{},green=>{}); Context()->{format}{number} = "%.8g";$answertolerance = 0.000003;
$functioncolor = String(black);$epsiloncolor = String(red);
$epsilon = random(.001, .01,.0001);$a = random(-3,3,.01);

##############################################################
# Check for student input
if (defined $inputs_ref->{'xmin'} ) {$xmin = $inputs_ref->{'xmin'}; } elsif (defined$in{'xmin'} ) {
$xmin =$in{'xmin'};
}
else {
$xmin =$a-1;
}
# set increment value for y
if (defined $inputs_ref->{'xmax'} ) {$xmax = $inputs_ref->{'xmax'}; } elsif (defined$in{'xmax'} ) {
$xmax =$in{'xmax'};
}
else {
$xmax =$a+1;
}
if (defined $inputs_ref->{'ymin'} ) {$ymin = $inputs_ref->{'ymin'}; } elsif (defined$in{'ymin'} ) {
$ymin =$in{'ymin'};
}
else {
$ymin = -20; } # set increment value for y if (defined$inputs_ref->{'ymax'} ) {
$ymax =$inputs_ref->{'ymax'};
}
elsif (defined $in{'ymax'} ) {$ymax = $in{'ymax'}; } else {$ymax = 20;
}

#####################################################
## This code allows the student to input an arithmetic
## expression, which we reduce to a number. Otherwise
## the graph object will complain in an unfriendly way.
$xmin=Real("$xmin")->reduce;
$xmax=Real("$xmax")->reduce;
$ymin=Real("$ymin")->reduce;
$ymax=Real("$ymax")->reduce;
$xmin->with( tolType=>'absolute', tolerance=>'.0000001');$xmax->with(
tolType=>'absolute', tolerance=>'.0000001');
$ymin->with( tolType=>'absolute', tolerance=>'.0000001');$ymax->with(
tolType=>'absolute', tolerance=>'.0000001');

#####################################################
## Better check the student input, they won't like the
## result if they input the coordinates backwards
if ($xmin >$xmax){
$xmin =$xmax-1;
BEGIN_TEXT
$BBOLD XMIN MUST BE LESS THEN XMAX!$EBOLD
$BR END_TEXT } if ($ymin >= $ymax){$ymin = $ymin-1; BEGIN_TEXT$BBOLD YMIN MUST BE LESS THEN YMAX!$EBOLD$BR
END_TEXT
}

#####################################################
## Define the functions

$f = Formula("sin(pi*(x^2))");$dfdx = $f->D('x');$m = $dfdx->eval(x=>$a)->with(
tolType=>'absolute', tolerance=>$answertolerance); while ($m == 0){
$a = random(-3,3,.01);$m = $dfdx->eval(x=>$a);}
$y =$f->eval(x=>$a)->with( tolType=>'absolute', tolerance=>$answertolerance);
$fplusepsilon = Formula("$y+$epsilon");$fminusepsilon = Formula("$y-$epsilon");

######################################################
## Now we are going to use the quadratic Taylor series approximation
## to f to estimate delta. Close enough for this application.
## First we define the coefficients $a0+$a1x+$a2x^2$dfdxdx = $dfdx->D('x');$c = $dfdxdx->eval(x=>$a);
#$a1 = Real("$m-$a*$c");
#$a2 = Real("$a^2*$c/2"); #$delta = Real("
#(-(2*$a2*$x+$a1)+sqrt((2*$a2*$x+$a2)^2+4*$a2*$epsilon))/(2*$a2)")->with(tolType=>'absolute', #tolerance=>'.00001'); ###################################################### ## This would be the tangent line approximation$delta = Real(abs($epsilon/$m))->with(
tolType=>'absolute', tolerance=>'0.000003');;

######################################################
## Generate the graphs

$size = [200,250];$tex_size = 350;

## initialize the graph and add the functions in
$gr = init_graph($xmin,$ymin,$xmax,$ymax,axes=>[$a,$y],size=>[400,400]); add_functions($gr,
"$f for x in <$xmin,$xmax> using color:$functioncolor and weight:2");
add_functions($gr, "$fplusepsilon for x in <$xmin,$xmax> using color:$epsiloncolor and weight:1"); add_functions($gr,
"$fminusepsilon for x in <$xmin,$xmax> using color:$epsiloncolor and weight:1");
$gr->stamps( closed_circle($a,$y,'black') );$in=time();
$gr->gifName($gr->gifName()."$in"); ####################################################### ## The code to generate student input boxes$len = 1;
$INPUTSPACEXMIN = M3( qq! \\rule{${len}in}{.01in}!,
qq!\begin{rawhtml}<INPUT TYPE = "TEXT" NAME="xmin"
VALUE="$xmin">\end{rawhtml}!, qq!<INPUT TYPE = "TEXT" NAME="xmin" VALUE="$xmin">!
);
$INPUTSPACEXMAX = M3( qq! \\rule{${len}in}{.01in}!,
qq!\begin{rawhtml}<INPUT TYPE = "TEXT" NAME="xmax"
VALUE="$xmax">\end{rawhtml}!, qq!<INPUT TYPE = "TEXT" NAME="xmax" VALUE="$xmax">!
);
$INPUTSPACEYMIN = M3( qq! \\rule{${len}in}{.01in}!,
qq!\begin{rawhtml}<INPUT TYPE = "TEXT" NAME="ymin"
VALUE="$ymin">\end{rawhtml}!, qq!<INPUT TYPE = "TEXT" NAME="ymin" VALUE="$ymin">!
);
$INPUTSPACEYMAX = M3( qq! \\rule{${len}in}{.01in}!,
qq!\begin{rawhtml}<INPUT TYPE = "TEXT" NAME="ymax"
VALUE="$ymax">\end{rawhtml}!, qq!<INPUT TYPE = "TEXT" NAME="ymax" VALUE="$ymax">!
);

########################################################
## The problem
Context()->texStrings;
BEGIN_TEXT
\{ image( insertGraph($gr),, width=>400, height=>400,tex_size=>250) \}$PAR
Use the graph of f to find $$\delta$$ so that $$\vert f(x) - f(a)\vert <epsilon$$ when $$\vert x-a\vert < \delta$$ if $$a = a$$ and $$f(a) = y$$.
$BR $$\delta =$$ \{ans_rule(20)\}$BR
To change the graph enter values for
$PAR x min =$INPUTSPACEXMIN x max= $INPUTSPACEXMAX$PAR
y min = $INPUTSPACEYMIN y max=$INPUTSPACEYMAX
$BR and then press the Submit Answer button.$PAR
$BBOLD Note:$EBOLD $$f(a) + \epsilon$$ and $$f(a)-\epsilon$$ are graphed in red to help you find $$\delta$$. Mathematically speaking, once you have found $$\delta$$ any smaller number will also work for $$\delta$$. Since that would make the problem rather easy, you could just put in a very small number, for this problem you will need to find the largest delta that will work. Your answer must be within $answertolerance of the correct answer. END_TEXT BEGIN_HINT$BR
$BR$BBOLD HINT $EBOLD$BR
$$\epsilon$$ determines the vertical size of the window, you want it at $$y-\epsilon$$ and $$y+\epsilon$$. The horizontal spacing you will have to guess at, how will you know when you have it right.
END_HINT
BEGIN_SOLUTION
$BR$BR
$BBOLD SOLUTION$EBOLD
$BR You have the correct horizontal window when the graph exits the corners of the windows. END_SOLUTION Context()->normalStrings; ANS($delta->cmp);
ENDDOCUMENT(); # This should be the last executable line in the problem.

Re: epsilon delta problem

by Darwyn Cook -
Here is a similar problem that uses oracle functions instead of graphs:

##DESCRIPTION
#
# File Created: 7/10/2008
# Problem Author: Darwyn Cook
# WeBWorK Entry:
# Location: Alfred University
#
##ENDDESCRIPTION

##KEYWORDS('limits' 'epsilon' 'delta' 'rigorous')
##

## DBsubject('Calculus')
## DBchapter('Limits and Derivatives')
## DBsection('Limits Rigorously')
## Date('7/10/2008')
## Author('Darwyn Cook')
## Institution('Alfred University')
## TitleText1('Calculus: with Early Transcendentals')
## EditionText1('8')
## AuthorText1('Anton')
## Section1('2.4')
## Problem1('')

DOCUMENT();
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGnumericalmacros.pl",
"PGauxiliaryFunctions.pl",
"MathObjects.pl"
);

# define function to be evaluated

$a= random(14,18,.1);$b= random(2,20,.1);
$c = random(2,40,.1);$x0=random(1,5,0.001);
$epsilon = random(.0001,.0009,.0001);$m = Real("2*$a*$x0+$b"); #$delta = Real("(-(2*$a*$x0+$b)+sqrt((2*$a*$x0+$b)^2+4*$a*$epsilon))/(2*$a)")->with(tolType=>'absolute', tolerance=>'.000001');$delta = Real("abs($epsilon/$m)")->with(tolType=>'absolute',tolerance=>'.000001');
$function = FEQ("${a}x^2+${b}x +$c ");
$f = sub { my$x = shift;
$a*$x*$x +$b*$x +$c;
};

# Calculate the cubic spline approximation
$llimit = -5;$rlimit = 5;
my $num_of_intervals = 500; # number of interpolation points my$deltax = ($rlimit-$llimit)/($num_of_intervals); my (@x_values, @y_values); foreach my$i (0..$num_of_intervals) { my$x = $llimit +$deltax*$i; my$y = &$f($x);
push(@x_values, $x); push(@y_values,$y);
}

($t_ref,$a_ref, $b_ref,$c_ref, $d_ref) = create_cubic_spline (~~@x_values, ~~@y_values);$f_approx = cubic_spline(~~@x_values, ~~@y_values);

HEADER_TEXT(javaScript_cubic_spline(~~@x_values,~~@y_values, name => 'func2', llimit => -3, rlimit => 3) );

TEXT(beginproblem());

# A warning that we are using javaScript
TEXT(M3("",
"\begin{rawhtml}
<NOSCRIPT> This problem requires that Java Script be enabled </NOSCRIPT> \end{rawhtml}
",
"<NOSCRIPT> This problem requires that Java Script be enabled </NOSCRIPT>"
));

Context()->texStrings;
BEGIN_TEXT
Below is an "oracle" function. An oracle function is a function presented interactively.
When you type in an $$x$$ value, and press the --f--$$>$$ button and
the value $$f(x)$$ appears in the right hand window. There are three lines, so you can easily calculate
three different values of the function at one time.

$PAR Find the largest $$\delta$$ so that $$\vert f(x)-f(x0)\vert <epsilon$$ when $$\vert x-x0\vert < \delta$$.$BR
$$\delta =$$\{ans_rule(20) \}
$PAR END_TEXT BEGIN_HINT$BR
$BR$BBOLD HINT $EBOLD$BR
Use this oracle function to determine $$f(x0)$$. Use the oracle function to determine the input values that give you exactly the tolerance required.
END_HINT

BEGIN_SOLUTION
$BR$BR
$BBOLD SOLUTION$EBOLD
$BR $$delta$$ is the correct solution because $$f(x0+delta) = f(x0)+epsilon$$, or at least to the required number of decimal places. END_SOLUTION Context()->normalStrings; ANS($delta->cmp());

<SCRIPT Xlanguage="JavaScript">
<!-- Begin
function func(x) {
return $a*x*x +$b*x + $c } --> </SCRIPT> EOF$javaScript2 = begintable(3) .
MODES(HTML =>q{<TR>
<TH>x</TH><TH></TH><TH>f(x)</TH>
<TR>},
TeX => "x & $$\rightarrow$$ & f(x)\\"
) .
row(MODES(HTML => qq{<INPUT TYPE="text" NAME="Input" Value = "$x0" Size="16">}, TeX => '\fbox{Enter $$x$$}' ), MODES(HTML => qq{<INPUT TYPE="button" VALUE="---f-->" XonClick="this.form.Output.value=func(this.form.Input.value);">}, TeX => '$$\rightarrow$$' ), MODES(HTML => qq{<INPUT TYPE="text" NAME="Output" Size="30">}, TeX => 'result: $$f(x)$$') ) . row(MODES(HTML => qq{<INPUT TYPE="text" NAME="Input2" Value = "$x0" Size="16">}, TeX => '\fbox{Enter $$x$$}' ),
MODES(HTML => qq{<INPUT TYPE="button" VALUE="---f-->"
XonClick="this.form.Output2.value=func(this.form.Input2.value);">}, TeX => '$$\rightarrow$$' ),
MODES(HTML => qq{<INPUT TYPE="text" NAME="Output2" Size="30">}, TeX => 'result: $$f(x)$$')
) .
row(MODES(HTML => qq{<INPUT TYPE="text" NAME="Input3" Value = "$x0" Size="16">}, TeX => '\fbox{Enter $$x$$}' ), MODES(HTML => qq{<INPUT TYPE="button" VALUE="---f-->" XonClick="this.form.Output3.value=func(this.form.Input3.value);">}, TeX => '$$\rightarrow$$' ), MODES(HTML => qq{<INPUT TYPE="text" NAME="Output3" Size="30">}, TeX => 'result: $$f(x)$$') ) . endtable(); TEXT($javaScript2);

ENDDOCUMENT;

Re: epsilon delta problem

by Darwyn Cook -
And another version that uses a table of values instead of the oracle function. One of the "drawbacks" to these problems is that student is forced to get the maximum delta that works, otherwise they could just guess a very small number and get the answer correct.

##DESCRIPTION
#
# File Created: 7/10/2008
# Problem Author: Darwyn Cook
# WeBWorK Entry:
# Location: Alfred University
#
##ENDDESCRIPTION

##KEYWORDS('limits' 'epsilon' 'delta' 'rigorous')
##

## DBsubject('Calculus')
## DBchapter('Limits and Derivatives')
## DBsection('Limits Rigorously')
## Date('7/10/2008')
## Author('Darwyn Cook')
## Institution('Alfred University')
## TitleText1('Calculus: with Early Transcendentals')
## EditionText1('8')
## AuthorText1('Anton')
## Section1('2.4')
## Problem1('')

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

"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGauxiliaryFunctions.pl",
"MathObjects.pl");

TEXT(beginproblem());
$showPartialCorrectAnswers = 0; #TEXT($BEGIN_ONE_COLUMN);
## Overide the default computing numbers to six decimal places.
Context()->{format}{number} = "";
## $x1 is the point where we will estimate the derivative.$x1 = random(.001, .01,.0001);
$epsilon = random(.0001,.0004,.00001);$answertolerance = 0.00001;
# check inputs_ref to see if the student modified $dh, otherwise we set it to ## 0.0001. if (defined(${$inputs_ref}{'dh'}) ) {$dh = ${$inputs_ref}{'dh'};
}
else {
$dh = .0001 } ## Define the function f and it's derivative$f = Formula("x^2+x");
$f->perlFunction(f1);$dfdx = $f->D('x');$dfdxx0 = $dfdx->eval(x=>$x1)->with(
tolType=>'absolute', tolerance=>'$answertolerance');$delta = Real("abs($epsilon/$dfdxx0)")->with(
tolType=>'absolute', tolerance=>'0.000001');

## Create the list of values to be put into the table.
my $i; my @input = (); my @output = (); foreach$i (-2..2) {

push(@input,spf($x1+$i*$dh , "%12.8g") ); push(@output, spf( f1($x1+$i*$dh) , "%12.8g") );
}

# Present the text.
TEXT(EV2(<<EOT));
Given the following table:
EOT

TEXT(
&begintable(1+scalar(@input)),
&row("x",@input),
&row("f(x)",@output),
&endtable()
);
$len = 1;$INPUTSPACE = M3( qq! \\rule{${len}in}{.01in}!, qq!\begin{rawhtml}<INPUT TYPE = "TEXT" NAME="dh" VALUE="$dh">\end{rawhtml}!,
qq!<INPUT TYPE = "TEXT" NAME="dh" VALUE="$dh">! ); Context()->texStrings; #TEXT(EV2(<<EOT)); BEGIN_TEXT Use the table to find the largest $$\delta$$ so that $$\vert f(x)-f(x1)\vert <epsilon$$ when $$\vert x-x1\vert < \delta$$.$BR
$$\delta =$$\{ans_rule(20) \}
$PAR$PAR
To obtain more precise information about the value of $$f$$ near $$x1$$
enter a new increment value for $$x$$ here
$INPUTSPACE and then press the Submit Answer button.$PAR
Your answer must be within $$answertolerance$$ of the correct answer.
END_TEXT
#EOT
Context()->normalStrings;
ANS($delta->cmp); #TEXT($END_ONE_COLUMN);
ENDDOCUMENT(); # This should be the last executable line in the problem.