- Define the Objective Function Correctly: Your objective function needs to be defined properly to be used with fmincon. This function should accept a single vector argument [x1, x2, x3] and return the scalar value to be minimized. Avoid using global variables to pass data into your objective function.
- Optimization with fmincon: Use fmincon correctly by defining constraints and the objective function in a way that MATLAB expects.
An optimization with fmincon
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello everyone, sorry for the inconvenience. I would have a problem with the optimization via the fmincon function. I wanted to ask, kindly, if anyone could help me. I need to get 3 parameters (x1, x2, x3 = 0.4242 0.2928 0.2830; max = 319). Thanks to who will answer me. Good day.
%% FITTING MODEL
%-----------------------------------------------
clc, clear
global x1s x2s x3s rs
global betax
data = [100 0 0 265
0 100 0 170
0 0 100 119
50 50 0 252
50 0 50 247
0 50 50 222
66.67 16.67 16.67 310
16.67 66.67 16.67 244
16.67 16.67 66.67 239
33.33 33.33 33.33 322
100 0 0 257
0 100 0 181
0 0 100 127];
x1s = data(:,1)/100; % values of x1
x2s = data(:,2)/100; % values of x2
x3s = data(:,3)/100; % values of x1
rs = data(:,4); % values of residual
% function
beta0 = [62.8 173.1 122.6 131.0 228.2 274.2 1597.9];
beta=lsqnonlin('funzione',beta0);
for j=1:length(rs)
rc(j)= modello(x1s(j),x2s(j),x3s(j));
res(j) = rc(j) - rs(j);
end
%% data calculation
betax = beta;
ndat=10000;
for j=1:ndat
x1c(j) = rand(1,1);
x2c(j) = rand(1,1)*(1-x1c(j));
x3c(j) = 1-x1c(j)-x2c(j);
risp(j) = modello(x1c(j),x2c(j),x3c(j));
end
%% diagram
figure(1);
% plot of data
% colour
% test surface response
colormap (jet)
[hg,htick,hcb] = tersurf(x1c,x2c,x3c,risp);
% axes label
hlabels = terlabel('x1','x2','x3');
%% experiment
figure(2)
%-- Plot axis
[h,hg,htick] = terplot;
%-- Plot data
hter = ternaryc(x1s,x2s,x3s);
set(hter,'marker','o','markerfacecolor','blue','markersize','8')
hlabels = terlabel('x1','x2','x3');
%% residual plot
figure(3)
plot(x1s,res,'ro')
xlabel('x1')
ylabel('residui')
grid
%% NORMAL PROBABILITY GRAPH
figure(4)
res_sort = sort(res);
for j = 1:length(res)
xj(j) = j/(length(res)-1);
end
semilogy(res_sort,xj,'ko')
grid
%% SEARCH FOR THE MINIMUM
x0 = [0.3 0.3 0.3]; % punto centrale
lb = [0 0 0]; % limite inferiore
ub = [1 1 1]; % limite superiore
Aeq = [1 1 1];
beq = 1;
y = fmincon('funz',x0,[],[],Aeq,beq,lb,ub)
modello(xmax(1),xmax(2),xmax(3))
function [z] = funz (x1s,x2s,x3s)
global x1s x2s x3s
z = 62.8.*x1s+173.1.*x2s+122.6.*x3s+131.0.*x1s*x2s+228.2.*x1s*x3s+274.2.*x2s*x3s+1597.9.*x1s*x2s*x3s;
end
0 comentarios
Respuestas (1)
Nipun
el 22 de Mayo de 2024
Hi Silver,
I understand that you are trying to optimize a model using the fmincon function in MATLAB to obtain three parameters (x1, x2, x3) that minimize or fit a given set of data. You have a complex script that involves global variables, data fitting using lsqnonlin, and plotting results, but your main focus seems to be on optimizing parameters for a model using fmincon.
To streamline and correct your approach, especially for the optimization part, here's a concise guide and code snippet:
Given your scenario, here's how you can adjust the relevant parts of your script, particularly focusing on the definition of the objective function and the call to fmincon:
Objective Function (funz.m)
Create a separate file named funz.m with the following content:
function z = funz(x)
% Assuming x is a vector [x1, x2, x3]
z = 62.8*x(1) + 173.1*x(2) + 122.6*x(3) + 131.0*x(1)*x(2) + 228.2*x(1)*x(3) + 274.2*x(2)*x(3) + 1597.9*x(1)*x(2)*x(3);
end
Call to fmincon
Adjust your script to correctly call fmincon with the objective function, initial guess, bounds, and constraints:
% Initial guess, bounds, and linear equality constraints
x0 = [0.3, 0.3, 0.3]; % Initial guess
lb = [0, 0, 0]; % Lower bounds
ub = [1, 1, 1]; % Upper bounds
Aeq = [1, 1, 1]; % Linear equality constraints coefficients
beq = 1; % Linear equality constraints target value
% Objective function handle
objFun = @funz;
% Optimization
options = optimoptions('fmincon', 'Display', 'iter', 'Algorithm', 'sqp');
[x_opt, fval] = fmincon(objFun, x0, [], [], Aeq, beq, lb, ub, [], options);
% Display the optimized parameters and the function value at the optimum
fprintf('Optimal parameters: x1 = %.4f, x2 = %.4f, x3 = %.4f\n', x_opt(1), x_opt(2), x_opt(3));
fprintf('Minimum function value: %.4f\n', fval);
This revised approach should help you correctly use fmincon for your optimization problem. Remember to replace the modello and plotting sections with your specific functions and plotting commands as needed.
Hope this helps.
Regards,
Nipun
0 comentarios
Ver también
Categorías
Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!