WeBWorK Main Forum

Help with programming a particular problem

Help with programming a particular problem

by Dan Margalit -
Number of replies: 3
Hi,

I was wondering if someone could help me program a particular problem. Mike Gage was nice enough to point me to the description of how to deal with multi-answer problems on the wiki, but unfortunately this seems involved to me. (I'd be more comfortable mimicking an existing .pg problem if someone could point one out.)

The problem is a Riemann sum that you have to turn into an integral. The Riemann sum is the sum of:

\frac{\pi}{$a n}
\tan\left(\frac{i \pi}{$p n}\right)

or, in more readable form:

pi/($a*n) tan( (i*pi) / ($p*n) )

(here, $p = $a*$b, where $b is randomly chosen).

The student is supposed to enter the left and right limits of integration and the function.

So if they enter left and right limits P and Q, the answer is:

pi/((Q-P)*$a) tan( pi/((Q-P)$a*$b) (x-P) )

(please correct me if I've screwed this up!)

I'll put the text of the original .pg file below. I am guessing that this would be very easy for someone else. Any help would be greatly appreciated.

Thanks!

Dan


## DESCRIPTION
## Calculus: Areas and Distances
## ENDDESCRIPTION

##KEYWORDS('calculus', 'areas', 'distances')
## Tagged by XW

## DBsubject('Calculus')
## DBchapter('Integrals')
## DBsection('Area and Distance')
## Date('5/30/2005')
## Author('Jeff Holt')
## Institution('UVA')
## TitleText1('Calculus: Early Transcendentals')
## EditionText1('5')
## AuthorText1('Stewart')
## Section1('5.1')
## Problem1('19')

## TitleText2('Calculus: Early Transcendentals')
## EditionText2('6')
## AuthorText2('Stewart')
## Section2('5.1')
## Problem2('')

DOCUMENT();

loadMacros(
"PG.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGanswermacros.pl",
"PGauxiliaryFunctions.pl"
);

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

$a = random(4,8,2);
$b = random(2,5,1);
$p = $a*$b;
$pi = 3.1415926535898;

TEXT(EV2(<
The value of the limit
\[
\lim_{n\rightarrow\infty}\sum_{i=1}^{n} \frac{\pi}{$a n}
\tan\left(\frac{i \pi}{$p n}\right)
\]

is equal to the area below the graph
of a function \(f(x)\) on an interval \([A,B]\). Find
\(f\), \(A\), and \(B\). (Do not evaluate the limit.)
$BR
$BR
\(f(x)\) = \{ans_rule(20) \}
$BR
$BR
\(A\) = \{ans_rule(20) \}
$BR
$BR
\(B\) = \{ans_rule(20) \}
$BR

EOT

@ans = ( fun_cmp("tan(x/$b)", vars=>"x"), num_cmp(0), num_cmp($pi/$a));
ANS(@ans);

ENDDOCUMENT();
In reply to Dan Margalit

Re: Help with programming a particular problem

by Davide Cervone -
Dan:

The complete problem didn't come through (because of the << that made Moodle think it was the start of an HTML tag), but I looked it up in the NPL from the tag data.

In any case, the original problem doesn't matter that much. If you are going to make a MultiAnswer problem, it has to be completely rewritten anyway. (Also, that problem uses an older approach to the text output, and should be changed anyway.)

Here is a shell for a problem that accepts P, Q, and a function and checks if the function is of the form given in your message above (depending on P and Q). You will need to write your own text for the problem, and you may want to make additional checks for values of P and Q (like Q > P) if you want those.


    loadMacros(
      "PGstandard.pl",
      "MathObjects.pl",
      "parserMultiAnswer.pl",
      "PGcourse.pl",
    );

    TEXT(beginproblem);

    $a = random(4,8,2);
    $b = random(2,5,1);

    $ma = MultiAnswer(0,1,"(pi/$a) tan((pi/($a*$b))*x)")->with(
      checker => sub {
        my ($correct,$student,$ans) = @_;
        my ($P,$Q,$f) = @$student;
        $g = Formula("pi/(($Q-$P)*$a) * tan((pi/(($Q-$P)*$a*$b))*(x-$P))");
        return $f == $g;
      }
    );

    BEGIN_TEXT
    (a = $a, b = $b)$PAR
    P = \{$ma->ans_rule(5)\}$BR
    Q = \{$ma->ans_rule(5)\}$BR
    f = \{$ma->ans_rule(30)\}
    END_TEXT

    ANS($ma->cmp);
    $showPartialCorrectAnswers = 1;

See the parserMultiAnswer.pl file for more details about the options you can set for it. Note that you need to give the MultiAnswer object a correct answer, so it has something to display when students ask for correct answers.

Hope that helps.

Davide

In reply to Davide Cervone

Re: Help with programming a particular problem

by Dan Margalit -
Thanks, Davide!

I think I have the problem working the way I want. I'll post the resulting .pg file below. I should be able to use this template in the future for similar problems.

Dan

DOCUMENT();

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"parserMultiAnswer.pl",
"PGcourse.pl",
);

TEXT(beginproblem);

$a = random(4,8,2);
$b = random(2,5,1);

$ma = MultiAnswer("(pi/$a) tan((pi/($a*$b))*x)",0,1)->with(
checker => sub {
my ($correct,$student,$ans) = @_;
my ($f,$P,$Q) = @$student;
$g = Formula("pi/(($Q-$P)*$a) * tan((pi/(($Q-$P)*$a*$b))*(x-$P))");
return $g == $f;
}
);



$a = random(4,8,2);
$b = random(2,5,1);
$p = $a*$b;
$pi = 3.1415926535898;

BEGIN_TEXT;

The value of the limit
\[
\lim_{n\rightarrow\infty}\sum_{i=1}^{n} \frac{\pi}{$a n}
\tan\left(\frac{i \pi}{$p n}\right)
\]

is equal to the area below the graph
of a function \(f(x)\) on an interval \([A,B]\). Find
\(f\), \(A\), and \(B\). (Do not evaluate the limit.)
$BR
$BR
\(f(x)\) = \{$ma->ans_rule(30) \}
$BR
$BR
\(A\) = \{$ma->ans_rule(5) \}
$BR
$BR
\(B\) = \{$ma->ans_rule(5) \}
$BR

END_TEXT;

ANS($ma->cmp);
$showPartialCorrectAnswers = 1;

ENDDOCUMENT();
In reply to Dan Margalit

Re: Help with programming a particular problem

by Davide Cervone -
Dan:

Looks good. Note that you don't need to set a value for $pi because you never use it. In any case, there is already a variable $PI that contains the most precise version that perl can store, and there is also a function pi that returns the same value, so there is no need ever to write it out by hand.

Davide