gsl-shell.git - gsl-shell

index : gsl-shell.git
gsl-shell
summary refs log tree commit diff
path: root/examples/linfit.lua
diff options
context:
space:
mode:
authorfrancesco-ST <francesco.abbate@st.com>2010年08月04日 16:50:59 +0200
committerfrancesco-ST <francesco.abbate@st.com>2010年08月04日 16:50:59 +0200
commit18d74ee6b5a575f285707e8528056f366de93d0d (patch)
tree88ec2fa560b587f5eba87aa8e9e1eeb635ff4cc4 /examples/linfit.lua
parent5f73662ea331c0ae5a2e3710dce62cc31df1d302 (diff)
downloadgsl-shell-18d74ee6b5a575f285707e8528056f366de93d0d.tar.gz
examples improvements
Diffstat (limited to 'examples/linfit.lua')
-rw-r--r--examples/linfit.lua 132
1 files changed, 68 insertions, 64 deletions
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'
generated by cgit v1.2.3 (git 2.25.1) at 2025年10月01日 06:27:24 +0000

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