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.

10 ビュー (過去 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)
  1 件のコメント
Josyula Gopala Krishna
Josyula Gopala Krishna 2020 年 2 月 3 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAssumptions についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by