MATLAB Answers

Akbar
0

Need help with symbolic variables (if else statement)

Asked by Akbar
on 13 Jul 2018 at 13:55
Latest activity Edited by Akbar
on 18 Jul 2018 at 12:22
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 Comments

...and what is stored in a? what does a equal?

Just one number

Ok, see answers. If 'a' must be 'sym' Walter's answer is best. If 'a' can be converted to 'double', see my answer.

Sign in to comment.

Products


Release

R2017b

2 Answers

Answer by Walter Roberson
on 13 Jul 2018 at 18:05
 Accepted Answer

You need to use piecewise() for this purpose.

  0 Comments

Sign in to comment.


Answer by Adam Danz
on 13 Jul 2018 at 19:11

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 Comments

Thank you very much Walter for trying to help out. I dont want to share files on public. I can send to your email if you provide it.

Sorry, I only answer public questions.

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

Sign in to comment.