Need help with symbolic variables (if else statement)

조회 수: 4 (최근 30일)
Akbar
Akbar 2018년 7월 13일
편집: Akbar 2018년 7월 18일
if a > 10^-8
switch1 = b;
else
switch1 = 0;
end
i get the following error:
Conversion to logical from sym is not possible.
at line:
if a > 10^-8
I Need the above code to work with symbolic variables.
  댓글 수: 5
Akbar
Akbar 2018년 7월 13일
Just one number
Adam Danz
Adam Danz 2018년 7월 13일
편집: Adam Danz 2018년 7월 13일
Ok, see answers. If 'a' must be 'sym' Walter's answer is best. If 'a' can be converted to 'double', see my answer.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2018년 7월 13일
You need to use piecewise() for this purpose.

추가 답변 (1개)

Adam Danz
Adam Danz 2018년 7월 13일
According to your comments, 'a' is a symbol symbolizing a scalar number. If that's the case, you can convert 'a' to numerical.
a = sym('3.14159');
a = double(a); % Convert to double
if a > 10^-8
...
else
...
end
If you prefer to keep 'a' in the sym class, use Walter's answer.
  댓글 수: 10
Walter Roberson
Walter Roberson 2018년 7월 17일
Sorry, I only answer public questions.
Akbar
Akbar 2018년 7월 18일
편집: Akbar 2018년 7월 18일
Ok. I have just solved my issue by substituting if else statement with tanh().

댓글을 달려면 로그인하십시오.

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by