## Change the value of a variable between each iteration of fminsearch

### Ivan Cantarin (view profile)

on 13 Mar 2019
Latest activity Commented on by Jeff Miller

on 15 Mar 2019

### Jeff Miller (view profile)

Hello, I need help to find a way for switching the value of a variable between each iteration of an optimisation by "fminsearch". Is it even possible ?
Actually, it's a ponderation factor of a cost function which I made in Simulink.
My code :
x0=[39,102]; %Initialisationd de Kc, Ti et des vecteurs
[x,fval,history] = my_problem_2(x0); %On ne fait tourner la routine d'optimisation
Kp_v = history(:,1);
Ti_v = history(:,2);
[p,~] = size(Kp_v);
Jc_v_opt = ones(p,1); %On initialise les différents vecteurs
gamma_v = ones(p,1);
gamma= 5000;
for i = 1:p
Kp_inc = Kp_v(i,1); %On parcourt les vecteurs de l'historique des points, et pour chaque duo de points (Kp et Ti), on calcule le coût associé
Ti_inc = Ti_v(i,1);
Kps=num2str(Kp_inc); %On transforme une valeur en un string pour pouvoir l'envoyer dans le bloc
Tis=num2str(Ti_inc);
set_param('test_PI_vis/PI incrémental pour centrale de vis','Kp_inc',Kps,'Ti_inc',Tis);
[~,~,Y1,Y2,Y3] =sim('test_PI_vis',600000,[]); %On simule avec les paramètres de cette simulation pendant 100000 sec
Jc_v_opt(i,1)=Y1(end); %En sortie de la simu, on a les coût qu'on envoie dans des vecteurs
if Y2(end) == 0 %On calcule le gamma pour chaque set de paramètres parcouru par la routine d'optimisation
gamma = 1;
else
gamma = Y3(end)/Y2(end);
end
if gamma == 0
gamma_v(i,1) = 1;
else
gamma_v(i,1) = gamma;
end
end
The function my_problem_2 is the function doing the optimisation with fminsearch.
Actually, I want to calculate gamma and change its value for each iteration. I'm calculating it for each iteration in the loop but at the moment, it's useless, because I don't know how to change it when fminsearch is working.
Here is the code of my_problem_2 if it can help :
function [x,fval,history] = my_problem_2(x0)
history = [];
options = optimset('OutputFcn', @myoutput);
[x,fval] = fminsearch('m_optipi_vis', x0,options);
function stop = myoutput(x,~,state)
stop = false;
if isequal(state,'iter')
history = [history; x];
end
end
end
And the code of my function m_optipi_vis :
function [Jc]=m_optipi_vis(x) %Permet d'utiliser la fonction fminsearch dans le but de trouver le meilleur Kp et Ti
% X est donné dans la fonction fmnsearch
Kp_inc=x(1); %On donne une valeur initiale pour Kp
Ti_inc=x(2); %On donne une valeur initiale pour Ti
Kps=num2str(Kp_inc); %On transforme une matrice en un string pour pouvoir l'envoyer dans le bloc
Tis=num2str(Ti_inc);
set_param('test_PI_vis/PI incrémental pour centrale de vis','Kp_inc',Kps,'Ti_inc',Tis);
[~,~,Y1,~,~]=sim('test_PI_vis',600000,[]); %600000 est le temps de simulation
Jc=Y1(end);
end
I know I'm asking a lot but I'm really blocked on this.
Can someone help me plz ?

Alan Weiss

### Alan Weiss (view profile)

on 13 Mar 2019
I do not understand what you hope to accomplish. You are free to ask fminsearch to take just one iteration and then stop, but I don't think that is what you want.
If you would explain what you hope to get out of this, what you are really trying to obtain as the result of some calculation, then maybe we would have some ideas.
Alan Weiss
MATLAB mathematical toolbox documentation
John D'Errico

### John D'Errico (view profile)

on 13 Mar 2019
Alan is as puzzled as I am, I think for good reason. What you are doing would seem to be creating an objective function that is signifcantly non-smooth, and probably discontinuous. As such, it will make fminsearch potentially fail (I'd say you will make your results unstable), even if you managed to figure out how to do this.
Your goal may possibly be accomplishable using a tool like GA, which is more tolerant of such shenanigans. But even there, it is not at all clear what you are trying to accomplish.
Stephen Cobeldick

### Stephen Cobeldick (view profile)

on 14 Mar 2019
Actually, I have a cost function with two parts. And I want to make the cost of both parts always equal, so fminsearch minimizes them equally at each iteration.
For that, I need to calculate at each iteration gamma which is the ponderation factor, and change it to make the cost of both parts equal !
But maybe, like you said, it's impossible to do this with "fminsearch" ?

### Jeff Miller (view profile)

on 14 Mar 2019

If gamma has to be changed at each iteration, can't you let the error function change it? The error function is called at each iteration.

Ivan Cantarin

### Ivan Cantarin (view profile)

on 15 Mar 2019
I don't know, how can I try this ?
Jeff Miller

### Jeff Miller (view profile)

on 15 Mar 2019
I don't understand the problem well enough to give any detailed suggestions, but my general idea was that the error function could simply call the function to recompute gamma, passing as parameters the current level and flow costs and whatever other values are needed.

### Alan Weiss (view profile)

on 14 Mar 2019

I still think that I do not understand what you are really trying to do. But if you wnat to keep two functions equal, then in fmincon you can represent this requirement as a nonlinear equality constraint. Such a constraint will not necessarily be satisfied at each iteration, but should be satisfied well enough by the end.
Alan Weiss
MATLAB mathematical toolbox documentation

#### 1 Comment

Stephen Cobeldick

### Stephen Cobeldick (view profile)

on 14 Mar 2019 