# Create a for loop using fminsearch

4 views (last 30 days)
jeff on 19 Apr 2016
Answered: Walter Roberson on 21 Apr 2016
I am creating a matlab code that utilizes fminsearch over a number of guesses for initial variables (3 variables total, 10 guess). I have created an array of guesses, i.e. a 10x3 matrix with each row representing one iteration (1,1 = var1, 1,2 = var2, 1,3 = var3). I have run the code with nominal values and a single iteration works, but I don't know how to incorporate the for loop with fminsearch. The objective function is elusive to me. In this particular problem, second order differential equations are solved for each set of variables, and then a corresponding statistical value is calculated for each case. When creating the objective function to use for fminsearch, should I put my for loop within the main script, or in the objective function? Additionally, since the objective function can only take one variable array, and I have recently been told it is nothing but a placeholder, should I define my variables within the objective function? An example of using a for loop with fminsearch using ode45 and how to choose the proper objective function would be very helpful. I understand that the objective function is the function I want to minimize, however the function I want to minimize (which is a statistical value sigma), depends on a number of other functions. Any help appreciated. Apologize if this is a poor description. Also, please do not suggest using the optimization toolbox, I need to solve this strictly using code. Thanks!

Chibuzo Nnonyelu on 21 Apr 2016
If I understand your question correctly, you want to be able to run the fminsearch for each initial guess at the time time?
Well, you can use the parfor and make sure to have every variable you need inside the parfor loop.
However, parfor loop does not allow the creation of figures inside the loop.
My suggestion is that you use the optimtool and export the code after entering the details in the optimtool GUI. You can format the code as a function and run inside the parfor loop.

Walter Roberson on 21 Apr 2016

### Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by