Eliminate the long number appearing in symbolic calculations

42 ビュー (過去 30 日間)
Luqman Saleem 2024 年 7 月 19 日 16:17
コメント済み: Sam Chak 2024 年 7 月 19 日 21:05
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 2024 年 7 月 19 日 16:36
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 件のコメント3 件の古いコメントを表示3 件の古いコメントを非表示
Luqman Saleem 2024 年 7 月 19 日 18:01
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.
Star Strider 2024 年 7 月 19 日 18:03
As always, my pleasure!

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord 2024 年 7 月 19 日 17:07
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 2024 年 7 月 19 日 18:21
More compactly,
term = str2sym("J1*(6582018229284824168619876730229377341315370891042652124695852093/13164036458569648337239753460458804039861886925068638906788872192 - 4302204281461843i/81129638414606681695789005144064)")
term =
Sam Chak 2024 年 7 月 19 日 21:05
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 Center および File ExchangeConversion Between Symbolic and Numeric についてさらに検索

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by