Using NR in Excel


jk1
09-26-2002, 02:29 PM
I am trying to implement some of the optimization routines in an Excel application (the Powell and related functions from Ch10)
Can anyone give me some guidance as to the appropriate way to do this (I am not a C++ programmer). I compiled a dll, but the form of the routines looks inappropriate (subroutines rather than functions) and I am unsure how to register them and use them in Excel.
Also, the VB versions of these routines exist, but look rather strange. Again any useful feedback and suggestions would be much appreciated.
JK1

hbell
05-13-2003, 12:07 AM
This message has languished a long time and deserves an answer (something like a message in a bottle).
Implementing Numerical Recipes in a spreadsheet seems an excellent educational approach. The NR authors might consider tackling Excel spreadsheets as a next big project!

In answer to your question, you probably want to incorporate the Excel "Solver" add-in (unless you feel that would be cheating) to solve one-dimensional minimizations. The set up would then be very simple: For example, (1) establish a fixed cell that will be the parameter to multiply your direction set vector (put the address of this cell in the Solver menu where it says "By Changing Cells". (2) Establish a fixed vector of contiguous cells to be the "direction set". (3) Construct a scalar-vector product (a vector) from the variables described in the previous two sentences. (4) Construct a "previous best vector" adjacent to the vector in the last sentence (this will have values only). (5) Construct your function (a scalar) that you are trying to minimize out of the sum of the previous two vectors (4) + (5) above) in the last two sentences then put the fixed address of this cell in the Solver menu where it says "Set Target Cell". (6) The Click the "Min" radio button assuming you want a minimization. (7) Now somewhere else construct a square matrix that will be the direction set vectors of order that is the number of unknowns in your optimization; probably start with the identity matrix or a scaled down identity matrix. Click the "Options" button and choose among the various one-dimensional optimization options. Now each iteration of the Powell routine will: copy the vector (3) to the vector (4) by value, copy a column from the matrix in step (7) to the vector in step (2) then click the Solve button from the Solver menu, then paste the resulting vector (3) by value back in the direction set matrix where you had just copied it from. Write a macro to do the previous sentence for each column in the direction set matrix. steps. I didn't mention the re-orthogonalization but that would be a straightforward refinement.

I haven't tried this but it ought to work.

Sorry it's too late for fall semester!
Harry Bell