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.

7 次查看(过去 30 天)
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.

采纳的回答

Githin John
Githin John 2020-2-3
编辑:Githin John 2020-2-3
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);

更多回答(1 个)

Walter Roberson
Walter Roberson 2020-2-3
(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)

类别

Help CenterFile Exchange 中查找有关 Assumptions 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by