Strange output from symbolic calculation

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

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)

% 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 = 
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.,
sym(pi)
ans = 
π
sym(4*pi)
ans = 
sym(pi/3)
ans = 
sym(pi^2)
ans = 
sym(pi)^2
ans = 
sym(exp(1))
ans = 
exp(sym(1))
ans = 
e
sym(exp(1)/3)
ans = 
exp(sym(1))/3
ans = 
sym(pi*exp(1))
ans = 
sym(pi)*exp(sym(1))
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.,
sym(pi/100000)
ans = 
sym(pi/100001)
ans = 

1 Comment

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.

Products

Release

R2018b

Tags

Community Treasure Hunt

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

Start Hunting!