MATLAB Answers


Solving Inequalities greater than and smaller than at the same time

Asked by Ali Tawfik on 23 Aug 2019
Latest activity Commented on by Ali Tawfik on 23 Aug 2019
Hi All,
I have been trying to solve the following inequalities I mean I'd like to divide the both sides by 10 then get (the left side and right side):
I did the following Matlab Code:
clear all;
close all;
syms S real;
numb(:,1)=[negative positive]
Is that possible to get the two possible results from one single line of comand ?, I mean could it possible to write the following equation as it's in Matlab -15<10S<15


Sign in to comment.

1 Answer

Answer by Walter Roberson
on 23 Aug 2019
 Accepted Answer

In sufficiently recent versions you can code
solve(-15< 10*S < 15, S)
However this is not documented and not recommended. It is safer to code
solve(-15 < 10*S & 10*S < 15, S)
However, what you cannot do using that interface is get out an inequality. You would like to get out -1.5 < S < 1.5 but you will not be able to get that in any direct way. When you use that interface, you will get out what I have in the past referred to as a "representative solution", such as "1" or "pi" -- a "nice" number that the system is true for. In the past I have posted a best-effort examination of exactly how it determines the representative solution to use.
What can you do? Well, two possibilities:
1) You can convert to equalities and solve on the two sides like you did except with == instead of <
negative = solve(-15==10*S,S);
positive = solve(10*S==15,S);
[negative, positive]
and be careful on the boundary condition.
2) More obscurely,
>> feval(symengine, 'solve', -15<10*S <15)
ans =
matrix([[S]]) in solvelib::cartesianPower(Dom::Interval(-3/2, 3/2), 1)
That is, the symbolic engine internally already calculates and returns intervals just as you might hope, and it is the nice MATLAB interface to the internal symbolic engine that turns them into the near-useless representative solutions. You can get at the boundary values returned by feval() by using children().


Hi Walter,
Thanks for your prompt reply.
I had tried the second option, I got the following error:
Error using mupadengine/feval (line 163)
Inequalities must not be multiplied by complex numbers.
Error in trying_modeling (line 7)
feval(symengine,'solve',-15 < 10*S < 15)
but when I try only feval(symengine,'solve',-15 < 10*S),
I got a solution of:
matrix([[S]]) in solvelib::cartesianPower(Dom::Interval(-3/2, Inf), 1)
Why can not I get like yours ?
I suspect you are using a slightly older version. Try
feval(symengine, 'solve', -15<10*S & 10*S <15)
Works now! that's great,
May I ask you another question ,
I am trying to build in function, including equation which is a result of another code,
I understand function is a separate script but I just need to do something smart, no to add the equation in every time ,
below is the code:
clear all;
syms x y;
an=solve(-15== 10*x+2*y,y);
ap=solve(10*x+2*y ==20,y);
damontwo % Name of the function
%% function
function damontwo(x)
x=input('please enter number ')
yin(:,1)=input('Enter equation to solve ')
When I type an: I got an error ?, however, when I type equation, it works, so could you please help me, how could I introduce that to the function ?

Sign in to comment.