views:

81

answers:

7

Hi, I'm a math teacher wanting to insert some dynamic math into a website. What I'd like to achieve is to have a button that a student can press to randomly vary a question so that it's the same type of question, but with different numbers. For example,

Factor the quadratic expression of the form ax^2 + bx + c, where a = 1, and b and c are positive integers between 1 and 100, and such that the roots would be real whole numbers.

If I'm using MathML to encode the math (e.g., like the markup below), stored in a database (e.g., MySQL), how could I set things up so that the computer automatically and randomly varies the math expression in the way I described above? I don't know much about server-side scripting... could I achieve this with PHP? Or would this be more a job of JavaScript on the client side? I'm just looking for some advice to guide my choice of study path. Thank you

<math xmlns='http://www.w3.org/1998/Math/MathML'&gt;
 <mrow>
  <msup>
   <mi>x</mi>
   <mn>2</mn>
  </msup>
  <mo>+</mo>
  <mrow>
   <mn>7</mn>
   <mo>&#8290;</mo>
   <mi>x</mi>
  </mrow>
  <mo>+</mo>
  <mn>12</mn>
 </mrow>
</math>
+1  A: 

Well, you can use javascript and random numbers to vary the coefficients. Have a look at the following web side for agood lesson on how to do this:

JavaScript: Random Scripts

Michael Goldshteyn
I will, thanks.
Bill
+1  A: 

I would try the Google Mathematical (TeX) Formulas. It will be a lot cleaner.

You can do it server side if you understand javascript, php isn't that hard.

The database should only be storing the values/type of expression (a,b,c) you don't to store presentation specific stuff in the db. I would make a generator that creates values a,b,c and store them in the db.

