How to sole the solve the error " Conversion to logical from sym is not possible" in my program?

7 views (last 30 days)
esmail chogoli
esmail chogoli on 22 Jan 2016
Answered: Walter Roberson on 22 Jan 2016
Hi everyone.
I want to solve the following optimization problem, But my program has a error please help me if it is possible for you...
my optimization problem and loss function ..
And I writing the code of this program and try to solve that with YALMIP package...in my program K=E(ρ(z)) , c=1 , T=5 , z=randn and ρ is the loss function.thanks
my progra code is :
clc;
clear;
close all;
ab=[ 0.0181 -0.0041 0.0040 0.0373 0.0580 0.0009 0.0250 -0.0009 0.0205 0.0302
0.0290 0.0260 0.0260 -0.0234 0.0250 -0.0136 -0.0311 -0.0451 0.0576 -0.0288];
% I use a YALMIP package for solve this problem
T=5;
z=randn;
syms f(x) g(y) integer
f(x)=(1/6)*((x^2 - 1)^3 + 1);
g(y)=(1/6);
if abs(z)<=1
K=mean(f(z));
else
K=mean(g(z));
end
syms a b c d e l
E =(( a*e + b*l -d )/c);
a=sdpvar(1,1);
% a b c d are the variable defined in package
b=sdpvar(1,1);
c=sdpvar(1,1);
d=sdpvar(1,1);
F=0;
for i=1:5
E2=subs(E,[e,l],[ab(1,i),ab(2,i)]);
if abs(E2)<=1;% my problem starts here
F=F+f(E3);
else
F=F+g(E2);
end
end
% The main template package in accordance with the instructions
f1=c;
co=[F==K*T];
co=co+[a+b==1];
options=sdpsettings('solver','BMIBNB');
sol=optimize(co,f1,options);
if sol.problem == 0
solution = value(x)
else
display('Hmm, something went wrong!');
sol.info
yalmiperror(sol.problem)
end

Answers (1)

Walter Roberson
Walter Roberson on 22 Jan 2016
A = sdpvar(1,1); % a b c d are the variable defined in package
B = sdpvar(1,1);
C = sdpvar(1,1);
D = sdpvar(1,1);
...
E2 = double( subs(E, {e, l, a, b, c, d}, {ab(1,i), ab(2,i), A, B, C, D}) );
Remember, when a formula has been defined with a symbol, and the symbol is later assigned a numeric value, then the formula does not gain the numeric value until you request to subs() it in. Just like if you have
X = 2
A = X + 1
X = 4
then A does not suddenly become 4+1,
sym X
A = X + 1
X = 4
then A does not suddenly become 4+1

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!