-rw-r--r-- | examples/nlinfit.lua | 53 |
diff --git a/examples/nlinfit.lua b/examples/nlinfit.lua index 6e8a0084..83bef8e9 100644 --- a/examples/nlinfit.lua +++ b/examples/nlinfit.lua @@ -83,56 +83,3 @@ function demo2() until status ~= 'continue' print_state (s) end - -function demo2plot() - local x0, x1, n = 0, 1.5, 50 - local P = vector {1.55, -3.1} - local model = - {f = |p, x| p[1] * exp(p[2] * x), - J = function(p, x) - local e = exp(p[2] * x) - return e, x * p[1] * e - end} - local err_g = function(p) - local r = rng() - return || p[1] * 0.03 * rnd.gaussian(r) - end - local err = err_g(P) - local xs = |i| x0 + (i-1) * (x1 - x0) / n - local xy = new(n, 2, |i,j| j == 1 and xs(i) or model.f(P, xs(i))+err()) - local function expf(p, f, J) - for k=1, n do - local x = xs(k) - if f then f:set(k, 1, model.f(p, x) - xy:get(k,2)) end - if J then - local d1, d2 = model.J(p, x) - J:set(k, 1, d1) - J:set(k, 2, d2) - end - end - end - local function print_state(s) - print ("x: ", s.x:row_print()) - print ("chi square: ", prod(s.f, s.f)[1]) - end - s = solver {fdf= expf, n= n, p= 2, x0= vector {3.5, -2.5}} - repeat - print_state (s) - local status = s:iterate() - until status ~= 'continue' - print_state (s) - - local fmget = function(m, i) - local a, b = m:get(i+1,1), m:get(i+1,2) - if a then return i+1, a, b end - end - - pts = igpath(fmget, xy, 0) - ln_result = ipath(sample(|t| model.f(s.x, t), x0, x1, 128)) --- ln_theory = ipath(sample(|t| model.f(P, t), x0, x1, 128)) - p = plot() - p:add(pts, 'black', {{'stroke'}, {'marker', size=5}}) - p:add_line(ln_result, 'red') - p:show() - return p -end |