Strange output from symbolic calculation

조회 수: 10 (최근 30일)
David Winthrop
David Winthrop 2022년 11월 1일
댓글: Steven Lord 2022년 11월 1일
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))

채택된 답변

David Winthrop
David Winthrop 2022년 11월 1일
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))

추가 답변 (2개)

Gautam Chettiar
Gautam Chettiar 2022년 11월 1일
편집: Gautam Chettiar 2022년 11월 1일
% 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 2022년 11월 1일
편집: James Tursa 2022년 11월 1일
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
Steven Lord
Steven Lord 2022년 11월 1일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by