-rw-r--r-- | draw.lua | 4 | ||||
-rw-r--r-- | examples/fft.lua | 5 | ||||
-rw-r--r-- | examples/hermite.lua | 59 | ||||
-rw-r--r-- | examples/linfit.lua | 132 | ||||
-rw-r--r-- | examples/ode.lua | 20 |
@@ -84,8 +84,8 @@ function segment(x1, y1, x2, y2) return p end -local bcolors = {'red', 'green', 'blue', 'cyan', 'magenta', 'yellow'} -local mcolors = {'dark', '', 'light'} +local bcolors = {'red', 'blue', 'green', 'magenta', 'cyan', 'yellow'} +local mcolors = {'', 'dark', 'light'} function rainbow(n) local p = #bcolors diff --git a/examples/fft.lua b/examples/fft.lua index d761aece..9705efed 100644 --- a/examples/fft.lua +++ b/examples/fft.lua @@ -63,8 +63,11 @@ function demo2() for k=ncut, n/2 do bess:set(k,0) end fft_inv(bess) - p:addline(filine(|i| bess[i], n), 'red', {{'dash', a=7, b=3}}) + p:addline(filine(|i| bess[i], n), 'red', {{'dash', 7, 3}}) p:show() return p, fftplot end + +print 'demo1() - GSL example with square function and frequency cutoff' +print 'demo2() - frequency cutoff example on bessel function' diff --git a/examples/hermite.lua b/examples/hermite.lua index 99a6754a..b5cb25f1 100644 --- a/examples/hermite.lua +++ b/examples/hermite.lua @@ -1,31 +1,28 @@ -
-function hermiteLp(n,x)
- return 1/sqrt(fact(n) *2^n*sqrt(pi)) * exp(-x*x/2) * (-4)^(n/2) * fact(n/2) * laguerre(n/2, -1/2, x^2)
-end
-
-p = plot()
-p:addline(fxline(|x| hermiteLp(4, x), -12, 12), 'darkgreen')
-p:addline(fxline(|x| hermiteLp(2, x), -12, 12), 'blue')
-p:addline(fxline(|x| hermiteLp(50, x), -12, 12, 1024), 'magenta')
-p:show()
-
-function hermiteU(n,x)
- return 1/sqrt(fact(n) *2^n*sqrt(pi)) * exp(-x*x/2) * 2^n * hypergU((1-n)/2, 3/2, x^2)
-end
-
-function hermiteFp(n,x)
- return 1/sqrt(fact(n) *2^n*sqrt(pi)) * exp(-x*x/2) * (-1)^(n/2) * fact(n)/fact(n/2) * hyperg1F1(-n/2, 1/2, x^2)
-end
-
-
-p2 = plot()
-p2:addline(fxline(|x| hermiteFp(4, x), -12, 12), 'darkgreen')
-p2:addline(fxline(|x| hermiteFp(2, x), -12, 12), 'blue')
-p2:addline(fxline(|x| hermiteFp(50, x), -12, 12, 1024), 'magenta')
-p:show()
-
-
--- p3 = plot()
--- p3:add_line(fxline(|x| hermiteU(4, x), -12, 12), 'darkgreen')
--- p3:add_line(fxline(|x| hermiteU(2, x), -12, 12), 'blue')
--- p3:add_line(fxline(|x| hermiteU(50, x), -12, 12, 1024), 'magenta')
+ +local function hermiteLp(n,x) + return 1/sqrt(fact(n) *2^n*sqrt(pi)) * exp(-x*x/2) * (-4)^(n/2) * fact(n/2) * laguerre(n/2, -1/2, x^2) +end + +local function hermiteU(n,x) + return 1/sqrt(fact(n) *2^n*sqrt(pi)) * exp(-x*x/2) * 2^n * hypergU((1-n)/2, 3/2, x^2) +end + +local function hermiteFp(n,x) + return 1/sqrt(fact(n) *2^n*sqrt(pi)) * exp(-x*x/2) * (-1)^(n/2) * fact(n)/fact(n/2) * hyperg1F1(-n/2, 1/2, x^2) +end + +local function demo_gen(hermiteFF) + local p = plot('Hermite functions') + p:addline(fxline(|x| hermiteFF(2, x), -10, 10), 'red') + p:addline(fxline(|x| hermiteFF(4, x), -10, 10), 'blue', {{'dash', 7, 3}}) + p:addline(fxline(|x| hermiteFF(16, x), -10, 10, 512), 'green') + p:show() +end + +demo1 = || demo_gen(hermiteLp) +demo2 = || demo_gen(hermiteFp) +demo3 = || demo_gen(hermiteU) + +print 'demo1() - hermite function using Laguerre polynomials' +print 'demo2() - hermite function using Hypergeometric 1F1 function' +print 'demo3() - hermite function using Hypergeometric U function (broken)' diff --git a/examples/linfit.lua b/examples/linfit.lua index 47cce244..300963ed 100644 --- a/examples/linfit.lua +++ b/examples/linfit.lua @@ -1,64 +1,68 @@ -
-function demo1()
- local x0, x1, n = 0, 12.5, 32
- local a, b = 0.55, -2.4
- local xsmp = |i| (i-1)/(n-1) * x1
-
- local r = rng()
- local x = new(n, 1, xsmp)
- local y = new(n, 1, |i| a*xsmp(i) + b + rnd.gaussian(r, 0.4))
-
- fit, c = linfit(|x| {1, x}, x, y)
-
- print('Linear fit coefficients: ', tr(c))
-
- p = fxplot(fit, x0, x1)
- p:addline(xyline(x, y), 'blue', {{'marker', size=6}})
- p.title = 'Linear Fit'
-
- return p
-end
-
-function demo2()
- -- warning: this example may twist your brain and give headache :-)
- local order, x0, x1, n = 3, 0.0, 24.0, 64
- local bess = |x| besselJ(order, x)
- local xsmp = |i| (i-1)/(n-1) * x1
-
- local x = new(n, 1, xsmp)
- local y = new(n, 1, |i| besselJ(order, xsmp(i)))
-
- local xn = |x| (2*x - x0 - x1) / (x1-x0)
- local legmodel = |n| |x| ilist(|i| i == 0 and 1 or legendreP(i, xn(x)), 0, n)
-
- fitleg = linfit(legmodel(14), x, y)
- p = fxplot(fitleg, x0, x1)
- p:addline(xyline(x, y), 'darkgreen', {{'marker', size=6}})
- p.title = 'Legendre Polinomial fit of Bessel J3(x)'
-
- return p
-end
-
-function demo3()
- -- the same as before done slightly differently
- local order, x0, x1, n = 3, 0.0, 24.0, 64
- local bess = |x| besselJ(order, x)
- local xsmp = |i| (i-1)/(n-1) * x1
-
- local x = new(n, 1, xsmp)
- local y = new(n, 1, |i| besselJ(order, xsmp(i)))
-
- local xn = |x| (2*x - x0 - x1) / (x1-x0)
-
- local kfit = 14
- local legmodel = |j, x| j == 0 and 1 or legendreP(j, xn(x))
- local X = new(n, kfit+1, |i,j| legmodel(j-1, x[i]))
- local c = mlinear(X, y)
- local yfit = mul(X, c)
- p = plot('Legendre Polinomial fit of Bessel J3(x)')
- p:addline(xyline(x, yfit))
- p:addline(xyline(x, y), 'darkgreen', {{'marker', size=6}})
- p:show()
-
- return p
-end
+ +function demo1() + local x0, x1, n = 0, 12.5, 32 + local a, b = 0.55, -2.4 + local xsmp = |i| (i-1)/(n-1) * x1 + + local r = rng() + local x = new(n, 1, xsmp) + local y = new(n, 1, |i| a*xsmp(i) + b + rnd.gaussian(r, 0.4)) + + fit, c = linfit(|x| {1, x}, x, y) + + print('Linear fit coefficients: ', tr(c)) + + p = fxplot(fit, x0, x1) + p:add(xyline(x, y), 'blue', {{'stroke'}, {'marker', size=5}}) + p.title = 'Linear Fit' + + return p +end + +function demo2() + -- warning: this example may twist your brain and give headache :-) + local order, x0, x1, n = 3, 0.0, 24.0, 64 + local bess = |x| besselJ(order, x) + local xsmp = |i| (i-1)/(n-1) * x1 + + local x = new(n, 1, xsmp) + local y = new(n, 1, |i| besselJ(order, xsmp(i))) + + local xn = |x| (2*x - x0 - x1) / (x1-x0) + local legmodel = |n| |x| ilist(|i| i == 0 and 1 or legendreP(i, xn(x)), 0, n) + + fitleg = linfit(legmodel(14), x, y) + p = fxplot(fitleg, x0, x1) + p:addline(xyline(x, y), 'blue', {{'marker', size=5}}) + p.title = 'Legendre Polinomial fit of Bessel J3(x)' + + return p +end + +function demo3() + -- the same as before done slightly differently + local order, x0, x1, n = 3, 0.0, 24.0, 64 + local bess = |x| besselJ(order, x) + local xsmp = |i| (i-1)/(n-1) * x1 + + local x = new(n, 1, xsmp) + local y = new(n, 1, |i| besselJ(order, xsmp(i))) + + local xn = |x| (2*x - x0 - x1) / (x1-x0) + + local kfit = 14 + local legmodel = |j, x| j == 0 and 1 or legendreP(j, xn(x)) + local X = new(n, kfit+1, |i,j| legmodel(j-1, x[i])) + local c = mlinear(X, y) + local yfit = mul(X, c) + p = plot('Legendre Polinomial fit of Bessel J3(x)') + p:addline(xyline(x, yfit)) + p:addline(xyline(x, y), 'blue', {{'marker', size=5}}) + p:show() + + return p +end + +print 'demo1() - examples of linear regression of (x, y) data' +print 'demo2() - examples of linear regression based on legendre polynomials' +print 'demo3() - same of demo2 with slightly different procedure' diff --git a/examples/ode.lua b/examples/ode.lua index b77a24f2..7b7a5efd 100644 --- a/examples/ode.lua +++ b/examples/ode.lua @@ -40,7 +40,7 @@ function demo1() return p end -function demo1bis() +function demo2() local odef = function(t, y, f) f:set(1,1, -y[2]-y[1]^2) f:set(2,1, 2*y[1] - y[2]^3) @@ -87,7 +87,7 @@ function ode_lines(s, t0, y0, t1, tstep) return p end -function demo2() +function demo3() local mu = 10 local odef = function(t, y, f) @@ -104,7 +104,7 @@ function demo2() return plot_lines(ln) end -function demo3() +function demo4() local mu = 10 local odef = function(t,y,f) @@ -129,7 +129,7 @@ function demo3() return plot_lines(ln) end -function demo4() +function demo5() local t0, t1, tstep = 0, 30, 0.05 local alpha = 1i - 0.08 local z0 = 1.0 + 0.0i @@ -151,7 +151,7 @@ function demo4() return p end -function demo4bis() +function demo6() local t0, t1, tstep = 0, 30, 0.05 local alpha = 1i - 0.08 local z0 = 1.0 + 0.0i @@ -184,7 +184,7 @@ function demo4bis() return p end -function demo5() +function demo7() local odef = function(t, y, f) f:set(1,1, y[2]) f:set(2,1, -sin(y[1])*y[1]) @@ -198,3 +198,11 @@ function demo5() local ln = ode_lines(s, t0, y0, t1, tstep) return plot_lines(ln) end + +print 'demo1() - ODE integration example' +print 'demo2() - the same of demo1 but using GSL \'bsimp\' method' +print 'demo3() - GSL example of Var der Pol oscillator integration' +print 'demo4() - same as demo3 but using \'bsimp\' method' +print 'demo5() - spriral obtained by ODE integration' +print 'demo6() - spriral obtained by ODE integration using \'bsimp\' method' +print 'demo7() - another ODE integration example' |