WeBWorK Main Forum

Problems with the following script

Problems with the following script

by Ravinder Kumar -
Number of replies: 4
I am reproducing a script for solving a differential equation problem that asks for the integrating factor and then the solution.

Here are the problems

integrating factor must be given in the form x^n. If given in the form e^(n ln(x)), it does not recognize it and reports incorrect answer. If given in the form 1/x^m, the integrating factor is gradede correct but correct solution is graded incorrect. I copied the answer checker from the solution answer in the script and modifeid it. It did not work work either.   Help needed. Thanks. Here is the script
_____________________
\## DESCRIPTION
## First order ODEs: separable differential equations
## ENDDESCRIPTION

## KEYWORDS('differential equations','first order','first order linear differential equations')

## DBsubject('Differential Equations')
## DBchapter('First Order Differential Equations')
## DBsection('Separable Equations')
## Date('01/30/2011')
## Author('Paul Pearson')
## Institution('Fort Lewis College')
## TitleText1('Notes on Diffy Qs')
## EditionText1('December 9, 2010')
## AuthorText1('Jiri Lebl')
## Section1('1.3')
## Problem1('4')


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

DOCUMENT(); 

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"parserAssignment.pl",
"AnswerFormatHelp.pl",
);

TEXT(beginproblem());


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

Context("Numeric")->variables->add(
y=>"Real", k=>"Real"
);
parser::Assignment->Allow;

$a = random(3,15,1);
$b=2-$a;
$answer1 =Compute("x^(-$a)");
$answer2 = Compute("y = x^2/$b+k*x^$a");


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

Context()->texStrings;
BEGIN_TEXT


Solution to the following differential equation can be found by treating it as $BR
first order linear differential equation.
$BR
 
\( \displaystyle  x \frac{dy}{dx} = x^{2} + $a y \)
$BR


Integrating factor for this differential equation is  

\{ ans_rule(20) \}
$BR
Note: You must write Your answer in the form "x^n".
$PAR

Find a solution to 
\( \displaystyle  x \frac{dy}{dx} = x^{2} + $a y \).
$BR
Use must use k for constant of integration.
$BR
\{ ans_rule(30) \}
\{ AnswerFormatHelp("equations") \}
END_TEXT
Context()->normalStrings;



##############################
#  Answer evaluation 

$showHint = 2;
BEGIN_HINT
Write the differential equation in the form $BR
\( \displaystyle \frac{dy}{dx} + P(x)y = Q(x) \) 

END_HINT

$showPartialCorrectAnswers = 1;


ANS( $answer1->cmp() ); 
 
 

ANS( $answer2->cmp( checker => sub {
    my ( $correct, $student, $self ) = @_;
    if ($self->{_filter_name} ne 'produce_equivalence_message') {
      my $stu = Formula($student->{tree}{rop});
      if ($stu->isConstant) {
         Value::Error('Your answer should not be constant');
         return 0;
      }
      my $stu_x = $stu->D('x');
      return $stu_x == Formula("$a*$stu /x + x");
    }
  })
);

COMMENT("MathObject version.");

ENDDOCUMENT();

In reply to Ravinder Kumar

Re: Problems with the following script

by D. Brian Walton -
I can give a quick answer for the first issue.  That has to do with the domain.  x^n is defined for positive and negative numbers x, but exp(n ln(x)) is only defined for positive numbers x.  The answer checker requires that the programmed answer and the student answer agree at all tested points.  You could restrict your test points to only include positive numbers.

I don't understand what you mean about 1/x^m.  I tried your code and found an integrating factor 1/x^11.  It accepted my answer "y = -1/9 x^2 + k*x^11".

But it also accepted the nongeneral solution "y = -1/9 x^2". I think you might be suffering from the challenges of checking solutions numerically.  The power x^11 is so small when |x|<1 that the second term can be missing and still be within the acceptable precision for purposes of comparison.

- Brian
In reply to D. Brian Walton

Re: Problems with the following script

by Ravinder Kumar -
Thank you Brian.

I have set domain for x to (0, \infty)
I added an answer checker for the first answer with a tolerance of 10^(-16) inside the $ans ->cmp(      )   command.

The problems are the same as before. 

I am new to authoring and editing of problems. Can you please show me how to fix the problems? That will help me learn a few techniques. Thanks.


Ravinder
In reply to Ravinder Kumar

Re: Problems with the following script

by D. Brian Walton -
Ravinder,

I looked at your solution checker more carefully.  The domain issue is certainly the first problem.  However, I would not use such a large domain for the purpose of checking the answer. Perhaps the following choice for x:

Context()->variables->set(x=>{limits=>[0,2]})

The second part looks to be a little trickier.  It appears that you want to verify that the solution provided by the student satisfies the differential equation rather than seeing if the solution matches a particular form of the general solution.

The reason that y=-1/9x^2 was marked correct when the general answer should have been y=-1/9x^2+kx^11 is because y=-1/9x^2 IS a solution to the differential equation.

My proposal for you is to explicitly check that the answer depends on k AND is a solution to the differential equation.  See the following modification to your answer checker.

Best wishes,

- Brian

ANS( $answer2->cmp( checker => sub {
    my ( $correct, $student, $self ) = @_;
    if ($self->{_filter_name} ne 'produce_equivalence_message') {
      my $stu = Formula($student->{tree}{rop});
      if ($stu->isConstant) {
         Value::Error('Your answer should not be constant');
         return 0;
      }
      my $stu_x = $stu->D('x');
      my $isSoln = $stu_x == Formula("$a*$stu /x + x");

      my $stu_k = $stu->D('k');
      if ($isSoln && $stu_k == Formula("0")) {
         Value::Error('You need to find the general solution');
         return 0;
      }
      return $isSoln;
    }
  })
);

In reply to D. Brian Walton

Re: Problems with the following script

by Ravinder Kumar -
Thanks Brian.
After writing to you I was looking at some problems in the library and figured out a solution for answer2 on the lines you have suggested. That worked great. Thanks for all your help.