Using fmincon to find the best combination of x1,x2,x3
1 view (last 30 days)
Show older comments
I would like to find the best combination of x1, x2, x3 that minimize my objective function.
Since the way to obtain the objective function is by using ODE, I write the separate M-file for this. Now, I have tried to use fmincon to find the best combination here, but it didn't work out.
x0=[2000,10000,1000];
A=[4.39 1.68 15.98];
b=[100000];
[x,fval]=fmincon(@allocation1,x0,A,b)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- For my M-file code:
function objective = allocation1 (x1,x2,x3)
%% compute r1,r2,r3 from the interrelationships
pop=1000;
% service coverage
DWS=0.1;
WST=0.1;
MSW=0.1;
% rate of convertion
rate_DWS=13.2;
rate_WST=50;
rate_MSW=4.38;
rate_DWS_MSW=0.05;
rate_WST_MSW=0.01;
rate_MSW_WST=0.10;
% total coverage
total_DWS=rate_DWS*pop;
total_WST=rate_WST*pop;
total_MSW=rate_MSW*pop;
% compute coverage
cov_DWS=DWS*pop*rate_DWS;
cov_WST=WST*pop*rate_WST;
cov_MSW=MSW*pop*rate_MSW;
% construct interrelationship matrix
I1 = [ 0 0 rate_DWS_MSW*cov_DWS; 0 0 rate_WST_MSW*cov_WST; 0 rate_MSW_WST*cov_MSW 0];
I2 = [ 0 0 rate_DWS_MSW*x1; 0 0 rate_WST_MSW*x2; 0 rate_MSW_WST*x3 0];
% compute the current state variables
r1_cur=(cov_DWS-sum(I1(:,1)))/total_DWS;
r2_cur=(cov_WST-sum(I1(:,2)))/total_WST;
r3_cur=(cov_MSW-sum(I1(:,3)))/total_MSW;
% compute the capacity expansion state variables
r1=r1_cur+((x1-sum(I2(:,1)))/total_DWS);
r2=r2_cur+((x2-sum(I2(:,2)))/total_WST);
r3=r3_cur+((x3-sum(I2(:,3)))/total_MSW);
%% function of nonlinear differential equation for disease incidents
function xprime = SIWDR2 (t,x)
% SIWDR model: the goal is the observe the behavior of system of % diferential equations
Lambda=0.05;
mu=0.04;
beta_i=0.005;
beta_w=0.001;
gamma=0.1;
alpha_1=1;
alpha_2=2;
theta_1=3;
theta_2=1;
psi=2;
xi_w=0.2; xi_d=0.2;
xprime=[Lambda-(1-r1)*beta_w*x(1)*x(3)-beta_i*x(1)*x(2)-mu*x(1);
(1-r1)*beta_w*x(1)*x(3)+beta_i*x(1)*x(2)-gamma*x(2)-mu*x(2);
(xi_w/alpha_1)*((1-r2)*alpha_1+theta_2)*x(2)+((xi_w*theta_2)/alpha_1)*x(1)+...
((xi_w*theta_2)/alpha_1)*x(5)+((1-r3)*psi*alpha_2*xi_w/(alpha_1*xi_d))*x(4)-...
xi_w*x(3);
(xi_d/alpha_2)*((1-r2)*alpha_2+theta_1)*x(2)+((xi_d*theta_1)/alpha_2)*x(1)+...
((xi_d*theta_1)/alpha_2)*x(5)-(xi_d*x(4));
gamma*x(2)-mu*x(5)];
end
%% Output of the allocation model
tspan=0:1:364;
x0=[0.8 0.1 1 1 0.1];
[t,x]=ode45(@SIWDR2,tspan,x0);
objective = sum(x(:,2))
end
Error using @(x1,x2,x3)allocation1(x1,x2,x3)
Not enough input arguments.
Error in fmincon (line 564)
initVals.f = feval(funfcn{3},X,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FMINCON cannot
continue.
0 Comments
Answers (1)
Alan Weiss
on 23 Apr 2015
Without reading your code in detail, it is clear that your main problem is that fmincon minimizes a SINGLE set of parameters. You simply have to rewrite allocation and your fmincon call to use a single variable x = [x1,x2,x3]:
function objective = allocation(x)
% Use x(1), x(2), and x(3) wherever you used x1, x2, or x3 before
Your objective for fmincon is @allocation or @(x)allocation(x).
Alan Weiss
MATLAB mathematical toolbox documentation
0 Comments
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!