> On Mar 2, 2016, at 10:29 AM, Jens Axel Søgaard <jensa...@soegaard.net> wrote:
> 
> The root finder is the right tool.
> 
> Add this below your program:

Excellent. That makes a lot of sense. Given the setup, I think  h = 0.0001 is 
probably plenty small. It didn’t occur to me that I could differentiate 
numerically, rather than analytically. Thanks!

John

> 
> (require math/flonum)
> 
> (define (df/dx x)
>   (define f badness)
>   (define h 0.0001) 
>   (define d (/ (- (f (+ x h)) (f (- x h)))
>                (* 2.0 h)))
>   (if (zero? d)
>       +inf.0
>       d))
> 
> (plot (list (function df/dx)
>             (function (λ (x) 0.0)))
>       #:width 1300
>       #:x-min 1
>       #:x-max 1200000
>       #:y-min -5e-8)
> 
>  
> (flbracketed-root df/dx 5.0e5  6e5)
> (flbracketed-root df/dx 7.5e5 10e5)
> 
> 
> The results are: 
>     514285.7058330256
>     817605.5857361854
> 
> I don't know what the "proper" value of h ought to be
> and how much it affects the results. (A few experiments
> show that the integer part ought to trustworthy).
> 
> Maybe you can combine the approach with golden section
> search to hone in on a more precise value?
> 
> /Jens Axel
> 
> 
> 
> 
> 
> 
> 2016-03-02 18:36 GMT+01:00 'John Clements' via Racket Users 
> <racket-users@googlegroups.com>:
> 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!
> 
> <Screen Shot 2016-03-02 at 9.34.28 AM.png>
> 
> #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.
> 
> 
> 
> -- 
> -- 
> Jens Axel Søgaard
> 



-- 
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