Ken Struys
So you discourage the use of MathML? Browser support seems to be improving lately (esp. Firefox, and IE has a "MathType" plugin.
Bill
+1  A: 

something like this might help :

<html>

<?php

//if the button has been pressed
if(isset($_POST['click'])){
    //generate a random number from 1 - 5 and store it in $random
$random = rand(1,5);
    //query the database for the equation with the id of $random
$result = $mysql_query("select equation from questions where id=$random");
$row = mysql_fetch_row($result);
    //print the equation
echo $row[0];
}

?>

<form method="post" action="">
<input type="submit" name="click" value="go"/>
</form>  
</html>
Orbit
I'm not well versed enough in PHP to get that, but I'll keep it in mind! Thanks.
Bill
Not much going on here, if you have equations that are browser readable and stored in a database, you can generate a random number (5=#ofequations) and pull and print that row. the code below is just a button that will trigger the top code. i'll add comments for readability.
Orbit
Thanks for adding the code comments.
Bill
+1  A: 

You can certainly do it in PHP on the server side. PHP has built-in libraries for XML parsing which should be good in your case. It's easy to just randomize a, b & c but to always have a real answer you must use some other algorithm to generate the numbers. Since you're a math teacher I think you are as good as anyone in finding such an algorithm.

I think you should split the work up into different parts:

  • Fetch a question from MySQL (this is easy)
  • Parse the MathML (maybe you can find a library to do this?)
  • Generating the random numbers

The last one is the hardest, especially if you want a generic system. How do the computer know what sort of answer you are looking for? Maybe you need specific algorithm for different types of questions, because sometimes you do want a complex number, and sometimes you need a sinus wave. Each question may need to be solved by a different approach on your behalf.

There are (more or less) generic algorithms for solving e.g. polynomial equations out there. You do probably know some of the approaches already in your profession. Maybe such an algorithm can be used? I think you will find out that most of the hard work is actually the mathematics involved in this project, not the coding.

Be prepared that a generic system may not be possible.

Emil Vikström
Well, Firefox seems to have pretty good built-in support for MathML parsing, and IE has a plug-in. I admit I don't know how reliable they are, but the demos seem pretty impressive.
Bill
+3  A: 

One way is to store generic formulas in your database, i.e. the example you gave ax^2 + bx + c. Example of database called formulas:

id_formulas  formula_problem      constants           type         formula_solution
1            ax^2 + bx + c = 0    a{split}b{split}c   polynomial   x = (-1*{b} + ({b}^2 - 4*{a}*{c}) ) / 2*{a} {split} x = (-1*{b} - ({b}^2 - 4*{a}*{c}) ) / 2*{a}
2            y = mx + b           b{split}m{split}y   graph        x = ({b} - {y}) / -1*{m}        
3            etc                  etc                 etc          etc

Then (psuedocode):

  1. app retrieves a random formula_problem from database (either any type, or of a certain type)
  2. app assigns randomly generated numbers to constants, i.e. "b = 1, m = 2, y = .5"
  3. app swaps constants in formula_solution with numbers from step 2 (inside {} so easy to find)
  4. app solves for x and encrypts answer (in the case of the quadratic, there are 2 answers, Split("{split}") into array)
  5. app displays to web browser and asks student to solve: "y = mx + b" and "b = 1, m = 2, y = .5"
  6. app also puts encrypted answer in a hidden form field on the web page
  7. student solves for x, then types answer into text box, and clicks submit button
  8. app compares student's solution with decrypted hidden solution
  9. app displays to web browser: "correct/incorrect" along with correct answer

This web application can be written in Java/C#.NET/VB.NET/PHP/any web technology. The database can be SQL Server/MySQL/PostgreSQL/XML/etc. The processing can all be done server side in one of the aforementioned languages, or, once the data (formula) is retrieved from the database, the processing could be done in client side JavaScript.

This question is very open ended because there are many approaches that a developer could take, and it comes down to preference. My personal opinion though is that it would be harder to program some of this stuff in client side JavaScript versus server side C# or PHP.

If you know any computer languages already, i.e. C++, then pick a scripting technology that looks similar and start learning by reading books and online tutorials/code examples.

(sorry I'm not a math guy though)

JohnB
I get the sense the consensus is along the lines of inserting the generic formula on the server, and insert the specific data via JavaScript on the client side. Thanks.
Bill
I don't get why you want to do anything in JavaScript here. You can insert specific data just as well using server-side scripting. I think you should stick with one language, otherwise you will get into the troubles of making Js and PHP talk with each other, which seems unnecessary to me.
Emil Vikström
I agree with Emil.
JohnB
Someone made the comment about too much server side scripting taking more time, but perhaps it wouldn't be that much of a difference (not enough, at least, to outweigh the disadvantage you point out). Thanks
Bill
Bill, it takes the same amount of time whether server side or client side, but in some high traffic systems, server CPU might be a limited resource, hence you would want to offload work to the client's PC (i.e. JavaScript) if possible (i.e. no security risks). For this program, processing time would be infinitesimally small so no worries.
JohnB
@JohnB good to know, thanks. And thanks for the further detail in your post.
Bill
@JohnB, you mention XML as a possible database. Is there a reason/advantage to choose XML as opposed to DBMS such as MySQL?
Bill
JohnB
+1  A: 

it shouldn't matter much whether to do it server or client side. I usually prefer to do any sort of processing on the client side since it should be random for each student there is no reason to add processing time of the server for this.

How do you read from the MySQL database? You could easily work with the data returned from that server side script. As to how to go about implementing it I'd suggest a jQuery plugin that should be very easy to use: jQuery xmlObjectifier

The flow is then: Student Excersize page loaded and requests the mathml xml object, after returning it from mysql it dynamically loads the xml, transforms it into a js object (json) and then performs randomization on the object's data. Upon finishing it then loads the product into a container on the student page.

tutuDajuju
A: 

You might want to consider WeBWorK, a homework management system now administered by the MAA. It's designed by and for mathematicians as an online homework system, and it's quite popular. Of course, it's also quite a lot more product than you're asking for.

Josephine