how to combine symbolics and if statement?
47 views (last 30 days)
Karthik Nimishakavi on 15 Oct 2016
Hello, My function is like this
y=x+2 for 0<=x<=10
y = x^2 for 10<x<=500 and x belongs to 0,500.
so I tried like this
if x>=0 && x<=10
but i am getting an error saying
"Conversion to logical from sym is not possible"
"Operands to the || and && operators must be convertible to logical scalar values".
Please help me how to overcome this.
Walter Roberson on 15 Oct 2016
You cannot use if together with symbolic expressions.
You should be considering using numeric expressions together with logical indexing. See though http://www.mathworks.com/matlabcentral/answers/301165-ode-suite-for-solving-switched-systems#comment_397941 for a discussion on hidden problems with one way of coding that.
You could also code the obscure
evalin(symengine, 'piecewise([0 <= x and x <= 10, x+2], [10 < x and x <= 500, x^2])')
but you will find that it probably will not be of any use to you.
More Answers (1)
Karan Gill on 20 Oct 2016
Edited: Karan Gill on 17 Oct 2017