fmaster@brahms.udel.edu (Fred A Masterson) (03/31/91)
I am seeking an APL function that will maximize a function of one variable, i.e., find the value of x that maximizes f(x). Does anyone have one or know where one can be obtained? Fred Masterson fmaster.udel.edu University of Delaware
cs450a03@uc780.umd.edu (03/31/91)
Fred Masterson writes: >I am seeking an APL function that will maximize a function of >one variable, i.e., find the value of x that maximizes f(x). Gee.. uh... could you maybe give a few more hints about the kind of problem you are talking about? Maybe we should just assume that x is intended to be a floating-point scalar, and that any local maxima of f is a suitable answer? Also, how important is it that the routine be a "function"? Often interactive routines are quite powerful (and potentially instructive). Raul Rockwell
sam@kalessin.jpl.nasa.gov (Sam Sirlin) (04/01/91)
In article <20114@brahms.udel.edu> fmaster@brahms.udel.edu (Fred A Masterson) writes: >I am seeking an APL function that will maximize a function of >one variable, i.e., find the value of x that maximizes f(x). To do this once, a simple plot is pretty powerful. To automate this, you need to decide a bunch of things such as - is the function differentiable (analytically or numerically) to some order? - is there a single maximum (no local maxima)? - do you know a priori bounds on a (finite) region that contains the desired maxima? Given the last condition, a simple binary search works pretty fast and is simple to code (I have one somewhere). Sam Sirlin Jet Propulsion Laboratory sam@kalessin.jpl.nasa.gov
fmaster@chopin.udel.edu (Fred A Masterson) (04/01/91)
In article <31MAR91.00533434@uc780.umd.edu> cs450a03@uc780.umd.edu writes: >Fred Masterson writes: >>I am seeking an APL function that will maximize a function of >>one variable, i.e., find the value of x that maximizes f(x). > >Gee.. uh... could you maybe give a few more hints about the kind of >problem you are talking about? Maybe we should just assume that x is >intended to be a floating-point scalar, and that any local maxima of f >is a suitable answer? > >Also, how important is it that the routine be a "function"? Often >interactive routines are quite powerful (and potentially instructive). > >Raul Rockwell O.K., here are the hints: x is indeed a floating point scalar a local maximum would be just fine an interactive (no returned value) procedure would be fine Fred Masterson fmaster@chopin.udel.edu University of Delaware
cs450a03@uc780.umd.edu (04/02/91)
Fred Masterson writes: >>>I am seeking an APL function that will maximize a function of >>>one variable, i.e., find the value of x that maximizes f(x). >x is indeed a floating point scalar >a local maximum would be just fine >an interactive (no returned value) procedure would be fine Well, like the guy said, a couple postings back, plotting a graph works wonders. Other than that, it looks like some variant on Newton's method should work fine... Lets say I assume that f is a scalar function. (so I can give it a list of arguments and it will give me back a corresponding list of results). I'll also going to freely mix J and APL puns... _ V x =. max_x seed [1] 'seed contains the lower bound and upper bound for the interval' [2] 'you wish to test. If you like, just use i. 0' [3] n =. 30 [4] lower_bound =. <./ seed [5] upper_bound =. >./ seed [6] loop) [7] arg =. lower_bound + (upper_bound-lower_bound) * (i. n+1) % n [8] result =. f arg [9] k =. result i. >./result [10] seed =. arg {~ 1>. n<. _1 0 1 + k [11] lower_bound =. <./ seed [12] upper_bound =. >./ seed [13] $. =. ((lower_bound = upper_bound) # loop), end [14] end) [15] _ (+/seed) % 3 V Now, I just threw this thing together, totally untested and so on. But this is such a sleepy group anyways... Tradeoffs: I am guessing that "f" isn't going to be incredibly expensive, if it is, you'll want to do something a lot more concerned about the number of times f gets evaluated. The loop structure is so I could come up with something that looks almost decent in either J or APL. I don't expect this thing to be very fast, in any event (inverting functions by trial and error is never particularly efficient). Quicky J to APL reference [1] and [2] are comments <. is min (floor) >. is max (ceiling) / is reduce # is compress (in APL they are both / ) =. is assign to local variable * is multiply loop) and end) are line labels $. =. is the equivalent of -> {~ is indexing: list[indices] _1 is negative 1 max_x would be maxHx (maxDELTAx) if I did this in real APL ---------------------------------------------------------------------- I just use built in comparison tolerance to decide when it's done. Also, you could shorten variable names and squash the thing onto fewer lines, if that pleases you. If you're really ambitious, you could even test it :-) Raul Rockwell