필터 지우기
필터 지우기

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 (최근 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 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