Thread: Parameter estimation Nelder Mead

  1. #16
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The fact that your first iteration (for Fitted_Curve and Error_Vector at least) works and the the subsequent ones don't tells me that you are not properly updating values between iterations. Not a C issue, per se, but a flaw in your translation (you would probably need to make the same update between iterations in matlab). Also, your sum/sse is not matching (I think they should be the same -- confrim please) between matlab and C, so that looks like a problem too. Again, full code please.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by a.mlw.walker View Post
    Code:
        //calculating physical condition parameters a and b (stored as a_global and b_global)
    	if (NelderMeadSimplexMethod(n, function, x, length, &fopt, timeout, eps) == success) {
    		printf("reaching to minimum ");
    	} else {
    		printf("timeout  ");
    	}
    ...
    
    static double function(int n, double x[])
    {
        ...
    }
    Actually I'm going to go with 'Tater on this. It's not a C thing, it's a "I don't know C"-thing. For example, you are using function without your program knowing anything about it. (By that I mean it's not that C is doing something wrong, it's that you are doing something wrong because you don't understand what you are doing.)

    You need to start compiling with warnings on.


    Quzah.
    Last edited by quzah; 08-16-2011 at 03:44 PM.
    Hope is the first step on the road to disappointment.

  3. #18
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    No offense taken. The thing that confuses me is that for the first iteration of the problem, the printout of the C is exactly the same as the matlab. However the second iteration is different (and wrong because the matlab gives the right answers - i have the solution). So to me it seems like "I dont undertand C" because the maths is right once, therefore it will be right twice - so something else is incorrect. Could it not be passing x[] correctly?

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I'll spare you the "read the textbook" lecture here... but one thing you absolutely should do is to try to break your big math function down into smaller blocks that can be examined individually... shorter computations with intermediate variables, perhaps even separate functions. You have one large formula... work it by hand and make note of every place you use an intermediate value or write down the current value of something... that's probably pretty close to where you should do it in C as well.

    In my experience C likes working in small blobs and usually is a lot easier to fix that way too.

  5. #20
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    I think you are looking at old images or something so I have reposted the first three iterations. for the first iteration sum is the same as the matlab sse value. but the next iteration is not the same, so I think arduil is right and I am not passing back the parameters correctly, but I dont think I have changed anything to stop it passing them correctly
    Attached Images Attached Images Parameter estimation Nelder Mead-fittedcurelatest-jpg Parameter estimation Nelder Mead-matlab-3-iter-jpg 

  6. #21
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The sum is wrong on the first iteration. Matlab shows 1.217e3, C shows 535. That's one definite problem. Your matlab works correctly by your admission, and your C doesn't. That doesn't mean "the math is right" in both places. You very likely translated your matlab to C incorrectly. You could have written totally valid C, but if you screwed up the order of operations/statements, or screwed up a trig identity (acoth x = atanh 1/x), that's not a C issue, that's a math issue on your part. Maybe it was how you passed x, maybe not. We need to see your full C code to help you find/fix any C issues like overflowing your array bounds, pointer errors, etc.

    EDIT: Yes, your previous post (#11) with attachments had incorrect sum values. Ignore that part.

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is there any particular reason you just keep posting dumps of numbers, instead of what we've asked for multiple times now? We don't care about your output, we want to see the code. If I have the code, I can get my own output.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #23
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Look, nobody knows what NelderMeadSimplexMethod() does. We don't know if it screws up x or not. C and matlab probably handle arrays (vectors) passed to functions differently (I haven't done matlab in years, so who knows). In C, changes made to an array passed into a function persist after the function call. But for the last time, until you show us ALL YOUR BLOODY CODE, we can't help you very much.

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by a.mlw.walker View Post
    I think you are looking at old images or something so I have reposted the first three iterations. for the first iteration sum is the same as the matlab sse value. but the next iteration is not the same, so I think arduil is right and I am not passing back the parameters correctly, but I dont think I have changed anything to stop it passing them correctly
    Sounds reasonable. If you think that a_global and b_global are what parameters you're passing back, then fine, as long as you are aware that (as far as we can see) you never make use of them ever, even a little bit, while computing -- so expecting changes to those variables to do something seems foolish. Perhaps you want to initialize a and b to a_global and b_global rather than x[0] and x[1]. Or conversely, perhaps you want to change x[0] and x[1] rather than a_global and b_global.

    EDIT: What I just wrote would make sense if you were actually calculating a new a and b (or x[0] and x[1], whatever). Since you are actually doing nothing of the sort, you have calculated no changed parameters, and therefore you are unable to actually change some parameters.
    Last edited by tabstop; 08-16-2011 at 04:32 PM.

  10. #25
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    Ok I got it you want code. I have attached all the files that should get it to run. No more images of numbers... except the last image shows the matlab, that might help, i dunno...
    p.s a_global and b_global are just variables i plan on using in the future, (in another function - i know you dont like global!!!)
    Thanks guys
    Attached Files Attached Files

  11. #26
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Please read
    Cprogramming.com FAQ > Casting malloc

    You never included the correct header of stdlib.h

    Edit: I suggest you read up on the proper use of the keyword "extern"; you use it right before you define the function some Compilers may not like that.

    This code below should NOT have the extern; and, I would NOT use the old K&R method of function parameter typing.
    Code:
    extern void vectorcopy(n, x, y)
    int	n;
    dbl	x[], y[];
    {
    	int	i;
    
    	for (i=0; i<n; i++) {
    		x[i] = y[i];
    	}
    }
    Tim S.
    Last edited by stahta01; 08-16-2011 at 05:48 PM.

  12. #27
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For starters, clean up the warnings:
    Code:
    main.c:15: warning: missing braces around initializer
    main.c:15: warning: (near initialization for `init_T[0]')
    main.c: In function `function':
    main.c:38: warning: implicit declaration of function `asinh'
    main.c: At top level:
    main.c:80: warning: return type defaults to `int'
    main.c: In function `main':
    main.c:81: warning: unused variable `right'
    main.c:81: warning: unused variable `fall_angle'
    main.c:81: warning: unused variable `fall_time'
    main.c:81: warning: unused variable `guess'
    main.c:81: warning: unused variable `c0'
    main.c:81: warning: unused variable `xx'
    main.c:82: warning: unused variable `i'
    main.c:82: warning: unused variable `num_inputs'
    main.c:83: warning: unused variable `y'
    main.c: At top level:
    main.c:61: warning: 'rotor_function' defined but not used
    mat.c:15: warning: missing braces around initializer
    mat.c:15: warning: (near initialization for `init_T[0]')
    mat.c: In function `function':
    mat.c:38: warning: implicit declaration of function `asinh'
    mat.c: At top level:
    mat.c:80: warning: return type defaults to `int'
    mat.c: In function `main':
    mat.c:81: warning: unused variable `right'
    mat.c:81: warning: unused variable `fall_angle'
    mat.c:81: warning: unused variable `fall_time'
    mat.c:81: warning: unused variable `guess'
    mat.c:81: warning: unused variable `c0'
    mat.c:81: warning: unused variable `xx'
    mat.c:82: warning: unused variable `i'
    mat.c:82: warning: unused variable `num_inputs'
    mat.c:83: warning: unused variable `y'
    mat.c: At top level:
    mat.c:61: warning: 'rotor_function' defined but not used
    neldmead.c:11:20: values.h: No such file or directory
    neldmead.c: In function `initialize':
    neldmead.c:64: warning: implicit declaration of function `malloc'
    neldmead.c: At top level:
    neldmead.c:32: warning: 'fprint_simplex' defined but not used
    neldmead.c:41: warning: 'fprint_points' defined but not used
    neldmead.c:51: warning: 'fprint_generated_points' defined but not used
    If you do, you'll also find that you have a main function in each of those, which you don't want if they are to somehow be linked together.


    Quzah.
    Last edited by quzah; 08-16-2011 at 06:11 PM.
    Hope is the first step on the road to disappointment.

  13. #28
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by quzah View Post
    For starters, clean up the warnings:If you do, you'll also find that you have a main function in each of those, which you don't want if they are to somehow be linked together.


    Quzah.
    Are you positive you got the right mat.c; the one I downloaded did not have a main it it.
    Note: I did get a lot of warnings like you did.

    Tim S.

  14. #29
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by stahta01 View Post
    Are you positive you got the right mat.c; the one I downloaded did not have a main it it.
    That was it. I had pasted the wrong file in on that one.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #30
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    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

    I think that there is definately not a main in each of the files, I understand that there can be only one main. I didnt write the function Nelder Mead, it comes from available source code. It came with an example of its use. I first used it to calcualate two parametrs, but expanding it to three and it doesnt converge......
    I'll see about that external but ut was in the original code.
    I added in the stdlib.h file but the final result is still incorrect. I wondered whether a tolerance somewhere is setting to to be able to stop early?
    By the way, i have attached the fit that C gives and the fit that matlab gives - matlab on the left. For some reason matlab finds a far better solution...
    Attached Images Attached Images Parameter estimation Nelder Mead-matlab-vs-c-graphs-jpg 
    Last edited by a.mlw.walker; 08-17-2011 at 03:52 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