Thread: Parameter estimation Nelder Mead

  1. #31
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by a.mlw.walker View Post
    Most of those warnings are because of unused but declared functions - thats because i have been stripping back the code to try and find the problem, however i didnt remove the global variable names - shouldnt affect the running of it. if i put all the code back together most of them go apart from:

    main.c:16: warning: missing braces around initializer
    nm_t0\main.c:16: warning: (near initialization for 'init_T[0]')
    :108: warning: return type defaults to 'int'
    In function 'main':
    warning: 'return' with no value, in function returning non-void
    And you don't think using an unititlized array affects your calculations?

  2. #32
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    Sorry CommonTater, which one suggests it is unititialised? init_T[0] is not used at this stage so how could it be affecting the results?
    what braces though is it reffereing to?

  3. #33
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    Sorry CommonTater, which one suggests it is unititialised? init_T[0] is not used at this stage so how could it be affecting the results?
    what braces though is it reffereing to?

  4. #34
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by a.mlw.walker View Post
    Sorry CommonTater, which one suggests it is unititialised? init_T[0] is not used at this stage so how could it be affecting the results?
    what braces though is it reffereing to?
    The error in line 16 from your error list...

  5. #35
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    Ahh yes, init_T needed two sets of
    Code:
    {}
    brackets, sorted now, but doesnt solve the problem... can i fit a constraint possibily that decides whether C has found the best solution to the problem...?

  6. #36
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    Guys what do you reckon. My graph above shows that my code may be finding a minimum of sorts, but that is not a good curve. I think it is finding a local minimum as supposed to the global minimum. Interesting that the matlab implementation finds the global minimum. The following website states that nelder mead method can fail in simple situations. i wonder if matlab is doing "more" than just a nelder mead. i have heard the rosenbrock algorithm can find global minimum, anyone used it? Or an ant search or something? anyone used that?

  7. #37
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Matlab is doing exactly what you tell it. So is C. The reason you're getting different results is because your Matlab and C code aren't telling the computer to do exactly the same stuff. Now, I can't compare the two without your matlab code, and frankly, I'm not too exited about helping you do that, especially when your code is so horribly indented and full of global variables, and you haven't fixed previous errors we've told you about (incorrect usage of extern, incorrect declaration of main, etc). But the fact remains, you have a method that works mathematically, as proven by matlab. It's a good method, and starting from scratch with a new method means you need to validate that new method in matlab, then code it in C, which is probably more work than fixing this method or even rewriting this method from scratch in C. Stick with Nelder-Mead, but back way, way up.

    This is a sizeable chunk of fairly complex code (mathematically at least, the C used here is fairly trivial), and would take any of us a long time to pore through all of it, even with your matlab code along side. Have you tested each component/function thoroughly on it's own? Make sure all your fundamental matrix functions, like vectorcopy, et al, function perfectly before going on to writing your more complex functions. I feel like this project was 800 lines of helter-skelter coding then an "oh crap, it doesn't work" moment.

    The general steps are:
    1. Understand your problem: Done -- you know the math involved.
    2. Plan a solution: Design data types, functions, etc. that you will need to code up your solution (done or mostly done).
    3. Write a tiny bit and test it, comparing against matlab.
    4. Repeat step 3, making sure to work in tiny increments, so you can more easily find and fix bugs.


    It's also very possible that it's something as simple as a minor typo in your code, perhaps using il instead of ih or i as an index in NelderMeadSimplexMethod(), or you accidentally swapped the source and destination in a vectorcopy() call. That type of stuff we can't help much with, since I doubt there are many Nelder-Mead experts hanging around here. You will have to double-, triple- and quadruple-check all those things.

  8. #38
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    Ok i have noticed something. In the file neldermead.c on line 29 it says
    Code:
    static dbl	al = 2, bt = 0.5, gm = 1;
    these are parameters that control the adjustments made by the algorithm.
    Matlab has a similar line, however the variable names are different. I fiddled with the values (just changing which one was equal to 1,2 and 0.5) and I get the correct answer for the three parameters I have been looking for, however using an alternative function to solve for, and it gives incorrect results again.

  9. #39
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by a.mlw.walker View Post
    Ok i have noticed something. In the file neldermead.c on line 29 it says
    Code:
    static dbl	al = 2, bt = 0.5, gm = 1;
    these are parameters that control the adjustments made by the algorithm.
    Matlab has a similar line, however the variable names are different. I fiddled with the values (just changing which one was equal to 1,2 and 0.5) and I get the correct answer for the three parameters I have been looking for,
    Speaking of hammers....
    Quote Originally Posted by a.mlw.walker View Post
    however using an alternative function to solve for, and it gives incorrect results again.
    That means you screwed something else up, so you're going to have to go look for more problems.

    (Parenthetical: My esteem for GSGIS is dropping rapidly just based on your name, which is completely unfair but I can't help it.)

  10. #40
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by a.mlw.walker View Post
    these are parameters that control the adjustments made by the algorithm.
    Matlab has a similar line, however the variable names are different. I fiddled with the values (just changing which one was equal to 1,2 and 0.5) and I get the correct answer for the three parameters I have been looking for,
    That's why I told you in your other thread to make the variable names exactly the same across Matlab and C. Who know what other such errors lurk in your code.

  11. #41
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    sorry arduil, i didnt see your post.
    I understand you guys arent going to pour over my code. I dont expect that.
    Over those errors:
    extern - I didnt originally write this function, it was the template for the function NelderMead.c I just adjusted the maths inside it to meet my problem - I'm an engineer not a computer scientist. If this should go, then no problem.
    What is my incorrect declaration of main you talk of - again i didnt write this, it came from the template.
    Global variables - I never managed to get C to passs the variables correctly. In pasts posts I have managed to get the function to work for the Rosenbrock function - a function used to compare optimization techniques. It works.

    My last post tho - about the parameters in NelderMead, swapping this around got me a solution to the function - the correct one, however for another function I have written it does not solve it correctly once I change these values.
    When I change them back (static dbl al = 1, bt = 0.5, gm = 2, the second function works!
    I think the method may be finding local minimums and when i swap the parameters it adjusts to a global minimum! Wotz goin On!
    In terms of the differing vairable names:
    There not my variables names they are the variable names SET by the Matlab function (not my function) fminsearch - i didnt write that, and in turn I cant change it...

  12. #42
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I never managed to get C to passs the variables correctly.
    So get your C textbook out and read past chapter 2 this time.


    When I change them back (static dbl al = 1, bt = 0.5, gm = 2;), the second function works!
    That means, when you wrote one of your functions, you forgot what the parameters were supposed to be. (EDIT: Granted with such incredibly meaningful names as al, bt, and gm, how you could forget them I don't know. I admit I have, now and again, copied a formula into C with the same variables, but usually only when those names are relatively iconic (t = time, rho = density). You don't have to make sure your formulas fit on a punch card any more, use real names.)
    Last edited by tabstop; 08-17-2011 at 11:23 AM.

  13. #43
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by a.mlw.walker View Post
    extern - I didnt originally write this function, it was the template for the function NelderMead.c I just adjusted the maths inside it to meet my problem - I'm an engineer not a computer scientist. If this should go, then no problem.
    This is not a trivial program, you need to understand C. We have some basic tutorials, and the web has many more. Get a decent C book (recommendations here), go through it in order and work the examples. The extern keyword is placed in a header file before a function prototype (or global variable definition) so that a .c file that #includes that header file knows that the actual function (or storage for variable) is located in another .c file. You don't put it before the function definition (body) in a .c file.

    What is my incorrect declaration of main you talk of - again i didnt write this, it came from the template.
    It's int main(void). See this FAQ article: Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]).

    Global variables - I never managed to get C to passs the variables correctly. In pasts posts I have managed to get the function to work for the Rosenbrock function - a function used to compare optimization techniques. It works.
    Again, learning C will help with this. You need to be able to handle basic parameter passing to write something like this. As a note, you use the old form:
    Code:
    int function(int, double)
    int x;
    double y;
    {
    ...
    }
    Try switching to the current, and more readable:
    Code:
    int function(int x, double y)
    {
    ...
    }
    My last post tho - about the parameters in NelderMead, swapping this around got me a solution to the function - the correct one, however for another function I have written it does not solve it correctly once I change these values.
    When I change them back (static dbl al = 1, bt = 0.5, gm = 2, the second function works!
    I think the method may be finding local minimums and when i swap the parameters it adjusts to a global minimum! Wotz goin On!
    Again, no Nelder-Mead experts here, and no Matlab code. Does your Matlab code function exactly as expected when minimize the second function? What parameters do you use for al, bt and gm and are they the same?

    EDIT: Make sure you go back and read my post #40. The time stamps suggest you may have just missed it when you made post #41.

  14. #44
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by a.mlw.walker View Post
    There not my variables names they are the variable names SET by the Matlab function (not my function) fminsearch - i didnt write that, and in turn I cant change it...
    I missed this line the first time. Just like any normal programming language, variables in Matlab are local to the function. If the person who wrote your Matlab function made those global variables, then you need to borrow our hammer and go to town. If they didn't, then it doesn't matter one counterfeit penny what the variable names are inside the function, because they don't exist outside the function anyway, and if you've been thinking that changing the variables in one function affects variables in a different function, I'm surprised we've gotten this far.

    EDIT: I looked it up, and you mean that fminsearch is a built-in function of Matlab. In that case there are no restrictions on your variable names whatsoever.

  15. #45
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    In the matlab function fminsearch it has the parameters, set to the same values - however as matlab calls them something else i couldnt tell which is which relative to the C. I just needed to try something (I agree - Hammer) - so tried the 6 combinations (3!) and one (static dbl al = 2, bt = 0.5, gm = 1 worked.
    For the second function i used, (static dbl al = 1, bt = 0.5, gm = 2) worked.
    Reading around the Nelder Mead method is known to sometimes find local rather than global minimums. The author of the function has used the NelderMead funtion to solve his problems and has tested it works for him.
    I trust the maths - that matches the matlab - thats what i do understand...
    I'll get my textbook - Sam's teach yourself C...
    Until I know whether the NelderMead function is a problem I fear fiddling with it. Its proven by the author to get correct results, its not my position to question it - yet.
    I plotted the graphs and posted them earlier - matlab on left C on right.
    They are both finding 'a' fit, but the matlab one is a much better fit.
    tabstop - my point is I dont know which one in C is supposed to be which in Matlab. I know I can change the names of the variables within the "built-in" function, but as i said changing the values in C got one function to be solved, while changing them again solved another function - so it is very hard to decide which one is which in the matlab (if you see what i mean)
    changed main to int main void()...
    Interms of the extern, do you mean in mat.c?
    Last edited by a.mlw.walker; 08-17-2011 at 11:34 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with simple Factorial Estimation
    By Arthurdent in forum C Programming
    Replies: 7
    Last Post: 06-16-2011, 07:28 PM
  2. Probability estimation
    By Mario F. in forum General Discussions
    Replies: 3
    Last Post: 09-18-2009, 08:40 AM
  3. Replies: 6
    Last Post: 01-08-2008, 10:25 AM
  4. project help (mead,median,mode)
    By Pliskin_Vamp in forum C Programming
    Replies: 1
    Last Post: 04-09-2007, 05:08 PM
  5. area estimation of graph
    By hei in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 12:23 AM