/usr/share/doc/ruby-gsl/examples/fit/multifit.rb is in ruby-gsl 2.1.0.3+dfsg1-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | #!/usr/bin/env ruby
require("gsl")
GSL::Rng.env_setup()
r = GSL::Rng.alloc(GSL::Rng::DEFAULT)
n = 19
dim = 3
X = GSL::Matrix.alloc(n, dim)
y = GSL::Vector.alloc(n)
w = GSL::Vector.alloc(n)
file0 = "data.dat"
file1 = "fit.dat"
a = 0.1
File.open(file0, "w") do |f|
for i in 0...n
y0 = Math::exp(a)
sigma = 0.1*y0
val = r.gaussian(sigma)
X[i,0] = 1.0
X[i,1] = a
X[i,2] = a*a
y[i] = y0 + val
w[i] = 1.0/(sigma*sigma)
f.printf("%g %g %g\n", a, y[i], sigma)
a += 0.1
end
end
c, cov, chisq, status = GSL::MultiFit.wlinear(X, w, y)
printf("# best fit: Y = %g + %g X + %g X^2\n", c[0], c[1], c[2])
printf("# covariance matrix:\n")
printf("[ %+.5e, %+.5e, %+.5e\n", cov[0,0], cov[0,1], cov[0,2])
printf(" %+.5e, %+.5e, %+.5e\n", cov[1,0], cov[1,1], cov[1,2])
printf(" %+.5e, %+.5e, %+.5e ]\n", cov[2,0], cov[2,1], cov[2,2])
printf("# chisq = %g\n", chisq)
str = sprintf("%4.3f", c[0])
if c[1] > 0.0
str += sprintf("+ %4.3f*x", c[1].abs)
else
str += sprintf("- %4.3f*x", c[1].abs)
end
if c[2] > 0.0
str += sprintf("+ %4.3f*x*x", c[2].abs)
else
str += sprintf("- %4.3f*x*x", c[2].abs)
end
func = GSL::Function.alloc { |x, params|
c0 = params[0]; c1 = params[1]; c2 = params[2]
c0 + x*(c1 + x*c2)
}
func.set_params(c)
File.open(file1, "w") do |f|
x = 0
while x < 2
f.printf("%e %e\n", x, func.eval(x))
x += 0.01
end
end
system("graph -T X -C -g 3 -y 0 7 -L '#{str}' -I e -m -1 -S 4 data.dat -I a -m 2 -S 0 fit.dat")
File.delete(file0)
File.delete(file1)
|