Azzera filtri
Azzera filtri

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.

4 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Githin John
Githin John il 3 Feb 2020
Modificato: Githin John il 3 Feb 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);

Più risposte (1)

Walter Roberson
Walter Roberson il 3 Feb 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)

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by