Unrecognized variable, want to create a list of variable
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Paul AGAMENNONE
am 21 Nov. 2022
Bearbeitet: Paul AGAMENNONE
am 21 Nov. 2022
Hello,
I'm trying to run an optimization model but I always get the error "Unrecognized function or variable 'c1_std'.
I want to call d = (c1_std, c2_std, c3_std) to run my optimization model and find the best parameters. How should I define d to call d(1)=c1_std in my different functions?
I'm not an expert in Matlab so please excuse me if my code is quite rude.
Thank you in advance.
Paul
muL = 2000;
sigL = 200;
R1 = 1-9.92*10^-5;
R2 = 1-1.2696*10^-4;
R3 = 1-3.87*10^-6;
d = [c1_std,c2_std,c3_std];
Sr1_min = sqrt(((((1.5-1)*muL)/norminv(R1))^2)-(sigL)^2);
Sr1_max = sqrt(((((2.5-1)*muL)/norminv(R1))^2)-(sigL)^2);
Sr2_min = sqrt(((((1.5-1)*muL)/norminv(R2))^2)-(sigL)^2);
Sr2_max = sqrt(((((2.5-1)*muL)/norminv(R2))^2)-(sigL)^2);
Sr3_min = sqrt(((((1.5-1)*muL)/norminv(R3))^2)-(sigL)^2);
Sr3_max = sqrt(((((2.5-1)*muL)/norminv(R3))^2)-(sigL)^2);
sqrt((((1.5-1)*muL)/norminv(R1)^2)-(sigL)^2)
norminv(R1)
lb = [Sr1_min,Sr2_min,Sr3_min];
ub = [Sr1_max,Sr2_max,Sr3_max];
A = [];
B = [];
Aeq = [];
Beq = [];
d0 = (lb+ub)/2;
fun = @(d) parameterfun(d,muL,sigL);
const = @(d) nonlcon(d,muL,sigL);
[d,fval] = fmincon(fun,d0,A,B,Aeq,Beq,lb,ub,const);
function Rs = parameterfun(d,muL,sigL)
muL = 2000;
sigL = 200;
R1 = 1-9.92*10^-5;
R2 = 1-1.2696*10^-4;
R3 = 1-3.87*10^-6;
d = [c1_std,c2_std,c3_std];
mu_Sr1 = muL+norminv(R1)*sqrt((sigL)^2+(d(1))^2);
mu_Sr2 = muL+norminv(R2)*sqrt((sigL)^2+(d(2))^2);
mu_Sr3 = muL+norminv(R3)*sqrt((sigL)^2+(d(3))^2);
Y1_mean = muL-mu_Sr1;
Y2_mean = muL-mu_Sr2;
Y3_mean = muL-mu_Sr3;
Y1_std = sqrt((d(1))^2+(sigL)^2);
Y2_std = sqrt((d(2))^2+(sigL)^2);
Y3_std = sqrt((d(3))^2+(sigL)^2);
Y_mean = [Y1_mean Y2_mean Y3_mean];
Y_std = [(Y1_std^2) (sigL)^2 (sigL)^2; (sigL)^2 (Y2_std)^2 (sigL)^2; (sigL)^2 (sigL)^2 (Y3_std)^2];
y = zeros(size(d));
Rs = mvncdf(y, Y_mean, Y_std);
end
function [c,ceq] = nonlcon(d,muL,sigL)
muL = 2000;
sigL = 200;
R1 = 1-9.92*10^-5;
R2 = 1-1.2696*10^-4;
R3 = 1-3.87*10^-6;
d = [c1_std,c2_std,c3_std];
c(1) = ns_min(1) - ((muL+norminv(R1)*sqrt((d(1)^2)+(L_std^2)))/muL);
c(2) = ns_min(2) - ((muL+norminv(R2)*sqrt((d(2)^2)+(L_std^2)))/muL);
c(3) = ns_min(3) - ((muL+norminv(R3)*sqrt((d(3)^2)+(L_std^2)))/muL);
c(4) = ((muL+norminv(R1)*sqrt((d(1)^2)+(L_std^2)))/muL) - ns_max(1);
c(5) = ((muL+norminv(R2)*sqrt((d(2)^2)+(L_std^2)))/muL) - ns_max(2);
c(6) = ((muL+norminv(R3)*sqrt((d(3)^2)+(L_std^2)))/muL) - ns_max(3);
c(7) = c_min(1) - (c1_std/((muL+norminv(R1)*sqrt((d(1)^2)+(L_std^2)))));
c(8) = c_min(2) - (c2_std/((muL+norminv(R2)*sqrt((d(2)^2)+(L_std^2)))));
c(9) = c_min(3) - (c3_std/((muL+norminv(R3)*sqrt((d(3)^2)+(L_std^2)))));
c(10) = (c1_std/((muL+norminv(R1)*sqrt((d(1)^2)+(L_std^2))))) - c_max(1);
c(11) = (c2_std/((muL+norminv(R2)*sqrt((d(2)^2)+(L_std^2))))) - c_max(2);
c(12) = (c3_std/((muL+norminv(R3)*sqrt((d(3)^2)+(L_std^2))))) - c_max(3);
format long
c
ceq = [];
end
2 Kommentare
Jan
am 21 Nov. 2022
In the line:
d = [c1_std,c2_std,c3_std];
neither Matlab nor the readers in the forum can guess, what c1_std, c2_std, c3_std is. What are these variables or functions? Where are they defined?
Akzeptierte Antwort
Stephen23
am 21 Nov. 2022
Bearbeitet: Stephen23
am 21 Nov. 2022
The basic problem appears to be your attempt to re-define these parameters again inside every function, thus obliterating the input values. The simple solution is to define them only once and then pass them as input arguments:
The d value seems to be what you are trying to optimize, so you should not specify any values for that, because the d values are provided by FMINCON.
muL = 2000;
sigL = 200;
R1 = 1-9.92*10^-5;
R2 = 1-1.2696*10^-4;
R3 = 1-3.87*10^-6;
Sr1_min = sqrt(((((1.5-1)*muL)/norminv(R1))^2)-(sigL)^2);
Sr1_max = sqrt(((((2.5-1)*muL)/norminv(R1))^2)-(sigL)^2);
Sr2_min = sqrt(((((1.5-1)*muL)/norminv(R2))^2)-(sigL)^2);
Sr2_max = sqrt(((((2.5-1)*muL)/norminv(R2))^2)-(sigL)^2);
Sr3_min = sqrt(((((1.5-1)*muL)/norminv(R3))^2)-(sigL)^2);
Sr3_max = sqrt(((((2.5-1)*muL)/norminv(R3))^2)-(sigL)^2);
lb = [Sr1_min,Sr2_min,Sr3_min];
ub = [Sr1_max,Sr2_max,Sr3_max];
A = [];
B = [];
Aeq = [];
Beq = [];
d0 = (lb+ub)/2;
fun = @(d) parameterfun(d,muL,sigL,R1,R2,R3);
const = @(d) nonlcon(d,muL,sigL,R1,R2,R3);
[d,fval] = fmincon(fun,d0,A,B,Aeq,Beq,lb,ub,const)
That works without error. Only you can check if it makes sense.
function Rs = parameterfun(d,muL,sigL,R1,R2,R3)
%
mu_Sr1 = muL+norminv(R1)*sqrt((sigL)^2+(d(1))^2);
mu_Sr2 = muL+norminv(R2)*sqrt((sigL)^2+(d(2))^2);
mu_Sr3 = muL+norminv(R3)*sqrt((sigL)^2+(d(3))^2);
%
Y1_mean = muL-mu_Sr1;
Y2_mean = muL-mu_Sr2;
Y3_mean = muL-mu_Sr3;
%
Y1_std = sqrt((d(1))^2+(sigL)^2);
Y2_std = sqrt((d(2))^2+(sigL)^2);
Y3_std = sqrt((d(3))^2+(sigL)^2);
%
Y_mean = [Y1_mean Y2_mean Y3_mean];
Y_std = [(Y1_std^2) (sigL)^2 (sigL)^2; (sigL)^2 (Y2_std)^2 (sigL)^2; (sigL)^2 (sigL)^2 (Y3_std)^2];
%
y = zeros(size(d));
Rs = mvncdf(y, Y_mean, Y_std);
%
end
function [c,ceq] = nonlcon(d,muL,sigL,R1,R2,R3)
%
% You did not define these, so I will presume all have value 1:
L_std = 1;
ns_min = ones(1,3);
ns_max = ones(1,3);
c_min = ones(1,3);
c_max = ones(1,3);
c1_std = 1;
c2_std = 1;
c3_std = 1;
%
c(1) = ns_min(1) - ((muL+norminv(R1)*sqrt((d(1)^2)+(L_std^2)))/muL);
c(2) = ns_min(2) - ((muL+norminv(R2)*sqrt((d(2)^2)+(L_std^2)))/muL);
c(3) = ns_min(3) - ((muL+norminv(R3)*sqrt((d(3)^2)+(L_std^2)))/muL);
c(4) = ((muL+norminv(R1)*sqrt((d(1)^2)+(L_std^2)))/muL) - ns_max(1);
c(5) = ((muL+norminv(R2)*sqrt((d(2)^2)+(L_std^2)))/muL) - ns_max(2);
c(6) = ((muL+norminv(R3)*sqrt((d(3)^2)+(L_std^2)))/muL) - ns_max(3);
c(7) = c_min(1) - (c1_std/((muL+norminv(R1)*sqrt((d(1)^2)+(L_std^2)))));
c(8) = c_min(2) - (c2_std/((muL+norminv(R2)*sqrt((d(2)^2)+(L_std^2)))));
c(9) = c_min(3) - (c3_std/((muL+norminv(R3)*sqrt((d(3)^2)+(L_std^2)))));
c(10) = (c1_std/((muL+norminv(R1)*sqrt((d(1)^2)+(L_std^2))))) - c_max(1);
c(11) = (c2_std/((muL+norminv(R2)*sqrt((d(2)^2)+(L_std^2))))) - c_max(2);
c(12) = (c3_std/((muL+norminv(R3)*sqrt((d(3)^2)+(L_std^2))))) - c_max(3);
%
ceq = [];
%
end
1 Kommentar
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Nonlinear Optimization finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!