Asked by Akbar
on 13 Jul 2018

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.

Answer by Walter Roberson
on 13 Jul 2018

Accepted Answer

You need to use piecewise() for this purpose.

Sign in to comment.

Answer by Adam Danz
on 13 Jul 2018

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.

Akbar
on 17 Jul 2018

Walter Roberson
on 17 Jul 2018

Sorry, I only answer public questions.

Akbar
on 18 Jul 2018

Ok. I have just solved my issue by substituting if else statement with tanh().

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 5 Comments

## Adam Danz (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/410246-need-help-with-symbolic-variables-if-else-statement#comment_589066

## Akbar (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/410246-need-help-with-symbolic-variables-if-else-statement#comment_589089

## Adam Danz (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/410246-need-help-with-symbolic-variables-if-else-statement#comment_589096

## Akbar (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/410246-need-help-with-symbolic-variables-if-else-statement#comment_589121

## Adam Danz (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/410246-need-help-with-symbolic-variables-if-else-statement#comment_589125

Sign in to comment.