vpa not reducing the precision
13 次查看(过去 30 天)
显示 更早的评论
Hello,
I am doing some symbolic calculations. Matlab is returning rediculously small numbers up to the order of -180 ! I tried the suggested usage of digits and vpa but none seem to do the job. here is an example of numbers up to -15 .
[ -15 -15 -15 ]
[1. 0.1224 10 -0.1224 10 0.1224 10 q2 + 1. q1]
This is a very simple homogeneous transformation of a frame in multi-body system. you can imagine multiplying these small numbers to get even smaller ones. This is keeping the symbolic variables q1, q2 ... in the experession and further complicating the calculations and slowing down the program.
I ran vpa on these results, it didn't work. I went down and applied vpa on every symbolic math in my sheet, still didn't work.
please adivse..
thanks.
3 个评论
采纳的回答
Walter Roberson
2022-2-11
Consider:
1
10^-49 * --------
x*10^-50
This is, of course, mathematically the same as x*10 -- but there are cases where MATLAB prefers to factor out constants even though it makes other constants "awkward" . Imagine for example 10*y + 10^-14*x then you could imagine that MATLAB might prefer to rewrite that in terms of x first and y, and might prefer to have small-integer coefficient for x by factoring it out, 10^-14 * (x + 10^15*y) . MATLAB does prefer to have the symbols of a summand appear in lexographic order, except in denominators in which case it prefers the first non-negative symbol to be first... and it is prepared to invent multiplications and divisions to reach its preferred internal factorization form.
The point is that Yes, there are definitely cases where MATLAB will factor out into large numbers times small coefficients... cases that are mathematically the same but involve coefficients with small magnitude.
Now imagine that vpa() had the role of going in and zeroing coefficients that were smaller than 1/10^Digits . Then for that first expression, it would be doing 10^-49 * 1/((ZERO the 10^-50) * x) ... which would be 10^-49 * 1/(0 * x) -->> 10^-49 / 0 --> +infinity
Therefore, vpa should not be zeroing "small" coefficients: those small coefficients might be very necessary in context.
You could, I am sure, imagine formulae that involve dividing by Avogadro's Number 6.022*10^23 -- and mathematically that division would be equivalent to multiplying by the reciprical of the number... so multiplying by about 1.6605*10^-24 . If vpa() automatically zeroed "small" constants then that would drastically change the meaning of the expression.
This does not mean that there is no way of getting MATLAB to zero out "small" numbers... but it does mean that it should not be done by vpa() and that when it is done, it has to be done carefully. The fancy symbolic rational expressions you see displayed are sometimes internally coded quite differently than you expect.
10 个评论
Walter Roberson
2022-2-24
If you want to go back to doing text manipulation to zero small coefficients, then use char() or string() . Just be careful because the computing language returned by char() or string() is not exactly MATLAB and is not exactly the internal symbolic language MuPAD. Some of the changes are pretty visible, with phrases like
k in R\Z
showing up. But some of the changes are easily overlooked, such as the order of parameters for the beta() function .
It is not guaranteed that you will be able to use char() followed by str2sym() to get back the original expression.
It is a bit more robust to use matlabFunction() to generate equivalent numeric MATLAB code... provided that your code does not use piecewise(), and provided that you are comfortable with int() being turned into integral()...
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!