Clear Filters
Clear Filters

Strange output from symbolic calculation

4 views (last 30 days)
Can someone explain this output (in particular, the numbers)
syms f0 T t0 Omegan t n
subs(2*f0*pi^2/(Omegan*T*(- Omegan^2*t0^2 + 4*pi^2)),Omegan,2*n*pi/T)
ans =
-(2778046668940015*f0)/(281474976710656*n*pi*((4*n^2*t0^2*pi^2)/T^2 - 2778046668940015/70368744177664))

Accepted Answer

David Winthrop
David Winthrop on 1 Nov 2022
I figured out what was going on. It seems the sym interpreter was giving a rational approximation for pi only some of the time. Very strange. For future reference, this is how I got what is expected:
syms f0 T t0 Omegan t n
p = sym(pi); % Key step here.
simplify(subs(2*f0*p^2/(Omegan*T*(- Omegan^2*t0^2 + 4*p^2)),Omegan,2*n*p/T))
ans =
-(T^2*f0)/(4*n*pi*(- T^2 + n^2*t0^2))

More Answers (2)

Gautam Chettiar
Gautam Chettiar on 1 Nov 2022
Edited: Gautam Chettiar on 1 Nov 2022
% The numbers come because of your substitution of Omegan to 2*n*pi/T, with
% some of the variables cancelling out, and most of the numbers coming from
% 2 and pi
syms f0 T t0 Omegan t n
ans = subs(2*f0*pi^2/(Omegan*T*(- Omegan^2*t0^2 + 4*pi^2)),Omegan,2*n*pi/T)
ans = 
vpa(simplify(ans), 3)
ans = 

James Tursa
James Tursa on 1 Nov 2022
Edited: James Tursa on 1 Nov 2022
This isn't strange at all. You are asking the Symbolic Engine to examine all floating point expressions involving pi, which isn't even represented exactly in IEEE floating point, and extract the pi part. There are an infinite number of such expressions, of course, so you can't expect the Symbolic Engine to get them all. As such, it only gets the simple ones. E.g.,
ans = 
ans = 
ans = 
ans = 
ans = 
ans = 
ans = 
ans = 
ans = 
ans = 
ans = 
The solution, as you have already discovered, is to insert symbolic constants up front rather than passing results of floating point expressions to the Symbolic Engine and expecting it to divine the source in all cases. Even the simple expressions have a limit for when the Symbolic Engine stops looking. E.g.,
ans = 
ans = 
  1 Comment
Steven Lord
Steven Lord on 1 Nov 2022
See the description of the 'r' value for the flag input argument on the sym function's documentation page for a list of the expressions sym "recognizes". Most of the examples you posted that convert pi to π fall into the p*pi/q (for modest-sized integers p and q) case.

Sign in to comment.





Community Treasure Hunt

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

Start Hunting!