Eliminate the long number appearing in symbolic calculations

15 visualizzazioni (ultimi 30 giorni)
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.)

Risposta accettata

Star Strider
Star Strider il 19 Lug 2024
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 Commenti
Luqman Saleem
Luqman Saleem il 19 Lug 2024
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.

Accedi per commentare.

Più risposte (1)

Steven Lord
Steven Lord il 19 Lug 2024
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 Commenti
Walter Roberson
Walter Roberson il 19 Lug 2024
More compactly,
term = str2sym("J1*(6582018229284824168619876730229377341315370891042652124695852093/13164036458569648337239753460458804039861886925068638906788872192 - 4302204281461843i/81129638414606681695789005144064)")
term = 
Sam Chak
Sam Chak il 19 Lug 2024
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 = 

Accedi per commentare.

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by