I’m trying to minimize a function. It’s a continuous function made up of 
piecewise well-behaved functions of the form (k_0 / x) +( k_1 / x^2). It’s not 
hard to solve these analytically, but since they’re piecewise functions each 
with different coefficients, I figured I’d ask first: does the math package 
have a built-in function minimization operator? I looked around a bit and found 
the root-finder, but not a minimizer.

Well, what the heck; racket is compact, I might as well include some sample 
code and a picture.

Any help appreciated!



#lang racket

(require math/statistics)

;; this file tries to determine the "best-fit" l for each user.

;; the model in this case is a one-parameter model, defining this
;; simplified learning function:

;; f(t) = (1 - t/l) when 0 < t < l
;; f(t) = 0         when t >= l

(require plot)

(define example-sequence
  '(#(0 1)
    #(400000 2/9)
    #(700000 3/9)
    #(800000 0)
    #(1000000 0)))

(plot (list (points example-sequence)
            (lines example-sequence))
      #:x-max 1200000
      #:width 1300)

;; this fitness function measures the mean squared distance
;; from the datapoints to Model_l.
;; I want to find the value of l that minimizes this function
(define (badness l)
  (mean 
   (for/list ([pt (in-list example-sequence)])
     (match-define (vector t e) pt)
     (expt (cond [(< t l) (- e (- 1 (/ t l)))]
                 [else e])
           2))))

(plot (function badness)
      #:width 1300
      #:x-min 1
      #:x-max 1200000
      #:y-min 0)

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to