## Optimization of 5 parameters within a for loop and if statement

Hello! I am trying to optimize some parameters (gain, a1, a2, b1 and b2) which are inside a for loop and contains some additional conditions. I am not sure if it is possible to do it with fminsearch (or fminsearchbnd due to gain = [0 10] and a1 = [0 10] boundaries) and how to define the fx cost function. I would like to reduce the results dispersion (see result plot below) as much as possible (for example with the std function).

% Constant input values

A = 1:1:10;

B = rand(10,10);

C = rand(10,10);

D = rand(10,10);

% Variables to optimize (gain-scheduling)

gain=0.02;

a1=2;

a2=6;

b1=4.350;

b2=1.9;

% Calculation loop

y = zeros(1,length(A));

y(1) = 0.21;

y1 = zeros(1,length(A));

y1(1) = 1;

y2 = zeros(size(B));

yy = zeros(1,length(A));

out = zeros(size(B));

for k = 1:size(D,2)

Ax = A(:,k);

Bx = B(:,k);

Cx = C(:,k);

for j = 1:length(A)-1

if A(j)<=a1

y(j+1) = gain+1/b1;

elseif A(j)<=a1

y(j+1) = gain+1/b2;

else

div = b2+(a2-A(j))*(b1-b2)/(a2-a1);

y(j+1) = gain+1/div;

end

y1(j+1) = (gain+1/b1)*1./y(j+1);

y2(1) = Bx(1);

yy(1) = Cx(1);

y2(j+1,1) = y1(j)-exp(-4)*y1(j)+exp(-6)*y2(j);

yy(j+1) = Cx(j+1)*y2(j+1);

end

out(:,k) = yy;

end

plot(A,mean(out),’k*’)

Result plot:

I would apreciate to much any idea/help. Thanks in advance!Hello! I am trying to optimize some parameters (gain, a1, a2, b1 and b2) which are inside a for loop and contains some additional conditions. I am not sure if it is possible to do it with fminsearch (or fminsearchbnd due to gain = [0 10] and a1 = [0 10] boundaries) and how to define the fx cost function. I would like to reduce the results dispersion (see result plot below) as much as possible (for example with the std function).

% Constant input values

A = 1:1:10;

B = rand(10,10);

C = rand(10,10);

D = rand(10,10);

% Variables to optimize (gain-scheduling)

gain=0.02;

a1=2;

a2=6;

b1=4.350;

b2=1.9;

% Calculation loop

y = zeros(1,length(A));

y(1) = 0.21;

y1 = zeros(1,length(A));

y1(1) = 1;

y2 = zeros(size(B));

yy = zeros(1,length(A));

out = zeros(size(B));

for k = 1:size(D,2)

Ax = A(:,k);

Bx = B(:,k);

Cx = C(:,k);

for j = 1:length(A)-1

if A(j)<=a1

y(j+1) = gain+1/b1;

elseif A(j)<=a1

y(j+1) = gain+1/b2;

else

div = b2+(a2-A(j))*(b1-b2)/(a2-a1);

y(j+1) = gain+1/div;

end

y1(j+1) = (gain+1/b1)*1./y(j+1);

y2(1) = Bx(1);

yy(1) = Cx(1);

y2(j+1,1) = y1(j)-exp(-4)*y1(j)+exp(-6)*y2(j);

yy(j+1) = Cx(j+1)*y2(j+1);

end

out(:,k) = yy;

end

plot(A,mean(out),’k*’)

Result plot:

I would apreciate to much any idea/help. Thanks in advance! Hello! I am trying to optimize some parameters (gain, a1, a2, b1 and b2) which are inside a for loop and contains some additional conditions. I am not sure if it is possible to do it with fminsearch (or fminsearchbnd due to gain = [0 10] and a1 = [0 10] boundaries) and how to define the fx cost function. I would like to reduce the results dispersion (see result plot below) as much as possible (for example with the std function).

% Constant input values

A = 1:1:10;

B = rand(10,10);

C = rand(10,10);

D = rand(10,10);

% Variables to optimize (gain-scheduling)

gain=0.02;

a1=2;

a2=6;

b1=4.350;

b2=1.9;

% Calculation loop

y = zeros(1,length(A));

y(1) = 0.21;

y1 = zeros(1,length(A));

y1(1) = 1;

y2 = zeros(size(B));

yy = zeros(1,length(A));

out = zeros(size(B));

for k = 1:size(D,2)

Ax = A(:,k);

Bx = B(:,k);

Cx = C(:,k);

for j = 1:length(A)-1

if A(j)<=a1

y(j+1) = gain+1/b1;

elseif A(j)<=a1

y(j+1) = gain+1/b2;

else

div = b2+(a2-A(j))*(b1-b2)/(a2-a1);

y(j+1) = gain+1/div;

end

y1(j+1) = (gain+1/b1)*1./y(j+1);

y2(1) = Bx(1);

yy(1) = Cx(1);

y2(j+1,1) = y1(j)-exp(-4)*y1(j)+exp(-6)*y2(j);

yy(j+1) = Cx(j+1)*y2(j+1);

end

out(:,k) = yy;

end

plot(A,mean(out),’k*’)

Result plot:

I would apreciate to much any idea/help. Thanks in advance! optimize, for loop, if statement MATLAB Answers — New Questions