Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fminsearch - hold a variable constant

  1. Mar 1, 2008 #1

    I want to pass a number of variables through fminsearch. Because of the structure of the program, some of those variables must be held constant while the others are minimized. Is there a way to do this with fminsearch, or is there another minimization program which allows this?


  2. jcsd
  3. Mar 2, 2008 #2


    User Avatar

    Well, an array will do, won't it?
    If you can give me more details on what fminsearch is all about, I may be able to help you better.

    Duplicating the array into another temporary array will do the trick, won't it?

    Best regards,
  4. Mar 3, 2008 #3
    Thanks for the reply Eus.
    Unfortunately that won't work. I am designing a GUI for a program already written. The program calls on 7 functions, sending three variables to the first function, which returns six variables, two of which go to the second and three to the third etc. The GUI sends the three initial variables to the main program which minimizes the answer and returns a result.

    I want to send a filename to the main program which will send it to the 6th function to compare. However, if this function is to receive the filename, it will have to receive it at every stage of the fminsearch loop, so I will need to keep the filename from being varied.
    Thanks again,

  5. Mar 3, 2008 #4


    User Avatar

    How does the GUI communicate with the program? By just invoking the program with a command-line? For example, if the user clicks a run button, the GUI will invoke something like this: execve ("program_name", list_of_args, list_of_envp);
    Or, by using the API of the program?

    How does the program return the six variables? Is it in an array or in an environment variable?

    A good program should not modify the argument passed into it.
    So, if the program follows that principle, you should not need to worry about the filename being modified.

    If you are willing to give more details on the environment in which the program is run, the mechanism of communication between your GUI and the program, and the details of the fminsearch, we may help you better.

    Best regards,
  6. Mar 14, 2008 #5
    Thanks again for your help Eus,
    I will try to explain the environment a bit better.

    The original program takes in three variables which are plasma parameters, and calculates the expected spectrum based on these variables. This calculation requires a total of seven different codes, which send and receive certain variables. One of these codes opens a file, which is an experimental spectrum, and compares the calculated with the experimental. A graph is plotted.

    To match the calculated spectrum with the experimental, fminsearch is run. It minimizes the difference between the two and returns three variables which closely match with the experimental spectrum.

    All of the above runs fine.

    However, to implement the GUI, more than just the three variables must be sent. I want to create other options in the GUI, most importantly the option of choosing a file to compare the spectrum with.

    However, to send a filename I will have to do it via fminsearch, since the code that compares the calculated and experimental is not the main program but one of the seven in the loop. Hence I need to be able to send the filename as part of a vector which will now hold 4 variables. Can I send a 4 variable vector through fminsearch but only vary 3 of the variables? I can send the files if you want a closer look. Thanks again,
  7. Mar 14, 2008 #6


    User Avatar

    Of course you can send in four variables vector through fminsearch (I assume that the program is written in C/C++).

    But, will the function that eventually opens the file know whether there is an extra variable in the vector that is passed into it? If it doesn't know anything about the extra variable, you cannot tell the function to open a different file than usual, can you?

    What I want to know is how that function that opens the file know what file it is supposed to open. Does the filename hard-coded in the function definition? If not, how could the function what file it should open?

    Yes, please send the files.

    Best regards,
  8. Mar 14, 2008 #7
    fminsearch prob

    The code is written in Matlab. If you run RunsakkaGUI there should be two plots. The one on the left will work, as its only a single run of the program. The one on the right works, but only with the file that's written into the minimization code. I have included two sample files. Thanks,

    Attached Files:

  9. Mar 14, 2008 #8
    try sigma_1 = 1e3, N_2b = 1e19, N_e0=1e24
  10. Mar 17, 2008 #9


    User Avatar

    I have never used MathLab before.
    Therefore, I think I cannot help you further.
    I hope someone else in this forum can help you with that.

    Best regards,
  11. Mar 18, 2008 #10
    Thanks alot for your help anyway Eus, it's much appreciated.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook