1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Nelder Mead algorithm

  1. Sep 29, 2013 #1
    Hi This is a question somewhere between maths and numerical methods...

    I am using the algorithm for Nelder Mead found here:

    in java. It works nicely with the two example functions - rosen and parab.
    I am trying to adjust it to my function:

    Code (Text):

        static double myFunction(double p[]) {
            double v0;
            double sum = 0;
            double curve;
            double errorVector;
            int index = 0;
            v0 = (1 + (p[0] / 2) * timings.get(0) * timings.get(0))
                    / timings.get(0);

            for (Double f : timings) {
                curve = (v0 * f - (p[0] / 2)) * f * f;
                errorVector = index - rotorCurve;
                sum += errorVector * errorVector;
            System.out.println("p:" + p[0]);
            return sum;
    so that I can minimize for the sum and find p[0].
    My test data is


    The problem I think is occuring with my understanding of the 2D array simplex[]. As far as I understand the first two columns are the paramter initial values and the third is storing the value of the function, but Im not sure why there are three rows. I assume its three different values for the paramters or something, but it doesnt seem to make sense to me.

    I have attached my version of the algorithm for my function, in the hope that someone can help me get it to solve the problem.

    Sorry I made a mistake in my attachment. I had done a couple of undo's that need to be put back.

    The array simplex should be:
    Code (Text):

            double simplex[][] = // [row][col] = [whichvx][coord,FUNC]
            {{ 0.004, 0.0}};

    Attached Files:

  2. jcsd
  3. Sep 30, 2013 #2
    Is this in the wrong place? Could an admin move it please? Perhaps general engineering would be better?
  4. Sep 30, 2013 #3


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    The first two columns of simplex are storing initial points to evaluate your function if your function is over R2, and the third column is storing function values. If it's over R then it looks like the first column is initial points to evaluate at, the second column is storing the value of the function, and the third column is totally superfluous. The point is that you are supposed to evaluate the function on a simplex - in two dimensions that is a triangle with three vertices to keep track of, in 2 dimensions it's a line segment with two vertices to keep track of. So it seems like NPoints should be changed to 2 along with NDims to 1, and you should resize your simplex matrix to be 2x2 (this last part I suppose is not strictly necessary, but there are elements in that matrix that will never be referenced/changed).
  5. Sep 30, 2013 #4
    Thanks Office_Shredder, so now it compiles and converges, however it converges to the wrong value. I have a paper that gets the result as 0.0068 and My matlab program also gets that value, however this seems to converge to -0.32
    I now have simplex setup as:

    {{ 0.006, 0.0}, {0.0, 0.0}};

    i.e 2x2. Should the other values all start at 0? Or should they be something else?
  6. Sep 30, 2013 #5


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    It doesn't matter what they start at, the first thing the program does is evaluate your function and fill in (in your case) f(.006) and f(0) into the second column.
    That's what

    is doing.

    As to why it's converging to the wrong value, my understanding is that if your starting simplex is too small that you can get stuck in a local search. If the concavity around .0068 is very large, it might be that on the interval [0, .006] the function is sloped so that following the gradient means moving towards negative numbers, in which case the search would do that. You should try starting with a larger interval, like have your first column have a 0 and a 1 in it.
  7. Sep 30, 2013 #6


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    The Nelder-Mead simplex method is designed for 3d surfaces. I bet there are better ways to minimize a 2d function. In 3d N-M starts with a simplex of 3pts all on the surface. For minimization it will find a new simplex by reflecting the max valued vertex across the center pt of the line connecting the other 2 vertices. It then repeats this process until your stopping conditions are met.

    It seems to me that it really depends upon a 3d surface to work best, not sure how well it will work in 2d. Good luck.
  8. Nov 24, 2014 #7
    which function you used ???
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Java Nelder Mead algorithm
  1. No fastest algorithm (Replies: 10)

  2. Search algorithm (Replies: 14)

  3. Math algorithm (Replies: 5)

  4. Nesting Algorithm (Replies: 1)