How can I round the coefficient of a term containing symbolic variables?

37 views (last 30 days)
Friends,I need to round off a term containing symbolic variables to zero when its coefficient is numerically very small value.
For eg: in the following expression:
4.5*h + 6.12e-17*cos(q)
I need to round off the second term to zero as its coefficient is very small.
(q and h are symbolic varialbes)
How can I do this?
Thanks in advance.
(for those who want to have more details on how I arrived at that expression:
This was my preliminary result:
(4967757600021511*cos(q))/81129638414606681695789005144064
I used VPA to convert this to decimal form and the result obtained was
0.00000000000000006123233995736766035868820147292*cos(q)
Then, I set the no:of significant digits to 3 and the result was:
6.12e-17*cos(q)
  2 Comments
DM
DM on 6 Feb 2015
Hi, I was wondering were you able to round the coefficient or not? I have a similar problem.
amin ya
amin ya on 8 Jul 2019
Edited: amin ya on 8 Jul 2019
Did you find any method? my numers are not small like yours, but thoes kind of round off erros still exist

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 26 Dec 2014
vpa does not really allow you round coefficients as you think you wish to do. In fact, that will often be a bad thing, since one can easily construct a sum of terms where the term with the smallest coefficient was actually the most important and significant one. The symbolic toolbox cannot make that decision. In your example, if h is 1e-20, then since cos(q) will be of order 1, then that second term will generally dominate the sum. And since h is symbolic, it can be any value.
You CAN do something as you would like however, although I would not in general advise doing so.
X = 4.5*h + 6.12e-17*cos(q);
[C,T] = coeffs(X)
C =
[ 9/2, 4965133870973929/81129638414606681695789005144064]
T =
[ h, cos(q)]
C(2) = 0
C =
[ 9/2, 0]
dot(C,T)
ans =
(9*h)/2
Better would be to do your computations in a more intelligent form, so that you do not produce those spurious terms at all.
For example, when you created this expression, if the coefficients came from double precision numbers then subtractive cancellation happens, you might be left with garbage terms like this. However, a better way was to be more careful about how you entered those coefficients.
  4 Comments
Roy Goodman
Roy Goodman on 24 Apr 2015
Edited: Roy Goodman on 24 Apr 2015
There is a good discussion in the newsgroup , with an eventual answer provided by Cleve Moler himself!
I'm trying to do something similar, which I have already implemented in Mathematica but which I need to implement in Matlab as part of a larger piece of numerical software. I am trying to symbolically calculate the terms in a power series to satisfy a functional equation. The result of a symbolic calculation is a sequence of algebraic equations for the coefficients, that can be solved order by order.
I have a Matlab code that works well on an example for which all the coefficients are integers or relatively small-denominator rational numbers. However I have another example where the coefficients that arise are complicated-looking algebraic numbers, and Matlab seems to want to approximate these by floating point or rational numbers. It is not a matter of me being more careful, because Matlab, as far as I can tell, is deciding to make these approximations. It could be there's a way to tell it not to.
At each order in the series, Matlab should simply be solving a single linear equation. Unfortunately, in one example, I am trying to compute a certain coefficient c3. Matlab tells me that the equation to be solved is (I have simplified the output using vpa( ,10) but otherwise this is just cut and paste from the matlab desktop):
0.0000000006417584932*c3^2 + 10.71846967*c3 - 0.1288413869 ==0
The coefficient of the quadratic term is spurious and I would like to set it to zero. As this is going on inside a matlab function and not at the command line, I'd like to do it automatically.
In fact, it might be more out of my hands than this, because the code takes as input a user-supplied m-file that is used to define the functional equation, and this may have ordinary double-precision numbers in it.
John D'Errico
John D'Errico on 26 Apr 2015
The problem is that MATLAB cannot know that term is insignificant, because MATLAB does not know what range of numbers c3 lives in. c3 is a symbolic variable. It can take on ANY value, so 1e-12, pi, or 6.02e23 are all possibilities as far as your computer knows.
For the first value in my set of examples, this polynomial will be essentially constant. In the second case, it will in fact look vaguely like a linear polynomial, and for the third example, this function will be dominated by the quadratic term.
Remember the story of the blind men and the elephant. One feels the tail, and exclaims that the elephant is like a rope. Another feels a leg, and thinks the elephant is like a tree. A third feels the trunk, and says it is like a snake, etc... The point here is the point of view is important, and if you see only a small part, then you may be mistaken.
A variable is just that, variable. It can take on any value. Only you will supply the context, and therefore the intelligence to know that one term is or is not significant.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!