Error using function. I get the error Array indices must be positive integers or logical values.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Niveditha Kumar
le 16 Juil 2019
Commenté : Walter Roberson
le 16 Juil 2019
clear all
clc
format short
lambda = 0.9;
ES = zeros(5);
SCV = ES; %initializing SCV
TH = ES; %initializing TH
EW = ES; %initializing EW
% a = 1:5;
% b = a;
% nodes = create_nodes(a, b);
for i=1:5
for j=1:5
ES(i,j) = 5-abs(i-3)-abs(j-3); %compute service time
SCV(i,j) = abs((i-3)*(j-3)); %compute scv
end
end
EW(1,1) = ES(1,1)*(1+0.5*(1+SCV(1,1))*0.9*1*ES(1,1)/(1-0.9*1*ES(1,1)));
V(1,1) = 1;
V(1,2) = 0.5;
EW(1,2) = ES(1,2)*(1+0.5*(1+SCV(1,2))*0.9*V(1,2)*ES(1,2)/(1-0.9*V(1,2)*ES(1,2)));
x(4) = 0.5;
x(7) = 0.5;
x(9) = 0.5;
V_init = 0.5;
for i = 3:5 % compute sojourn time for row 1
V_fun{i} = @(x) V_init*x(i-2);
EW_fun{i} = @(x) ES(1,i)*(1+0.5*(1+SCV(1,i))*0.9*V_fun{i}*ES(1,i)/(1-0.9*V_fun{i}*ES(1,i)));
V_init = @(x) V_fun{i};
fun{i} = @(x) V_fun{i}*EW_fun{i};
end
V_init = 1;
for j = 6:8 % compute sojourn time for row 2
V_fun{j} = @(x) 0.5*(1-x(1))*V_init*x(j-2);
EW_fun{j} = @(x) ES(2,j-3)*(1+0.5*(1+SCV(2,j-3))*0.9*V_fun{j}*ES(2,j-3)/(1-0.9*V_fun{j}*ES(2,j-3)));
V_init = @(x) V_fun(j);
% fun{j} = @(x) V_fun(j)*EW_fun{j};
end
V_init = 0.5*x(1)*(1-x(2))*(1-x(5));
for i = 9:10 % compute sojourn time for row 3
V_fun{i} = @(x) V_init*x(i-2);
EW_fun{i} = @(x) ES(3,i-5)*(1+0.5*(1+SCV(1,i-5))*0.9*V_fun{i}*ES(1,i-5)/(1-0.9*V_fun{i}*ES(1,i-5)));
V_init = @(x) V_fun{i};
% fun{i} = @(x) V_fun{i}*EW_fun{i};
end
V_fun{11} = @(x) 0.5*x(1)*x(2)*(1-x(3))*(1-x(6))*(1-x(8))*x(9);
EW_fun{11} = @(x) ES(4,5)*(1+0.5*(1+SCV(4,5))*0.9*V_fun{11}*ES(4,5)/(1-0.9*V_fun{11}*ES(4,5)));
% fun{11} = @(x) V_fun{11}*EW_fun{11};
V_fun{12} = @(x) 0.5*x(1)*x(2)*x(3)*x(10);
EW_fun{12} = @(x) ES(5,5)*(1+0.5*(1+SCV(5,5))*0.9*V_fun{12}*ES(5,5)/(1-0.9*V_fun{12}*ES(5,5)));
% fun{12} = @(x) V_fun{12}*EW_fun{12};
% funMin = @(x) sum([fun{:}])
V_fun{1} = V(1,1);
V_fun{2} = V(1,2);
EW_fun{1} = EW(1,1);
EW_fun{2} = EW(1,2);
% fun{1} = @(x) V_fun{1}*EW_fun{1};
% fun{2} = @(x) V_fun{2}*EW_fun{2}
fun = @(x) cellfun(@(g,h)(g(x)*(h(x))),V_fun,EW_fun);
funMin = @(x) sum([fun{:}]);
x0 = zeros(1,10);
Aeq = [];
beq = [];
lb = zeros(1,10);
ub = ones(1,10);
x = fmincon(fun,x0,[],[],Aeq,beq,lb,ub);
0 commentaires
Réponse acceptée
Walter Roberson
le 16 Juil 2019
You have V(1,1) and you assign that to V_fun{1}. We do not know the datatype of V but we do know that indexing with () can never return a function handle, so if V is a variable then V_fun{1} cannot be a function handle. However you use it as if it were a function handle in your g(x) reference in your cellfun.
It is possible but not especially likely that V is a function of two arguments that returns a function handle: that could work, but I doubt it is the situation you have.
More likely, V(1,1) is either a numeric value or else a scalar cell array. Then when you use g(x) that would be a request to index the scalar at location x which is not likely to work.
My guess is that V is a cell array of function handles and that you should be assigning V{1,1} instead of V(1,1)
... Notice how much shorter this reply could have been if you had given us more information.
1 commentaire
Walter Roberson
le 16 Juil 2019
V(1,1) = 1;
V(1,2) = 0.5;
But you try to use those as function handles. You assign the numeric values into the cell and then pull entries from the cell, getting the numeric values, and try to g(x) where g is the numeric value.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Descriptive Statistics and Visualization dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!