Need help with symbolic variables (if else statement)

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

Your variable 'a' is probably a symbolic variable. To confirm,
class(a)
What does a equal?
...and what is stored in a? what does a equal?
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.

 Accepted Answer

You need to use piecewise() for this purpose.

More Answers (1)

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

If a could be converted to double then "if" would not have complained about it, unless perhaps in R2011a and earlier (I would need to check whether the earlier releases did the automatic conversion.)
With all current versions,
if sym(5) > 3
disp('yep');
else
disp('no');
end
will work fine.
The problem arises when you try to compare a symbolic expression that contains values that double() will not work with, such as if it contains an unresolved symbolic variable or if it contains an int() that is divergent, or a limit() that MATLAB cannot decide. For those cases, you need piecewise()
Akbar, Walter's correct and your 'a' variable probably isn't a simple number as you mentioned earlier. If Walter's answer didn't solve your problem, you need to provide the value of 'a' in order to understand what's going on.
Akbar
Akbar on 13 Jul 2018
Edited: Akbar on 13 Jul 2018
Thank you. I will try piecewise() on Monday, not at office now. However I tried with heaviside(), it seems to be working, but throws error at some other part of my codes, during parsing.
Basically i am trying to solve a differential equation using this solver which uses symbolics. The problem is that i have the above mentioned 'if else' statement inside my differential equation.
Watch out for the definition of heaviside when the input argument is exactly 0. See sympref('HeavisideAtOrigin')
Note that if you are taking these symbolic equations and converting them for use with the numeric ode*() routines (such as by using odeFunction()) then you can have problems. The numeric routines such as ode45() require that the functions be twice differentiable, so they cannot handle discontinuities such as you are programming. When you use routines such as ode45(), you need to stop the integration at each discontinuity and then resume with a new ode*() call. If the discontinuity is time based, you can do that by using the tspan to integrate for a limited time, and then call again for the next time span. If the discontinuity is not time based, then you would need to program event functions for this purpose.
Thank you for the advice, Walter. I will look into that.
ode15s solves this stiff ODE correctly. I have set tspan to [0 170].
f1 = b; % f1 is a new variable here
f1 = f1 * heaviside(a);
switch1 = f1;
% FYI: with given Initial conditions a = 8.8909e-04
But this solver doesn't solve it. And Im not sure why. This is the error I get:
Reference to non-existent field 'root'.
Error in amimodel/parseModel (line 99)
this.recompile =
not(strcmp(this.HTable(1).root,HTable.root));
Error in amiwrap (line 166)
model.parseModel();
Error in Solve_Realistic_Model (line 152)
amiwrap('myamici',model,cd);
Could you post the full code that calls amiwrap? I have downloaded the toolbox but it is easier to examine when there is a particular example to test with.
Akbar
Akbar on 17 Jul 2018
Edited: Akbar on 17 Jul 2018
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.
Akbar
Akbar on 18 Jul 2018
Edited: Akbar on 18 Jul 2018
Ok. I have just solved my issue by substituting if else statement with tanh().

Sign in to comment.

Products

Release

R2017b

Asked:

on 13 Jul 2018

Edited:

on 18 Jul 2018

Community Treasure Hunt

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

Start Hunting!