Solving Inequalities greater than and smaller than at the same time

44 visualizaciones (últimos 30 días)
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):
-15<10*S<15
I did the following Matlab Code:
clear all;
close all;
clc;
syms S real;
negative=solve(-15<10*S,S);
positive=solve(10*S<15,S);
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 23 de Ag. de 2019
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().
  3 comentarios
Walter Roberson
Walter Roberson el 23 de Ag. de 2019
I suspect you are using a slightly older version. Try
feval(symengine, 'solve', -15<10*S & 10*S <15)
Ali Tawfik
Ali Tawfik el 23 de Ag. de 2019
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;
clc;
syms x y;
an=solve(-15== 10*x+2*y,y);
ap=solve(10*x+2*y ==20,y);
yin=[an,ap]
damontwo % Name of the function
%% function
function damontwo(x)
x=input('please enter number ')
yin(:,1)=input('Enter equation to solve ')
end
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 ?
Thanks,

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by