I have this problem:
solution=feval(symengine, 'solve', '(abs(8/(k1+1)))<1', 'k1', 'Real')
solution = (7, Inf) union (-Inf, -9)
The solution is symbolic, but I need to have the two range in numeric form (array?) inside a script. Do I have to write about ten lines of code to do it? Thank you

 Respuesta aceptada

riccardo agnesi
riccardo agnesi el 2 de Ag. de 2019

0 votos

for my system I solved it this way alone.
I wanted something like that:
syms x
P = expand((x-3/2)*(x-5/4)*(x-1/10)*(x+4/8)*(x-12/5))
[ cond, condCell ] = inequality( P, 'x', 'P>0' )
The result, in symbolic form is: (-1/2, 1/10) union (5/4, 3/2) union (12/5, Inf). With my function, I have the result in numeric form (cond) and symbolic form, but separated into minimum and maximum.
cond =
-0.5000 0.1000
1.2500 1.5000
2.4000 Inf
condCell =
'-1/2' ' 1/10'
'5/4' ' 3/2'
'12/5' ' Inf'
I'm curious if there is another solution for my conditions (operating system xp, computer 20 years old, matlab 2010a)
function [ ra, racell ] = inequality( P, var, test )
test = subs(test,P);
s = feval(symengine, 'solve', test, var, 'Real')
if(isempty(s)) %no solution
ra=[1 -1]; %lower > higher
racell = {'no solution' ''};
else
s = char(s);
r = strfind(s,'R_');
if(isempty(r))
u = strfind(s,'union');
j = strfind(s,'Interval');
[r,c]=size(j);
%L=[0 0 0];
%racell = cell(1,2);
for(i=1:c)
if(i<c)
II = s(j(i)+9 : u(i)-3);
else
II = s(j(i)+9 : end -1);
end
kv = strfind(II,',');
v1 = II(1:kv-1);
v2 = II(kv+1:end);
try
L(i,:) = [sym2poly(vpa(v1)) sym2poly(vpa(v2)) i];
catch exception
L(i,:) = [(vpa(v1)) (vpa(v2)) i];
end
SS(i,1) = cellstr(v1);
SS(i,2) = cellstr(v2);
end
L = sortrows(L,1);
for(i=1:c)
ind = (L(i,3));
racell(i,1)=SS(ind,1);
racell(i,2)=SS(ind,2);
end
ra=L(:,1:2);
[ra racell] = checkrange(ra, racell);
else
ra = [-Inf Inf];
racell = {'-Inf' 'Inf'};
end
end
end
function [ra racell] = checkrange(rain, racellin)
[r,c] = size(rain);
j=1;
ra(j,1) = rain(1,1);
ra(j,2) = rain(1,2);
racell(j,1)=racellin(1,1);
racell(j,2)=racellin(1,2);
for i=2:r
if(rain(i,1) == rain(i-1,2))
ra(j,2) = rain(i,2);
racell(j,2)=racellin(i,2);
else
j = j + 1;
ra(j,1) = rain(i,1);
ra(j,2) = rain(i,2);
racell(j,1)=racellin(i,1);
racell(j,2)=racellin(i,2);
end
end
end

Más respuestas (1)

Walter Roberson
Walter Roberson el 11 de Jul. de 2019

0 votos

ch = children(solution);
[children(ch(1)),children(ch(2))]

4 comentarios

riccardo agnesi
riccardo agnesi el 11 de Jul. de 2019
ch = children(solution);
[children(ch(1)),children(ch(2))]
??? Undefined function or method 'children' for input arguments of type 'sym'.
Walter Roberson
Walter Roberson el 11 de Jul. de 2019
You might have a corrupt MATLAB installation.
... Or you might have forgotten to mention that you are using a version before R2012a, which would be important information in order to know how to solve the difficulty.
riccardo agnesi
riccardo agnesi el 12 de Jul. de 2019
Ops. I'm using a 20-year-old pc with the xp operating system and matlab 2010a
Walter Roberson
Walter Roberson el 8 de Ag. de 2019
I don't think I have a virtual machine for a MATLAB that old...

Iniciar sesión para comentar.

Productos

Versión

R2010a

Community Treasure Hunt

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

Start Hunting!

Translated by