Integrating a function

(Excerpt from "The MathML Handbook" by Pavi Sandhu)

The following example shows a simple MSP (Mathematica Server Pages) script that sets up a Web page for calculating integrals. The page includes a text field and a button. A user can enter a function of one variable in the text field, and then click the "Integrate" button to see the result in the same Web page (see the following figure). The function must be entered using Mathematica's input syntax, which means that all function names must be capitalized and arguments must be enclosed in square brackets, as in Sqrt[x], Sin[x], or Log[x, 2].

Example: An HTML document for calculating the integral of a function using webMathematica.

<html>
  <head><title>Integration</title></head>
  <body bgcolor="white">
    <h1>Integrate a Function</h1>
    <form method="post">
      <p>Enter a function of one variable: <input type="text" name="fn" align="left" size="24" /></p>
      <p><input type="submit" name="inputButton" value="Integrate" /></p>
      <p><% Mathlet MSPBlock[{$$fn},MSPFormat[Integrate[$$fn, x],TraditionalForm]] %></p>
    </form>
  </body>
</html>

Web page for calculating integrals using Mathematica

Figure: A Web page for calculating integrals using Mathematica.

This Web page contains two controls, a text field and a button, each represented by its own input element. The input elements are enclosed in a form element that has the attribute method="post". This attribute determines how the server processes the contents of the form.

The page also contains a single Mathlet tag. This tag contains the following command that will be passed to Mathematica for evaluation when the form is submitted:

MSPBlock[{$$fn},
MSPFormat[Integrate[$$fn, x], TraditionalForm]]

Here $$fn represents the value of the input element with the attribute name="fn". All variables that are to be sent to the Mathematica session must be prefaced with a $$ sign. The MSPBlock function checks the value of its first argument, namely $$fn. If this variable has a valid value — that is, if the user has entered a function in the text box — then the command given in the second argument is executed. Otherwise, MSPBlock returns a null string, which does not lead to any visible output in the Web page. The MSPBlock command is thus a way of ensuring that the evaluation proceeds only when a valid input is available. Otherwise, Mathematica will generate an error message that will be displayed in the Web page.

The second function in the Mathlet tag is MSPFormat, which formats the result obtained after evaluating its first argument and displays it in the Web page. By default, the result is displayed in StandardForm, which is a two-dimensional output format used by Mathematica for displaying mathematical expressions. You can specify a different display format by specifying the format as the second argument of MSPFormat. For example, to display an expression in traditional mathematical notation, you would use the command MSPFormat[expr, TraditionalForm]. See Mathematica syntax for more information about the different display forms in Mathematica.

When the user specifies a function in the text box and clicks the button, the contents of the text box are assigned to the variable $$fn and submitted to the server. Mathematica then evaluates the command Integrate[$$fn, x]. The result of the evaluation replaces the Mathlet tag in the original Web page . The result is displayed in the form of a GIF image that is stored in the browser's cache, and a link to the image is automatically inserted into the Web page. The MSPFormat function automatically handles these steps.

   

<< back next >>

 

 

 


 

Copyright © CHARLES RIVER MEDIA, INC., Massachusetts (USA) 2003
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "The MathML Handbook" is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.


CHARLES RIVER MEDIA, INC., 20 Downer Avenue, Suite 3, Hingham, Massachusetts 02043, United States of America