Need help with symbolic variables (if else statement)

3 visualizaciones (últimos 30 días)
Akbar
Akbar el 13 de Jul. de 2018
Editada: Akbar el 18 de Jul. de 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.
  5 comentarios
Akbar
Akbar el 13 de Jul. de 2018
Just one number
Adam Danz
Adam Danz el 13 de Jul. de 2018
Editada: Adam Danz el 13 de Jul. de 2018
Ok, see answers. If 'a' must be 'sym' Walter's answer is best. If 'a' can be converted to 'double', see my answer.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 13 de Jul. de 2018
You need to use piecewise() for this purpose.

Más respuestas (1)

Adam Danz
Adam Danz el 13 de Jul. de 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.
  10 comentarios
Walter Roberson
Walter Roberson el 17 de Jul. de 2018
Sorry, I only answer public questions.
Akbar
Akbar el 18 de Jul. de 2018
Editada: Akbar el 18 de Jul. de 2018
Ok. I have just solved my issue by substituting if else statement with tanh().

Iniciar sesión para comentar.

Etiquetas

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by