gsl-shell.git - gsl-shell

index : gsl-shell.git
gsl-shell
summary refs log tree commit diff
path: root/demos
diff options
context:
space:
mode:
Diffstat (limited to 'demos')
-rw-r--r--demos/contour.lua 57
-rw-r--r--demos/fractals.lua 12
-rw-r--r--demos/linfit.lua 26
3 files changed, 77 insertions, 18 deletions
diff --git a/demos/contour.lua b/demos/contour.lua
new file mode 100644
index 00000000..24dc5162
--- /dev/null
+++ b/demos/contour.lua
@@ -0,0 +1,57 @@
+use 'math'
+
+local function rosenbrock()
+ -- rosenbrock function
+ local f = function(x, y) return 100*(y-x^2)^2 + (1-x)^2 end
+ local N = 7
+ local function frbeval(k) return f(1, 1 - 2 * (k/N)^2) end
+ local ls = iter.ilist(frbeval, N)
+ return contour.plot(f, -1.5, -0.5, 1.5, 2, {gridx= 80, gridy= 80, ls= levels})
+end
+
+local function sincos()
+ local fsincos = function(sx,sy)
+ return function(x,y)
+ return cos(x)+cos(y) + sx*x + sy*y
+ end
+ end
+
+ local function add_box_title(p, x1, x2, y1, y2, title)
+ local box = graph.rect(x1, y1, x2, y2)
+ p:addline(box, 'black')
+ p.units = false
+ p.title = title
+ end
+
+ local f = fsincos(0.1, 0.3)
+ local p1 = contour.plot(f, 0, 0, 4*pi, 4*pi, {gridx=60, gridy=60})
+ add_box_title(p1, 0, 4*pi, 0, 4*pi, 'f(x,y) = cos(x) + cos(y) + 0.1x + 0.3y')
+
+ return p1
+end
+
+local function xypolar()
+ local N, R, zmax = 5, 1.2, 1.2
+ local ls = iter.ilist(|k| zmax * (k-N-1)/N, 2*N+1)
+ local p = contour.polar_plot(|x,y| y^2 - x^2*(x+1), R, {levels= ls})
+ p.title = 'f(x,y) = y^2 - x^2*(x+1)'
+ return p
+end
+
+return {'Contour Plots', {
+ {
+ name = 'contour1',
+ f = rosenbrock,
+ description = 'Contour plot of Rosenbrock function',
+ },
+ {
+ name = 'contour2',
+ f = sincos,
+ description = 'Another contour plot example',
+ },
+ {
+ name = 'contour3',
+ f = xypolar,
+ description = 'Polar plot example',
+ },
+}}
diff --git a/demos/fractals.lua b/demos/fractals.lua
index a5828316..84ceb3c1 100644
--- a/demos/fractals.lua
+++ b/demos/fractals.lua
@@ -1,16 +1,19 @@
-
-use 'stdlib'
+use 'math'
+use 'graph'
+use 'iter'
local function c_generator(n, n_angle, len_frac, g)
local exp, real, imag = complex.exp, complex.real, complex.imag
local w, r, k = ilist(|| 0, n+1), #g
local s = len_frac^n
- local sz = cnew(n_angle, 1, |k| s * exp(2i*pi*(k-1)/n_angle))
+ local sz = matrix.cnew(n_angle, 1, |k| s * exp(2i*pi*(k-1)/n_angle))
- local sh = ilist(|k| g[k%r+1] - g[(k-1)%r+1], 0, r-1)
+ local sh = ilist(|k| g[(k-1)%r+1] - g[(k-2)%r+1], r)
local a = (g[1]*n) % n_angle
+ print('sh', sh)
+
local z = 0
return function()
if w[n+1] == 0 then
@@ -18,6 +21,7 @@ local function c_generator(n, n_angle, len_frac, g)
z = z + sz[a+1]
for j=1,n+1 do
w[j] = (w[j] + 1) % r
+ print(j, w[j], sh[w[j]+1], a)
a = (a + sh[w[j]+1]) % n_angle
if w[j] ~= 0 then
break
diff --git a/demos/linfit.lua b/demos/linfit.lua
index 24d658b9..7fdfde07 100644
--- a/demos/linfit.lua
+++ b/demos/linfit.lua
@@ -11,15 +11,13 @@ local function demo1()
local x = matrix.new(n, 1, xsmp)
local y = matrix.new(n, 1, |i| a*xsmp(i) + b + rnd.gaussian(r, 0.4))
- X = matrix.new(n, 2, |i,j| j==1 and 1 or xsmp(i))
+ local X = matrix.new(n, 2, |i,j| j==1 and 1 or xsmp(i))
- c, chisq, cov = linfit(X, y)
-
- print('Linear fit coefficients: ')
+ local c, chisq, cov = linfit(X, y)
local fit = function(x) return c[1]+c[2]*x end
- p = graph.fxplot(fit, x0, x1)
+ local p = graph.fxplot(fit, x0, x1)
p:add(graph.xyline(x, y), 'blue', {{'stroke'}, {'marker', size=5}})
p.title = 'Linear Fit'
p.clip = false
@@ -37,23 +35,23 @@ local function demo2()
local xnorm = |x| (2*x - x0 - x1) / (x1-x0)
- model = function(k, x) return sf.legendreP(k, xnorm(x)) end
+ local model = function(k, x) return sf.legendreP(k, xnorm(x)) end
- legmodel_order = 18
+ local legmodel_order = 18
- X = matrix.new(n, legmodel_order+1, |i,j| model(j-1, xsmp(i)))
+ local X = matrix.new(n, legmodel_order+1, |i,j| model(j-1, xsmp(i)))
- c, chisq = linfit(X, y)
+ local c, chisq = linfit(X, y)
- pc = graph.fibars(|i| c[i], 1, #c)
+ local pc = graph.fibars(|i| c[i], 1, #c)
pc.title = 'Legendre polynomials fit coefficients'
pc.pad = true
- fitleg = function(x)
- return isum(|k| c[k+1] * model(k, x), 0, legmodel_order)
- end
+ local fitleg = function(x)
+ return isum(|k| c[k+1] * model(k, x), 0, legmodel_order)
+ end
- p = graph.fxplot(fitleg, x0, x1)
+ local p = graph.fxplot(fitleg, x0, x1)
p:addline(graph.xyline(x, y), 'blue', {{'marker', size=5}})
p.title = 'Legendre Polinomial fit of Bessel J3(x)'
p.clip = false
generated by cgit v1.2.3 (git 2.25.1) at 2025年10月05日 07:59:07 +0000

AltStyle によって変換されたページ (->オリジナル) /