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: . 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:
One can also evaluate this function at several different x values:
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:
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:
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
|ceil||nearest upper integer|
|floor||nearest lower integer|
One can also use some predefined constants, such as pi or Pi (the pi value, 3.14..)
Let us give the examples:
Here is a more detailed example showing how to plot several functions using different colors:
Exercise: add a third function $cos(x)*x$ and show it with the red color.
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.
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.