MATLAB: How to vary an element in an equality matrix – optimisation program

equality matrixfminconfor loopoptimization

Hello,
I'm running an optimisation problem using fmincon, with two equality constraints and three optimising variables x1, x2, x3. As seen in the code below, I also wish to vary the value 'lcg' which appears in the first inequality equation.
I initially tried using a for-loop (as coded below), but Matlab gives me the following error:
"In an assignment A(I) = B, the number of elements in B and I must be the same."
I understand the error, but i'm not sure how to achieve varying the value of lcg in the inequality matrix. My approach to use a for-loop was based upon my need to see all the values of x1,x2,x3 and the function for each value of lcg after the optimisation process.
If anyone could offer some advice that would be great.
Thanks, Rob
i=1
for lcg = 19:0.1:20
Aeq(i) = [(lf-lcg(i))*(bf^2)/Af, (lt-lcg(i))*(bt^2)/At, (lw-lcg(i))*(bw^2)/Aw; Aw/Af*((bf/bw)^2), Aw/At*((bt/bw)^2), 1];
beq = [(bw/Aw*(bw^2/Aw*Cm0w)+(bf^2)/Af*Cm0f); Cl];
[x fval] = fmincon(@myfun,[x1(0);x2(0)0;x3(0)],[],[],Aeq,beq,lb,ub,[],options);
i=i+1

Best Answer

N=length(lcg);
x=zeros(3,N);
fval=zeros(1,N);
for i=1:N
Aeq = [(lf-lcg(i))*(bf^2)/Af, (lt-lcg(i))*(bt^2)/At, (lw-lcg(i))*(bw^2)/Aw; Aw/Af*((bf/bw)^2), Aw/At*((bt/bw)^2), 1];
beq = [(bw/Aw*(bw^2/Aw*Cm0w)+(bf^2)/Af*Cm0f); Cl];
[x(:,i) fval(i)] = fmincon(@myfun,[x1(0); x2(0) ;x3(0)],[],[],Aeq,beq,lb,ub,[],options);
end