1

I want to find the Minimum of a function using

[x,fval] = fminsearch(@(param) esm6(param,identi),result(k,1:end-1),options)

now for each Iteration step i want some values that the function 'esm6' calculates to be saved in an Array. I tried the following: In the first line of the function i wrote

identi.sim.i_optiIter = identi.sim.i_optiIter + 1;

to have an iteration-variable counting the iteration steps of fminsearch. And later to catch the values that I need I used

identi.sim.guete_werte.gew(identi.sim.i_optiIter,:,:) = y_sim;
identi.sim.guete_werte.ungew(identi.sim.i_optiIter,:,:) = y_sim_ungew;

and to make sure that I use the new values of the identi-struct for the next function call, I wrote this at the end of the function:

assignin('base','identi',identi);

Now unfortunatly it doesn't do what I wanted it to do. Can anyone help me with this?

EDIT:
I made another attempt on it, using an Output function. I extendend my Options like this:

options = optimset('Display','iter','MaxIter',3,'OutputFcn',@outfun);

But now the Problem is that i cannot figure out where to put this outfun. The outfun Looks like this:

function stop = outfun(x,optimvalues,state,iteration,y_sim,y_sim_ungew)
    stop = false;
    if state == 'iter'
        guete_werte.gew(iteration,:,:) = y_sim;
        guete_werte.ungew(iteration,:,:) = y_sim_ungew;
    end
end

Now the Problem with it is, that i can not put it in the file, where i call the fminsearch, because that is a script. If i put the outputfunction into a separate .m-function file, it is not able to Access the variables of the esm6 function. And if I add it to the esm6-function file, matlab can't find the function and says

??? Error using ==> feval Undefined function or method 'outfun' for input arguments of type 'struct'.

Max
  • 1,471
  • 15
  • 37
  • 2
    Can you be specific about what variables you want saved? Are you sure they aren't optional outputs of `fminsearch`? Otherwise I suppose you can do what you're doing but instead of the `assignin` declare them as `global` in both your `esm6` function as well as your script that you call `fminsearch` from (or else in the command line) – Dan Dec 11 '15 at 09:44
  • @Dan +1: the way to do this is global variables. – Matthew Gunn Dec 11 '15 at 09:53
  • @dan unfortunatly it didn't work. declaring the variabel as global in esm6-function AND the fminsearch-calling-script produced Errors. So I just declared it once in the fminsearch-calling-script and called fminsearch with 'MaxIter' 10 and it made 19 function calls and at the end the Iteration Counter was still 1 and not 10 or 19. – Max Dec 11 '15 at 10:25
  • and yes, i am sure they are no optional Output, since the variables are just some "random" variables in the esm6 function. Right now I'm trying to figure out a way to do it with a "nested Output function", but didn't manage to do it yet. – Max Dec 11 '15 at 10:26
  • Please add the code you used to declare them as global as well as the error you get to your question. It won't do anything unless they are both declared global: http://www.mathworks.com/help/matlab/ref/global.html – Dan Dec 11 '15 at 11:03
  • @dan the code i use is just `global identi;`, should I use any more code? And the Errors I get are something like 'No Simulink-Object with Name 'blablabla' found to set Parameter to'. In the esm6 function I use values of the identi-struct to set Simulation Parameters and then simulate the a model. As soon as i run it without the global Thing, it works again. Is there no other way of doing it without global variables? they seem to be Messing up anything in my code, and i don't know what. – Max Dec 11 '15 at 11:11
  • If `global` is causing a problem, then you need to add more information to your question. Another (messier) option would be to use `save` and `load` within `esm6` – Dan Dec 11 '15 at 11:13
  • well, are you sure it's not possible to do it with an outputfunction? I will add my attempt into the question and maybe you can make it work? Because i really don't know wich parts of the code i would have to add.... – Max Dec 11 '15 at 11:56
  • @Dan okay, i solved the Problem using global variables. I just had to use new variables instead of my identi-struct and it worked out. Thank you very much. If you make this an answer, I can accept it. If you have a solution using outputfunction, I would still be very happy ;) EDIT: oh and do you know, how I can receive an Array containing the number of function Counts for each Iteration step? using `[x,fval,exitflag,output] = fminsearch(...)` i only get the amount of iterations and function Counts in total. – Max Dec 11 '15 at 13:23
  • @Max I've never seen the output function option before, but what you could try is wrapping your call to `fminsearch` in it's own function (and function file) and then including both `esm6` and `outfun` as [local functions](http://www.mathworks.com/help/matlab/matlab_prog/local-functions.html) in that new function file (i.e. underneath your new wrapper function). – Dan Dec 11 '15 at 13:29
  • I tried that already :/ ´do you know a solution to the other question? otherwise I'll make it a new question – Max Dec 11 '15 at 13:31
  • I"m not sure what you meant by the other question. Have you tried making it a [nested function](http://www.mathworks.com/help/matlab/math/output-functions.html#bsgpq6q-31)? – Dan Dec 11 '15 at 13:34
  • yes, i have tried 4 different Options: 1. function in the fminsearch caller-script (which was stupid) 2. function in the m-file of esm6-function 3. nested function in the esm6-function-file 4. own m-file. None of them worked. And about the other question: I use the Option 'Display','iter'. This results in fminsearch telling me in the command window, at which Iteration it made how many function calls. For example it says at Iteration 0 i made 3 calls, at Iteration 1 i made 5 calls, at Iteration 2 i made 6 calls and so on..... I want exactly this Information as Output to be used afterwards. – Max Dec 11 '15 at 13:56

0 Answers0