;; Fu Jie Huang , 8/2002
;; a quick test of the  one-dimensional minimization  
;; example from <GSL reference manual> pp.421
;; minimize function "f(x)=cos(x)+1" within bound [0, 6]
;; result: x=pi

(libload "gsl/const.lsh")
(libload "gsl/aux_globalvar")
(libload "gsl/aux_structure")
(libload "gsl/min")

(de fn1 ( x params)
    ((-double-) x)
    ((-gptr-) params)
    (double  #{   cos($x) + 1.0  #} ))

(dhc-make () fn1)

(setq f  (alloc_gsl_function))
(setval_function_gsl_function f (gptr fn1))
(setval_params_gsl_function f (gptr ()))

(setq ss (gsl_min_fminimizer_alloc (var_gsl_min_fminimizer_brent)))
(gsl_min_fminimizer_set  ss  f   2.0   0.0    6.0)

(setq iter 0)
(do-while (and (= status GSL_CONTINUE) (< iter 100))
  (incr iter)
  (setq status (gsl_min_fminimizer_iterate ss))
  (setq m      (gsl_min_fminimizer_minimum ss))
  (setq a      (gsl_min_fminimizer_x_lower ss))
  (setq b      (gsl_min_fminimizer_x_upper ss))
  (setq status (gsl_min_test_interval    a b   0.001   0.0))
  (when (= status GSL_SUCCESS) (printf "converged: %g\n" m))
)



