How to redefine a variable inside the function?

The problem is that the variable x13 has its input value, but every further iteration it should change. The code I've written uses its initial value every iteration, and I don't know how to redefine it correctly. I understand where the mistake is, but help me to fix it please
file 1
function [Q, y11, y12, y21, y22, y33]= myfun(x)
k1=3;
k2=6;
alpha1=0.3;
alpha2=0.3;
x12=10;
V1=x(1);
V2=x(2);
x13=x(3); %this variable gets its value from the array x in the file 2, but it has to be redefined further
C1=10; %cost 1
C2=15; %cost 2
C3=24;%cost 3
y11=(x12+x13)/(x12+x13+k1*V1);
y12=x12+x13;
x22=y12;
x21=y11;
y21=x22*x21/(x22+k2*V2);
y22=x22;
x31=y21;
x32=y22;
y33=x31*x32;
x13=y33; % It should be redefined here, and the same thing should happen in the every further iteration.
Q=C1*V1^alpha1+C2*V2*alpha2+C3*x32;
file 2
clear all
clc
fun = @myfun;
x = [20,30, 15];
A = [];
b = [];
Aeq = [];
beq = [];
lb = [5, 10];
ub = [15, 20];
nonlcon = [];
options = optimoptions('fmincon');
[x,fval,exitflag,output] = fmincon(fun,x,A,b,Aeq,beq,lb,ub,nonlcon,options)
Another interpretation, but the same problem
file 1
function [Q, y11, y12, y21, y22, y33]= myfun(x)
k1=3;
k2=6;
alpha1=0.3;
alpha2=0.3;
x12=10;
x13=15; % I assign the value to x13 here
V1=x(1);
V2=x(2);
C1=10; %cost 1
C2=15; %cost 2
C3=24;%cost 3
y11=(x12+x13)/(x12+x13+k1*V1);
y12=x12+x13;
x22=y12;
x21=y11;
y21=x22*x21/(x22+k2*V2);
y22=x22;
x31=y21;
x32=y22;
y33=x31*x32;
x13=y33; %it gets redefined here
Q=C1*V1^alpha1+C2*V2*alpha2+C3*x32;
file 2
clear all
clc
fun = @myfun;
x = [20,30];
A = [];
b = [];
Aeq = [];
beq = [];
lb = [5, 10];
ub = [15, 20];
nonlcon = [];
options = optimoptions('fmincon');
[x,fval,exitflag,output] = fmincon(fun,x,A,b,Aeq,beq,lb,ub,nonlcon,options)

 Accepted Answer

Torsten
Torsten on 30 Apr 2019
Edited: Torsten on 30 Apr 2019
Add the nonlinear constraint
x(3) - x31*x32 = 0
in function "nonlcon".
And remove the lines
y33=x31*x32;
x13=y33; % It should be redefined here, and the same thing should happen in the every further iteration.
at the end of "myfun".
And "myfun" must have only one return parameter (namely the value of the objective function), not six as in your code.

21 Comments

