41 views (last 30 days)
Show older comments
Luqman Saleem on 19 Jul 2024 at 16:17
Commented: Sam Chak on 19 Jul 2024 at 21:05
Accepted Answer: Star Strider
Open in MATLAB Online
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.)
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Star Strider on 19 Jul 2024 at 16:36
Open in MATLAB Online
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 Comments Show 3 older commentsHide 3 older comments
Show 3 older commentsHide 3 older comments
Luqman Saleem on 19 Jul 2024 at 16:47
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/2138806-eliminate-the-long-number-appearing-in-symbolic-calculations#comment_3215906
Open in MATLAB Online
@Star Strider Thank you. It solved my problem for some terms. But others are still like the following:
syms J1
J1*(0.5 - 0.00000000000000010605752387249061752691092791815i)
ans=
Torsten on 19 Jul 2024 at 16:59
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/2138806-eliminate-the-long-number-appearing-in-symbolic-calculations#comment_3215931
Edited: Torsten on 19 Jul 2024 at 17:00
Open in MATLAB Online
Thank you. It solved my problem for some terms. But others are still like the following:
You can only manipulate the way the expression is displayed, not its internal representation.
syms J1
term = J1*(0.5 - 0.00000000000000010605752387249061752691092791815i)
term=
vpa(term,7)
ans=
Star Strider on 19 Jul 2024 at 17:00
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/2138806-eliminate-the-long-number-appearing-in-symbolic-calculations#comment_3215936
Open in MATLAB Online
It works for me —
syms J1
q = J1*(0.5 - 0.00000000000000010605752387249061752691092791815i)
q=
vpaq = vpa(q,7)
vpaq=
The vpa function reduces the fractions to decimal fraction approximations, with the desired number of digits.
.
Luqman Saleem on 19 Jul 2024 at 18:01
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/2138806-eliminate-the-long-number-appearing-in-symbolic-calculations#comment_3215996
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 on 19 Jul 2024 at 18:03
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/2138806-eliminate-the-long-number-appearing-in-symbolic-calculations#comment_3216001
As always, my pleasure!
Sign in to comment.
More Answers (1)
Steven Lord on 19 Jul 2024 at 17:07
Open in MATLAB Online
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 Comments Show NoneHide None
Show NoneHide None
Walter Roberson on 19 Jul 2024 at 18:21
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/2138806-eliminate-the-long-number-appearing-in-symbolic-calculations#comment_3216031
Open in MATLAB Online
More compactly,
term = str2sym("J1*(6582018229284824168619876730229377341315370891042652124695852093/13164036458569648337239753460458804039861886925068638906788872192 - 4302204281461843i/81129638414606681695789005144064)")
term=
Sam Chak on 19 Jul 2024 at 21:05
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/2138806-eliminate-the-long-number-appearing-in-symbolic-calculations#comment_3216151
Open in MATLAB Online
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=
Sign in to comment.
Sign in to answer this question.
See Also
Categories
Mathematics and OptimizationSymbolic Math ToolboxSymbolic Computations in MATLABConversion Between Symbolic and Numeric
Find more on Conversion Between Symbolic and Numeric in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office