Eliminate the long number appearing in symbolic calculations

10 次查看(过去 30 天)
I am doing some symbolic calculations and end up with very large numbers in the symbolic expression. For example, I get the variable "term" as the result of my calculation:
syms J1
term = J1*(6582018229284824168619876730229377341315370891042652124695852093/13164036458569648337239753460458804039861886925068638906788872192 - 4302204281461843i/81129638414606681695789005144064);
It contains these long numbers. When I use vpa(), I get the following:
vpa(term)
ans = 
How can I eliminate these small numbers with imaginary parts using vpa() or any other function? They should be rounded to zero. (I don't want to take only the real part using the real() function because these numbers can show up as real part too.)

采纳的回答

Star Strider
Star Strider 2024-7-19
You can conttrol the number of digits displayed by including a second argument (here 7) to your vpa call —
syms J1
term = J1*(6582018229284824168619876730229377341315370891042652124695852093/13164036458569648337239753460458804039861886925068638906788872192 - 4302204281461843i/81129638414606681695789005144064);
vpa(term, 7)
ans = 
This does not affect exponents (including arguments to the exp function), so they will still have a large number of digits, however it works on all others.
.
  5 个评论
Luqman Saleem
Luqman Saleem 2024-7-19
I got "J1*(0.5 - 0.00000000000000010605752387249061752691092791815i)" after performing the vpa() on the results that I got from symbolic calculation. So, if I perform vpa() two times then I get results in exponential form. That's good enough for me.
Thank you.

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2024-7-19
Note that you don't want to write your numbers in double first before performing calculations involving the symbolic variable J1.
syms J1
term = J1*(6582018229284824168619876730229377341315370891042652124695852093/13164036458569648337239753460458804039861886925068638906788872192 - 4302204281461843i/81129638414606681695789005144064)
term = 
If you look at the first two terms you see that they can't be exactly 1/2; if it was twice the numerator (which ends in 3) would have to end in a 6 and the denominator doesn't. You can see if you convert the number (represented as a string, so the symbolic value is the exact value in the string rather than the closest double precision number to it) that it's very, very close to 1/2 but not exact.
a1 = sym('6582018229284824168619876730229377341315370891042652124695852093');
a2 = sym('13164036458569648337239753460458804039861886925068638906788872192');
vpa(a1/a2, 50)
ans = 
0.49999999999999999999999999999999812530027167267725
a3 = sym('4302204281461843i');
a4 = sym('81129638414606681695789005144064');
So what do you get if you use those symbolic values?
term2 = J1*(a1/a2-a3/a4)
term2 = 
Or, approximating to say 20 places:
vpa(term2, 20)
ans = 
  2 个评论
Walter Roberson
Walter Roberson 2024-7-19
More compactly,
term = str2sym("J1*(6582018229284824168619876730229377341315370891042652124695852093/13164036458569648337239753460458804039861886925068638906788872192 - 4302204281461843i/81129638414606681695789005144064)")
term = 
Sam Chak
Sam Chak 2024-7-19
This is awesome! The ability of MATLAB to perform numerical computations with an accuracy exceeding that of a 200-digit Full Precision Calculator is indeed an impressive capability.
@Luqman Saleem, Keep in mind that the computed real part is NOT exactly 0.5 or .
%% Number of digits in the Numerator
numDigits = numel(num2str('6582018229284824168619876730229377341315370891042652124695852093'))
numDigits = 64
%% Number of digits in the Denominator
denDigits = numel(num2str('13164036458569648337239753460458804039861886925068638906788872192'))
denDigits = 65
%% Convert to Symbolic expression
term = str2sym("J1*(6582018229284824168619876730229377341315370891042652124695852093/13164036458569648337239753460458804039861886925068638906788872192 - 4302204281461843i/81129638414606681695789005144064)")
term = 
%% Request 260 digits of precision (but failed to achieve)
vpa(term, 4*denDigits)
ans = 

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by