Could you explain why this solution?
So I can do the same operation with the similar cases in the code?
I don't understand your question.
This solution can be used together with the first two files.
If you say that myfun should have only one return parameter, then what should I do with the remaining equations?
Are you trying to minimize all 6 outputs at the same time?
only Q should be minimized. So I should have only Q in myfun? What should I do with the remaining equations?
What were you hoping to do with the y11 and so on being output by myfunc() ?
I think that's one of my mistakes too. But they are coupled equations. Maybe I should have them in a third file for nonlcon?
Torsten
Torsten on 2 May 2019
Edited: Torsten on 2 May 2019
Your relations show that all the variables you want to return in "myfun" can be deduced from V1, V2 and x13. The only relation that has to be taken care of is x(3)=x31*x32.
So you can just replace
function [Q, y11, y12, y21, y22, y33]= myfun(x)
by
function [Q]= myfun(x)
and - as already noted - define
ceq(1) = x(3)-x31*x32
in "nonlcon".
Okay, but where should I have
y11=(x12+x13)/(x12+x13+k1*V1);
y12=x12+x13;
x22=y12;
x21=y11;
y21=x22*x21/(x22+k2*V2);
y22=x22;
x31=y21;
x32=y22;
function [Q]= myfun(x)
k1=3;
k2=6;
alpha1=0.3;
alpha2=0.3;
x12=10;
V1=x(1);
V2=x(2);
x13=x(3); %this variable gets its value from the array x in the file 2, but it has to be redefined further
C1=10; %cost 1
C2=15; %cost 2
C3=24;%cost 3
y11=(x12+x13)/(x12+x13+k1*V1);
y12=x12+x13;
x22=y12;
x21=y11;
y21=x22*x21/(x22+k2*V2);
y22=x22;
x31=y21;
x32=y22;
Q=C1*V1^alpha1+C2*V2*alpha2+C3*x32;
end
function [c,ceq] = nlcon(x)
k1=3;
k2=6;
alpha1=0.3;
alpha2=0.3;
x12=10;
V1=x(1);
V2=x(2);
x13=x(3); %this variable gets its value from the array x in the file 2, but it has to be redefined further
C1=10; %cost 1
C2=15; %cost 2
C3=24;%cost 3
y11=(x12+x13)/(x12+x13+k1*V1);
y12=x12+x13;
x22=y12;
x21=y11;
y21=x22*x21/(x22+k2*V2);
y22=x22;
x31=y21;
x32=y22;
ceq(1) = x(3)-x31*x32
end
Thank you all for your answers.
But how will the value of x13 change every iteration?? This code doesn't actually work. I get the following error
Index exceeds matrix dimensions.
Did you provide initial guesses for all three design variables ?
If this is not the mistake, please show the complete code you are using.
I'll send it now. Give me five mins :)
file 1
function [Q]= myfun(x)
k1=3;
k2=6;
alpha1=0.3;
alpha2=0.3;
x12=10;
V1=x(1);
V2=x(2);
x13=x(3); %this variable gets its value from the array x in the file 2, but it has to be redefined further
C1=10; %cost 1
C2=15; %cost 2
C3=24;%cost 3
y11=(x12+x13)/(x12+x13+k1*V1);
y12=x12+x13;
x22=y12;
x21=y11;
y21=x22*x21/(x22+k2*V2);
y22=x22;
x31=y21;
x32=y22;
Q=C1*V1^alpha1+C2*V2*alpha2+C3*x32;
end
file 2
function [c,ceq] = nlcon(x)
k1=3;
k2=6;
alpha1=0.3;
alpha2=0.3;
x12=10;
V1=x(1);
V2=x(2);
x13=x(3); %this variable gets its value from the array x in the file 2, but it has to be redefined further
C1=10; %cost 1
C2=15; %cost 2
C3=24;%cost 3
y11=(x12+x13)/(x12+x13+k1*V1);
y12=x12+x13;
x22=y12;
x21=y11;
y21=x22*x21/(x22+k2*V2);
y22=x22;
x31=y21;
x32=y22;
ceq(1) = x(3)-x31*x32
end
file 3
clear all
clc
fun = @myfun;
x = [20,30];
A = [];
b = [];
Aeq = [];
beq = [];
lb = [5, 10];
ub = [15, 20];
nonlcon = @nlcon;
options = optimoptions('fmincon');
[x,fval,exitflag,output] = fmincon(fun,x,A,b,Aeq,beq,lb,ub,nonlcon,options)
The mistakes I get are the following
Index exceeds matrix dimensions.
Error in myfun (line 9)
x13=x(3); %this variable gets its value from the array x in the file 2, but it has to be redefined further
Error in fmincon (line 537)
% Evaluate function
Error in solution (line 14)
[x,fval,exitflag,output] = fmincon(fun,x,A,b,Aeq,beq,lb,ub,nonlcon,options)
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
>>
x(1) = V1
x(2) = V2
x(3) = x13
So x, lb and ub must be vectors of length 3.
You specify x and lb and ub each with two elements: this tells fmincon that you want to optimize two values, so fmincon therefore calls your objective function with an input argument with two elements. Then inside that objective function you try to access the third element of that two-element vector. Thus the error.
You need to decide if are you optimzing TWO or THREE values:
TWO: You need to parameterize your function, either with an aononymous function or a nested function:
For example, supply the x13 as a second input argument and use an anonymous function as the objective function, exactly as the documentation shows.
THREE: define x and lb and ub each with three elements.
okay, sir. I'll try to do that, but I'm new to matlab. If I have questions, can I ask you too?
Stephen, lb and ub are adjusted to match the size of x0; the number of variables is never derived from lb and ub.
x0 was given as [20, 30] which is only two values.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!