f =
Any way to remove terms smaller than "digits" from symbolic expression? R2023a
Mostrar comentarios más antiguos
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
Paul
el 14 de Feb. de 2026 a las 22:10
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.
henjimattrisse
el 14 de Feb. de 2026 a las 22:28
Respuesta aceptada
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
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{:}])
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)
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.
Categorías
Más información sobre Common Operations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

