Conversion to logical from sym is not possible. I want the first argument to be a logical argument which has && for non symbolic variables, In a piecewise function.

8 vues (au cours des 30 derniers jours)
Hi,
I'm trying to execute this piecewise function
a = 10;
b = 4;
c = 2;
mu1 = 1;
sig1 = 0.5;
pdf1(x) = piecewise( a == 0 && b < 0 , -(1/(b*exp((c1 - x + b*mu1)^2/(2*b^2*sig1^2))*sqrt(2*pi)*sig1)),...
a == 0 && b > 0, 1/(b*exp((c1 - x + b*mu1)^2/(2*b^2*sig1^2))*sqrt(2*pi)*sig1),...
a ~= 0 && (-b^2 + 4*a*c1 - 4*a*x) < 0,(exp((b - sqrt(b^2 - 4*a*c1 + 4*a*x) + 2*a*mu1)^2/(8*a^2*sig1^2)) + exp((b + sqrt(b^2 - 4*a*c1 + 4*a*x) + 2*a*mu1)^2/(8*a^2*sig1^2)))/(exp((b^2 + 2*a*b*mu1 + 2*a*(-c1 + x + a*mu1^2))/(2*a^2*sig1^2))*sqrt(2*pi)*sqrt(b^2 + 4*a*(-c1 + x))*sig1),...
0);
for which i get the error Conversion to logical from sym is not possible. I realised this is because the first argument cannot have && or ||, however for my application its critical for this to be present. Any help is appreciated. TIA.

Réponse acceptée

Githin John
Githin John le 3 Fév 2020
Modifié(e) : Githin John le 3 Fév 2020
You may try this code assuming it still satisfies your requirements.
syms a b c1 x;
assume(a==10);
assume(b==4);
assume(c1==2);
mu1 = 1;
sig1 = 0.5;
pdf1(x) = piecewise( a == 0 & b < 0 , -(1/(b*exp((c1 - x + b*mu1)^2/(2*b^2*sig1^2))*sqrt(2*pi)*sig1)),...
a == 0 & b > 0, 1/(b*exp((c1 - x + b*mu1)^2/(2*b^2*sig1^2))*sqrt(2*pi)*sig1),...
a ~= 0 & (-b^2 + 4*a*c1 - 4*a*x) < 0,(exp((b - sqrt(b^2 - 4*a*c1 + 4*a*x) + 2*a*mu1)^2/(8*a^2*sig1^2)) + exp((b + sqrt(b^2 - 4*a*c1 + 4*a*x) + 2*a*mu1)^2/(8*a^2*sig1^2)))/(exp((b^2 + 2*a*b*mu1 + 2*a*(-c1 + x + a*mu1^2))/(2*a^2*sig1^2))*sqrt(2*pi)*sqrt(b^2 + 4*a*(-c1 + x))*sig1),...
0);
or
syms a b c1 x;
mu1 = 1;
sig1 = 0.5;
pdf1(a,b,c1,x) = piecewise( a == 0 & b < 0 , -(1/(b*exp((c1 - x + b*mu1)^2/(2*b^2*sig1^2))*sqrt(2*pi)*sig1)),...
a == 0 & b > 0, 1/(b*exp((c1 - x + b*mu1)^2/(2*b^2*sig1^2))*sqrt(2*pi)*sig1),...
a ~= 0 & (-b^2 + 4*a*c1 - 4*a*x) < 0,(exp((b - sqrt(b^2 - 4*a*c1 + 4*a*x) + 2*a*mu1)^2/(8*a^2*sig1^2)) + exp((b + sqrt(b^2 - 4*a*c1 + 4*a*x) + 2*a*mu1)^2/(8*a^2*sig1^2)))/(exp((b^2 + 2*a*b*mu1 + 2*a*(-c1 + x + a*mu1^2))/(2*a^2*sig1^2))*sqrt(2*pi)*sqrt(b^2 + 4*a*(-c1 + x))*sig1),...
0);

Plus de réponses (1)

Walter Roberson
Walter Roberson le 3 Fév 2020
(a == 0 && b < 0) == sym(true)
piecewise cannot accept a logical value as the first parameter, but compare to symbolic true works.
sym(a == 0 && b < 0)
just might work, but in some code I was doing recently sym() of the condition failed and I had to compare to sym(true)
  1 commentaire
Josyula Gopala Krishna
Josyula Gopala Krishna le 3 Fév 2020
Strangely they don't work it throws the same error. Johns answer does work. He used
syms a b c1 x;
assume(a==10);
assume(b==4);
assume(c1==2);
This does work.

Connectez-vous pour commenter.

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by