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?
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?
Ahh yes, init_T needed two sets ofbrackets, 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...?Code:{}
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?
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:
- Understand your problem: Done -- you know the math involved.
- Plan a solution: Design data types, functions, etc. that you will need to code up your solution (done or mostly done).
- Write a tiny bit and test it, comparing against matlab.
- 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.
Ok i have noticed something. In the file neldermead.c on line 29 it says
these are parameters that control the adjustments made by the algorithm.Code:static dbl al = 2, bt = 0.5, gm = 1;
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.
Speaking of hammers....
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.)
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.
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...
So get your C textbook out and read past chapter 2 this time.I never managed to get C to passs the variables correctly.
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.)
When I change them back (static dbl al = 1, bt = 0.5, gm = 2;), the second function works!
Last edited by tabstop; 08-17-2011 at 11:23 AM.
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.
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[]).What is my incorrect declaration of main you talk of - again i didnt write this, it came from the template.
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: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.
Try switching to the current, and more readable:Code:int function(int, double) int x; double y; { ... }
Code:int function(int x, double y) { ... }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?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!
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.
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.
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.