Plotting a function

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

The following example shows an MSP for plotting functions. A user can enter a function along with a range, and then click a button to see a plot of the function over the specified range (see the following figure).

Example: An HTML document for plotting a function using webMathematica.

  <body bgcolor="white" >
    <h1>Plot a Function</h1>
    <form method="post">
      <p>Enter a function of one variable: <input type="text" name="f"  size="20" value="<%Mathlet MSPValue[ $$f, "Sin[x^2]"] %>" /> </p>
      <p>Enter the start of the range: <input type="text" name="x1" size="10" value="<%Mathlet MSPValue[ $$x1, "0"] %>" /> </p>
      <p> Enter the end of the range: <input type="text" name="x2" size="10" value="<%Mathlet MSPValue[ $$x2, "5"] %>" /> </p>
      <p><input type="submit" name="inputButton" value="Plot" /> </p>
      <%Mathlet MSPBlock[ {$$f, $$x1, $$x2},MSPShow[ Plot[$$f, {x,$$x1,$$x2}]]] %>

Web page for plotting functions using Mathematica

Figure: A Web page for plotting functions using Mathematica.

This page is a straightforward generalization of the page shown in the example An HTML document for calculating the integral of a function using webMathematica. It contains a form element that contains four different controls: three text boxes and one button. Notice that each text box has its value attribute set to a Mathlet tag that contains the command MSPValue[$$name,value]. This command displays the variable contained in the first argument if that variable has been assigned a value. Otherwise, it displays the second argument. Specifying a second argument is thus a way to include a default value in the text box when the Web page is first displayed. When the page is first loaded, a default value of the function as well as the starting and ending values of the range are displayed in the corresponding text boxes.

The page also contains a fourth Mathlet tag, which encloses the command for generating the plot, as shown here:

MSPBlock[{$$f, $$x1, $$x2},
MSPShow[Plot[$$f, {x, $$x1, $$x2}]

The MSPBlock command, as in the example An HTML document for calculating the integral of a function using webMathematica, first checks to see that all three variables ($$f, $$x1, and $$x2) have valid values. Then, it executes its second argument, Plot[f, {x,a,b}]. This is the Mathematica command for plotting a function of x in the range from a and b. The MSPShow command saves an image of the resulting graphic on the server and returns a link to that image for insertion into the Web page.


<< 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