- Ensure that fminsearchbnd is available in your MATLAB path. If it's not, you can download it from the MATLAB File Exchange.
- The success of the optimization heavily depends on the choice of initial parameters, the nature of your cost function, and the bounds you set for the parameters. You might need to experiment with these to find the best results.
Optimization of 5 parameters within a for loop and if statement
2 次查看(过去 30 天)
显示 更早的评论
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/740624/image.jpeg)
I would apreciate to much any idea/help. Thanks in advance!
0 个评论
回答(1 个)
Shubham
2024-5-17
Hi Joannes,
To optimize parameters such as gain, a1, a2, b1, and b2 in MATLAB using fminsearch or fminsearchbnd (for bounded optimization), you need to define a cost function that quantifies the "dispersion" or variability of your results, which you want to minimize. In your case, you mentioned wanting to reduce the results dispersion, which can be quantified using the standard deviation (std) of the results.
Here's a step-by-step guide on how to set this up:
1. Define the Cost Function
Your cost function should execute the core of your script (the for-loop and calculations) and return a single scalar value representing the cost. In this case, the cost could be the standard deviation of the mean output across all iterations, as you're interested in reducing dispersion.
2. Use fminsearch or fminsearchbnd
Since you have constraints on the parameters (gain, a1 must be in the range [0, 10]), fminsearchbnd is more appropriate. fminsearchbnd is not built-in MATLAB but is available from the MATLAB File Exchange. It behaves similarly to fminsearch but allows you to specify bounds for each parameter. Here is the link: https://in.mathworks.com/matlabcentral/fileexchange/8277-fminsearchbnd-fminsearchcon
Example Implementation
First, you need to modify your script to include the optimization parameters as inputs to a function and the cost (e.g., standard deviation of the output) as the output. Here's an example based on your description:
function cost = myCostFunction(params)
% Unpack parameters
gain = params(1);
a1 = params(2);
a2 = params(3);
b1 = params(4);
b2 = params(5);
% Your existing script here, modified to use the parameters above
% and to calculate the output 'out' based on those parameters
% Assuming 'out' is calculated within this script
% Calculate the cost as the standard deviation of the mean output
cost = std(mean(out, 2));
end
Next, use fminsearchbnd to minimize this cost function. You need to specify initial guesses for your parameters and their bounds:
% Initial parameter guesses
initialParams = [0.02, 2, 6, 4.350, 1.9]; % Example initial guesses
% Lower and upper bounds for each parameter
lb = [0, 0, -Inf, -Inf, -Inf]; % Lower bounds for gain and a1
ub = [10, 10, Inf, Inf, Inf]; % Upper bounds for gain and a1
% Optimize
opts = optimset('Display', 'iter'); % Show iterations
[optimizedParams, fval] = fminsearchbnd(@myCostFunction, initialParams, lb, ub, opts);
fprintf('Optimized Parameters:\n');
disp(optimizedParams);
Make sure to replace the placeholder Your existing script here with the actual calculations from your script, ensuring that it uses the parameters gain, a1, a2, b1, and b2 from the params input. This setup allows fminsearchbnd to adjust these parameters to minimize the cost function, which is designed to reduce the dispersion of your results.
Note
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Problem-Based Optimization Setup 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!