Any way to remove terms smaller than "digits" from symbolic expression? R2023a

I solve a problem using the Symbolic Toolbox vpasolve command. The result is
ans = 16.71143596598017180476988917338*c - 10.900132093792609548012713382556*b*c - 0.000000000000000000000000000000082376592867983723666747651083278
I would like to eliminate the last term. If I use digits(1000) then it shrinks accordingly but never goes to zero.
Some previous suggests have been to try vpa(ans,5) to reduce the precision. This doesn't appear to work in R2023a any more. I've tried simplify(), vpa(), round() but none of them give a neat way to throw away numerical garbage.
Does anyone have any suggestions?? Thanks!

2 comentarios

Hi henjimattrisse,
I think the solution, if there is one, will depend on the specificity on the rule to determine which term(s) to eliminate.
For examples: is it always the third term? Is it always a constant that's not multiplying another symbolic variable? Is there a threshold? Etc.
Uh, it's always the term that's on the order of the smallest number representable by digits.

Iniciar sesión para comentar.

 Respuesta aceptada

syms b c
f = 16.71143596598017180476988917338*c - 10.900132093792609548012713382556*b*c - 0.000000000000000000000000000000082376592867983723666747651083278
f = 
f1 = mapSymType(f, 'number', @(X) logical(abs(X)>1e-5).*X)
f1 = 
However, this is risky. There is always the chance that the formula ended up rewritten as something along the lines of
g = (sym('167114359659801718047698891733800000000000000000000000000000000000')*c - sym('109001320937926095480127133825560000000000000000000000000000000000')*b*c - sym('82376592867983723666747651083278'))/sym('10000000000000000000000000000000000000000000000000000000000000000')
g = 
vpa(g)
ans = 
In which case the construct will not necessarily simplify as much as is shown here; potentially the constant might appear in scaled form with the denominator not being proximate.
g1 = mapSymType(g, 'number', @(X) logical(abs(X)>1e-5).*X)
g1 = 

1 comentario

This approach also eliminates coefficients that are below the threshold (in addition to the constant term), which might or might not be wanted. The question is unclear in this regard, IMO.
syms b c
smallterm = sym('0.000000000000000000000000000000082376592867983723666747651083278')
smallterm = 
0.000000000000000000000000000000082376592867983723666747651083278
f = smallterm*c - smallterm*b*c - smallterm
f = 
mapSymType(f, 'number', @(X) logical(abs(X)>1e-5).*X)
ans = 
0

Iniciar sesión para comentar.

Más respuestas (2)

"Uh, it's always the term that's on the order of the smallest number representable by digits."
Giving the benefit of the doubt that the "Uh" was supposed to be lighthearted humor ....
Not sure what is the smallest number representable by digits. Maybe something like this?
syms b c
f = 16.71143596598017180476988917338*c - 10.900132093792609548012713382556*b*c - 0.000000000000000000000000000000082376592867983723666747651083278
f = 
z = children(f);
nums = isSymType([z{:}],'number');
zvar = z(~nums);
znum = z(nums);
znum(isAlways(abs([znum{:}]) < 1/10^(sym(digits)-1))) = [];
f = sum([zvar{:},znum{:}])
f = 
John D'Errico
John D'Errico el 15 de Feb. de 2026 a las 12:09
Editada: John D'Errico el 15 de Feb. de 2026 a las 12:09
While the solutions offered by @Paul and @Walter Roberson will both solve the problem you asked, be careful, as automatic approaches to any such problem will get you in trouble one day. We need go no further than a simple Taylor series to see that happen.
syms x
Ex = taylor(exp(x),x,order = 20)
Ex = 
The coefficient of the x*19 term is complete numerical garbage, MOST OF THE TIME. For example, if you were working in double precision, when x is less than 1, that term will be always less than eps.
However, when x is on the order of 20 or 30? Now those high order terms will dominate. As such, when discarding terms, you need to be very conscious of the domain.
Even a constant term can be a problem at times if it is near zero, but all other terms are smaller yet in some portion of your domain.
So discard if you will, but don't allow code to make that decision for you.

Productos

Versión

R2023a

Etiquetas

Preguntada:

el 14 de Feb. de 2026 a las 20:57

Comentada:

el 15 de Feb. de 2026 a las 13:42

Community Treasure Hunt

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

Start Hunting!

Translated by