WeBWorK Main Forum

How make Formulas allow ANY variable name? e.g. either 2*x + 1 and 2*k + 1?

How make Formulas allow ANY variable name? e.g. either 2*x + 1 and 2*k + 1?

by Christian Seberino -
Number of replies: 3
In my Algebra 1 class I sometimes ask students to translate a verbal description of an expression in words to one in algebraic notation.

My answers look like Formula("2*x + 1").

How make Webwork mark it correct if they choose another variable name
like y or k or something else?

cs
In reply to Christian Seberino

Re: How make Formulas allow ANY variable name? e.g. either 2*x + 1 and 2*k + 1?

by Davide Cervone -
This would require writing your own parser objects to implement the ability to use any variable, and to properly compare two functions that use different variables. That can be done, but it is subtle, and so not something to be done by the faint of heart.

The parserFormulaUpToConstant.pl file in the pg/macros directory does something very close to this in order to handle formulas with arbitrary constants (the student can use any variable). That would be a good place to start if you wanted to try to implement that.

One thing you would want to think about is what would you do if the student entered two different variables? What messages should be provided? I don't see a reasonable way to do multivariable functions this way.

Davide
In reply to Christian Seberino

Re: How make Formulas allow ANY variable name? e.g. either 2*x + 1 and 2*k + 1?

by Davide Cervone -
I've found time to write the code needed for this, and attach it here. There is documentation in the file itself, so read that to see how to use it.

Hope that does what is needed.

Davide
In reply to Davide Cervone

Re: How make Formulas allow ANY variable name? e.g. either 2*x + 1 and 2*k + 1?

by Christian Seberino -
Davide

It works great!  Just now got around to trying it!

cs