You are a guest. Restricted access. Read more

1D functions

Functions with one variable

We will start this tutorial with a simple example of how to create and display a function with one and only one variable (“x”). First, let us create an object representing a function: $f(x)=2*\exp(-x*x/50)+\sin(3.14*x)$. Such function can be constructed using the Java class F1D (1D means one-dimensional function). After we constructed such function, we can evaluate it at any give “x” value:

from jhplot import F1D
f = F1D("2*exp(-x*x/50)+sin(pi*x)/x")
print f.eval(10) # evaluate at x=10

One can also evaluate this function at several different x values:

from jhplot import F1D
f = F1D("2*exp(-x*x/50)+sin(pi*x)/x")
print f.eval([1,2,3,4]) # evaluate at x=11,2,3,4

To draw the function, one should specify limits during the function initialization (or later using the methods of the Java object F1D. Then one should build a canvas and use the method “draw” to plot the function. In the example below, we define the limits for this function (-2,5) and then we plot it:

from jhplot import F1D, HPlot
f = F1D("2*exp(-x*x/50)+sin(pi*x)/x", -2.0, 5.0)
c = HPlot("Canvas")

Integration and differentiation

Functions can be numerically integrated. The program supports 5 methods of integration, which vary in evaluation time and accuracy. Below we will integrate the function “cos(x*x)+sin(1/x)*x^2)”. This code integrates the function using different methods, in x between 10 and 100 using 5000 integration point (the large the number is, the more accurate the results are). The code also performs some benchmarking which gives you ideas about the accuracy of calculations:

from jhplot import F1D
import time
methods=['trapezium','gauss4', 'gauss8',  'richardson', 'simpson']
for m in methods:
   start = time.clock()
   t = time.clock()-start
   print m,' =',d,' time (ms)=',t*1000


Here is a short description about how to define functions. The following table shows the main mathematical operations:

 ()    parenthesis
 +     plus
 -     minus
 *     times
 /     divide
 ^     raise to a power
 **    raise to a power


String Definition
abs absolute value
log(x) Natural Logarithm
sqrt(x) squared root
exp(x) Exp
ceil nearest upper integer
floor nearest lower integer
cbrt cubic root
sin(x) Sine
cos(x) Cosine
tan(x) Tangent
asin(x) ArcSine
acos(x) ArcCosine
atan(x) ArcTangent
sinh(x) Hyperbolic Sine
cosh(x) Hyperbolic Cosine
tanh(x) Hyperbolic Tangent

One can also use some predefined constants, such as pi or Pi (the pi value, 3.14..)

Let us give the examples:

from jhplot  import F1D
f1=F1D("1+x+(x^2)-(x^3)")   # correct answer -1
print f1.eval(2)            
f1=F1D("1+x+(x*x)-(x*x*x)") # correct answer -1
print f1.eval(2)            
f1=F1D("1+x+x^2-x^3")       # correct answer -1
print f1.eval(2)
f1=F1D("1+x+x**2-x**3")     # correct answer -1
print f1.eval(2)

Displaying several functions

Here is a more detailed example showing how to plot several functions using different colors:

from java.awt import Color
from jhplot  import F1D,HPlot

f1 = F1D("2*exp(-x*x/50)+sin(pi*x)/x", -2.0, 5.0)
f1.setPenDash(4); f1.setPenWidth(2)

f2 = F1D("exp(-x*x/30)", -2.0, 5.0)
f2.setColor(; f2.setPenWidth(3)

c1 = HPlot("Canvas")
c1.setGTitle("2 functions",
c1.setNameX("X"); c1.setNameY("Y values")
c1.draw(f1); c1.draw(f2)

Exercise: add a third function $cos(x)*x$ and show it with the red color.

Converting functions to histograms

Histograms can be created from 1D and 2D functions. In the example above, we created a 2D function and then used it to generate 2D histogram. Functions can be converted to histograms with arbitrary number of bins. This often can be used for show selected regions in different color.

Consider the example in which a function is used to create a histogram with fine bins. We use this histogram to highlight a range of our original function.

from java.awt import Color
from jhplot  import HPlot,F1D 

c1 = HPlot("Canvas")
c1.setNameX("X"); c1.setNameY("Y")

f1 = F1D("2*exp(-x*x/50)+sin(pi*x)/x", -2.0, 5.0); c1.draw(f1)

h=f1.getH1D("Histogram",500,1,5) # histogram with 500 bins between 1 and 5 
h.setFill(1); h.setFillColor(

Histograms can have arbitrary number of bins, but if the bin number is close to 500 (a typical evaluation step for functions), the difference between function and a histogram will be almost impossible to see.

scavis/functions/f1d.txt · Last modified: 2013/04/14 16:52 (external edit)
Back to top
CC Attribution-Share Alike 3.0 Unported = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0