Plotting Equations

At this time, qdex does not allow you to simply input an equation and plot it automatically. To plot any equation, you must create a for-loop inside of a script that calculates the y value of said equation, then adds (x,y) points to a buffer on each iteration. When the for-loop is finished, the Update method is called to add the specified points to the plot. 

Example 1

This example plots the equation 4x^3 - x + 9 over the range -100 to 100.

You can copy this example into your document as-is.

<xyPlot name="myPlot">
  <series name="aSeries" manual="true"/>
</xyPlot>

<script>
  local x, y;
  local plotPointer = myPlot.aSeries;

  for x = -100, 100, 1 do
  y = 4*math.pow(x,3) - x + 9
  plotPointer:Add(x,y)
  end
  plotPointer:Update()
</script>
plot-equation.jpg

Example 2

This example plots the equation 2sin(x) over the range -10 to 10. A smaller increments of 0.1 is used in the for-loop to ensure that the wave is plotted smoothly. The lineSegments draw mode is used to draw a dotted line. 

You can copy this example into your document as-is.

<xyPlot name="newPlot">
  <series name="mySeries" draw="lineSegments" manual="true" />
</xyPlot>

<script>
  local x, y;
  local sinePlot = newPlot.mySeries;

  for x = -10, 10, 0.1 do
  y = 2*math.sin(x)
  sinePlot:Add(x,y)
  end
  sinePlot:Update()
</script>
plot-sine.jpg

Example 3

It is possible to add points to a plot without explicitly defining the series in that plot. 

This example plots three equations over the range of -10 to 10. Notice that the three series are not defined by name in the plot declaration, and instead defined as a plot attribute.

You can copy this example into your document as-is.

<xyPlot name="thisPlot" series="3" />

<script>
  for x = -10, 10, 0.1 do
    y1 = 2*math.pow(x,2)
    y2 = 0.5*math.pow(x,3)
    y3 = 0.25*math.pow(x,4)
    thisPlot.Series[1]:Add(x,y1)
    thisPlot.Series[2]:Add(x,y2)
    thisPlot.Series[3]:Add(x,y3)
  end   
</script>
plot-multiple-equations.jpg