---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
# solution(F,k, a,b) finds x between a and b such that F(x)=k (if F varies continuously
# between A and B, and the sign of F(x)-k changes) automates a procedure similar to the
# following: we repeatedly bisects the interval defined and then selects the subinterval
# in which the function changes sign: (a,b) → (a',b') → (a",b") → …
                       
# Similarly, with a trisection, the "minmax" command works.
#
# The graph of a function x -> f(x) and finding where it has a "bump"
f = function(x) x^3-x
graphF(f,-3,3, "blue")
                      
# I want to look for x such that around it  x^3 - x  has a minimum value
graphF(f,-2,2, "blue")
                      
# Between 0 and 1 the graph has a hump.
# I define a procedure that trisects the interval and selects the two subintervals
# in which the graph has a hump.
       
# In case (1) I take the two subintervals to the right, in case (2) those to the left
#
Min = function(f,a,b) {h=(b-a)/3;y1=f(a);y2=f(a+h);y3=f(a+2*h);if(y1>y2 & y2>y3) a<<-a+h else b<<- b-h}
#
a=0 ;b=1
Min(f,a,b); c(a,b)
#  0.3333333  1.0000000
Min(f,a,b); c(a,b)
#  0.3333333  0.7777778
Min(f,a,b); c(a,b)
#  0.4814815  0.7777778
Min(f,a,b); c(a,b)
#  0.4814815  0.6790123
#...
Min(f,a,b); c(a,b)
#  0.5773502 0.5773503
Min(f,a,b); c(a,b)
#  0.5773503  0.5773503
#
# The procedure "mimmax" automates this process.