## WeBWorK Problems

### Constants and General Solutions to ODEs ### Constants and General Solutions to ODEs

by Lynda Danielson -
Number of replies: 7

I was trying to use the "parserFormulaUpToConstant.pl" when evaluating the solution to a separable differential equation.

$fp = FormulaUpToConstant("-1/( ($coef/($exp+1))x^($exp+1) + C)")->reduce();

...

ANS($fp->cmp); This generated the Error message Your formula isn't linear in the arbitrary constant 'C' Is there another Macro I should be loading and using in these cases? Thank you! In reply to Lynda Danielson ### Re: Constants and General Solutions to ODEs by Davide Cervone - FormulaUpToConstant is really just for antiderivatives: answers where the student is supposed to enter f(x) + C and must actaully have the "+C" as part of the answer. In your case, the error is correct, since your formula ISN'T linear in C. It is of the form -1/(stuff + C) so the C is in the denominator. If you want a C in an arbitrary position, you might be better off adding C as a variable, and using a plain formula (that includes both x and C). The students will have to use the letter that you specified (unlike FormulaUpToConstant, where they can use whatever letter they like), but that is the cost of having the constant in an arbitrary position in the formula. Alternatively, if you are looking to allow answers where the students have a SPECIFIC constant and you don't care what it is (e.g., you want -1/(stuff + 12) to be accepted, then you want an adaptive parameter instead. These are done by adding variables of type "Parameter" to the Context rather than type "Real". Davide In reply to Davide Cervone ### Re: Constants and General Solutions to ODEs by Lynda Danielson - Thanks Davide, But what I'd really like is the ability to use C in an arbitrary position. Currently, if the correct answer is: -1/(4/5 x^5 +C) Webwork will indicate the following correct answer as incorrect: -5/(4x^5 +C) Of course, Webwork will accept -5/(4x^5 +5C) as correct, but this could be frustrating for students just learning about solving differential equations (especially when taught 5C is "just another" C) . I could throw in an initial condition, but wondered if a macro had been written to evaluate functions with "non-linear" arbitrary constants. In reply to Lynda Danielson ### Re: Constants and General Solutions to ODEs by Davide Cervone - OK, how about using "C" as a variable and an adaptive parameter "a" with the correct answer being  -1/(4/5x^5 + aC)  with a custom checker that verifies that the "a" is non-zero? (Or does it really need to be -1/(4/5x^5+aC + b) for adaptive parameters a and b? I.e., should -1/(4/5x^5 + 1 + C) be considered correct?) Something like this (untested) code:  Context()->variables->add(C=>'Real'); Context()->variables->add(a=>'Parameter');$f = Formula("-1/((4/5)x^5 + aC")
$f->{correct_ans} =$f->substitute(a=>1)->reduce;

ANS($f->cmp(checker => sub { my ($correct,$student,$ans) = @_;
return 0 unless $correct ==$student;
return Real(Context()->variables->get("a")->{value}) != 0;
}));

A more sophisticated version of this could be packaged up as a new MathObject (like FormulaUpToConstant). I would normally write that for you, but I'm a little pressed for time at the moment, so it will have to wait. I'll add it to my list of things to do.

Davide ### Re: Constants and General Solutions to ODEs

by Lynda Danielson -

Thanks again Davide.  I added the above code.  This didn't work, as the correct answer yielded "incorrect" with the message:

I will understand if this is too time-consuming to consider right now.  Lynda ### Re: Constants and General Solutions to ODEs

by Davide Cervone -
OK, I've looked into it further, and it looks like the adaptive parameters can only be used when the function depends linearly on the parameters (the method used to solve for them assumes linearity). So that means my suggestion won't work, since the function is not linear in the parameters. A more sophisticated adaptive parameter approach would be needed.

With that in mind, I don't see any straight-forward way to do what you are looking for. I'll keep thinking about it.

Davide ### Re: Constants and General Solutions to ODEs

by Gavin LaRose -
Hi Lynda and Davide,

A mathematically correct but not very pleasing work-around is to specify the form of the general solution by having the solution satisfy an "initial condition" like y(0) = C. This allows marking the problem with standard function graders, treating the constant C as a variable. It's not an aesthetically pleasing solution to the issue, however.

Gavin ### Re: Constants and General Solutions to ODEs

by Alex Jordan -
Hi Lynda,

I know you posted this question a long time ago, but I just wrestled with this problem, and the "solution" I came works like this:

Students will be required to use C for the arbitrary constant, and C should be added as a variable to the context.

$answer = Formula("1/(x+C)"); which is the solution to y' = -y^2 Make a custom answer checker that checks for two things: 1. the student's answer solves the differential equation 2. whatever the student has entered in place of C is a linear (and nonconstant) function of C I probably do not have the most efficient way to do this, but my custom checker works like this: ANS($answer->cmp( checker=>sub {
my ( $correct,$student, $ansHash ) = @_; my$dstudent = $student->D('x'); my$lhs = Formula("$dstudent"); my$rhs = Formula("-($student)^2"); my$studentC = Formula("1/$student - x"); my$dstudentC = $studentC->D('C'); my$ddstudentC = $dstudentC->D('C'); if (($lhs == $rhs) && ($ddstudentC == Formula("0")))