Functional programming opens new opportunities for effective plotting. The following example demonstrates that fact.
// Demonstrates functional style plotting in ScalaLab29
// define and plot the corresponding 1-D function
val color = Color.BLUE
val lineplotsFlag = true
def fx(x:Double) = { x -sin(x)*x }
fplot(fx, 1, 10, color, lineplotsFlag)
def fy(x: Double) = { x-2*cos(x)*x*x }
fplot(fy, 1, 10, Color.GREEN, false, 1000)
// define as String and plot the corresponding 1-D function
splot("sin(x)", 1, 5, color, lineplotsFlag)
// plot with VISAD
vsplot("sin(x)", 1, 5)
// plot with JFreeChart
jsplot("sin(x)", 1, 5)
// define and plot the corresponding 2-D function
def fxy(x:Double, y: Double) = { x*sin(x)+x*y*cos(y) }
figure3d; fplot2d(fxy, 1, 10, 1,5)
// define directly as a String and plot the corresponding 1-D function
figure3d; splot2d("x*y*cos(x*y)", -4, 4, -10, 10)
Generally, we can improve the plot of a function significantly by adjusting the sampling density according to the rate of function change. The
_faplot()
method is a first attempt towards adaptive functional plotting. We illustrate it by means of an example:
_
// Example: the function sin(x*x) changes generally more rapidly as x increases,
// however, as can be seen from its derivative x*x*cos(x*x),
// the rate of change oscilates also with increasing frequency as x increases
// Example: the function sin(x*x) changes generally more rapidly as x increases,
// however, as can be seen from its derivative x*x*cos(x*x),
// the rate of change oscilates also with increasing frequency as x increases
def f(x: Double) = sin(x*x)
closeAll
var Npoints = 200
figure(1)
subplot(2, 1, 1)
fplot(f, 0, 10, nP = Npoints )
xlabel("Fixed sampled x")
subplot(2, 1, 2)
var (ax, ay) = faplot(f, 0, 10, nP = Npoints)
xlabel("Adaptively sampled x")