#
# $Id: bivariat.demo,v 3.26 92/03/24 22:32:38 woo Exp Locker: woo $
#
#
# This demo is very slow and requires unusually large stack size.
# Do not attempt to run this demo under MSDOS.
#

# the function integral_f(x) approximates the integral of f(x) from 0 to x.
# integral2_f(x,y) approximates the integral from x to y.
# define f(x) to be any single variable function
#
# the integral is calculated as the sum of f(x_n)*delta 
#   do this x/delta times (from x down to 0)
#
f(x) = exp(-x**2)
delta = 0.025
#
# integral_f(x) takes one variable, the upper limit.  0 is the lower limit.
# calculate the integral of function f(t) from 0 to x
integral_f(x) = (x>0)?integral1a(x):-integral1b(x)
integral1a(x) = (x<=0)?0:(integral1a(x-delta)+delta*f(x))
integral1b(x) = (x>=0)?0:(integral1b(x+delta)+delta*f(x))
#
# integral2_f(x,y) takes two variables; x is the lower limit, and y the upper.
# claculate the integral of function f(t) from x to y
integral2_f(x,y) = (x<y)?integral2(x,y):-integral2(y,x)
integral2(x,y) = (x>y)?0:(integral2(x+delta,y)+delta*f(x))

set title "approximate the integral of functions"
set samples 50

plot [-5:5] f(x) title "f(x)=exp(-x**2)", 2/sqrt(pi)*integral_f(x) title "erf(x)=2/sqrt(pi)*integral_f(x)"

pause -1 "Hit return to continue"

f(x)=sin(x)

plot [-5:5] f(x) title "f(x)=sin(x)", integral_f(x)

pause -1 "Hit return to continue"

set title "approximate the integral of functions (upper and lower limits)"

f(x)=(x-2)**2-20

plot [-10:10] f(x) title "f(x)=(x-2)**2-20", integral2_f(-5,x)

pause -1 "Hit return to continue"

f(x)=sin(x-1)-.75*sin(2*x-1)+(x**2)/8-5

plot  [-10:10] f(x) title "f(x)=sin(x-1)-0.75*sin(2*x-1)+(x**2)/8-5", integral2_f(x,1)

pause -1 "Hit return to continue"

#
# This definition computes the ackermann. Do not attempt to compute its
# values for non integral values. In addition, do not attempt to compute
# its beyond m = 3, unless you want to wait really long time.

ack(m,n) = (m == 0) ? n + 1 : (n == 0) ? ack(m-1,1) : ack(m-1,ack(m,n-1))

set xrange [0:3]
set yrange [0:3]

set isosamples 4
set samples 4

set title "Plot of the ackermann function"

splot ack(x, y)

pause -1 "Hit return to continue"

set xrange [-5:5]
set yrange [-10:10]
set isosamples 10
set samples 100
set key 4,-3
set title "Min(x,y) and Max(x,y)"

#
min(x,y) = (x < y) ? x : y
max(x,y) = (x > y) ? x : y

plot sin(x), x**2, x**3, max(sin(x), min(x**2, x**3))+0.5

pause -1 "Hit return to continue"

#
# gcd(x,y) finds the greatest common divisor of x and y,
#          using Euclid's algorithm
# as this is defined only for integers, first round to the nearest integer
gcd(x,y) = gcd1(rnd(max(x,y)),rnd(min(x,y)))
gcd1(x,y) = (y == 0) ? x : gcd1(y, x - x/y * y)
rnd(x) = int(x+0.5)

set samples 59
set xrange [1:59]
set auto
set key

set title "Greatest Common Divisor (for integers only)"

plot gcd(x, 60)
pause -1 "Hit return to continue"

set xrange [-10:10]
set yrange [-10:10]
set auto
set isosamples 10
set samples 100
set title ""

