Asked by Ivan Cantarin
on 13 Mar 2019

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 ?

Answer by Jeff Miller
on 14 Mar 2019

Accepted Answer

Ivan Cantarin
on 15 Mar 2019

I don't know, how can I try this ?

Jeff Miller
on 15 Mar 2019

Sign in to comment.

Answer by Alan Weiss
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

Stephen Cobeldick
on 14 Mar 2019

Ivan Cantarin's "Answer" moved here:

I will show you with a screen, it will be easier.

You see, there are the level cost, and the flow cost. But my fminsearch is minimizing the global cost. I want both costs to be the same order of magnitude at each iteration, so fminsearch optimizes them equally and doesn't focus on the most important cost. And like I said, for that, gamma needs to be calculate at each iteration and changed in the Workspace !

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Alan Weiss (view profile)

Direct link to this comment:https://fr.mathworks.com/matlabcentral/answers/449868-change-the-value-of-a-variable-between-each-iteration-of-fminsearch#comment_680761

## John D'Errico (view profile)

Direct link to this comment:https://fr.mathworks.com/matlabcentral/answers/449868-change-the-value-of-a-variable-between-each-iteration-of-fminsearch#comment_680792

## Stephen Cobeldick (view profile)

Direct link to this comment:https://fr.mathworks.com/matlabcentral/answers/449868-change-the-value-of-a-variable-between-each-iteration-of-fminsearch#comment_681311

Sign in to comment.