Symbolic expressions with pi are displaying as decimals

조회 수: 35 (최근 30일)
Nathaniel H Werner
Nathaniel H Werner 2024년 2월 10일
편집: Paul 2024년 2월 11일
I am trying to write a livescript that has pi show up a lot in the expressions.
Here is a sample of my script.
clc, clear, close all
syms n x L
S = 3;
R = 0.6;
f1 = S*x; % x < R
f2 = S*R; % R < x < L
fplot(f1,[0 R],'b'), hold on
fplot(f2,[R 1],'b'), xline(R,'--')
ylim([0 ceil(S*R)]), grid on
lambda = (((2*n+1)*pi)/(2*L))^2
lambda = 
A_n = 2/L*(int(f1,x,0,R*L)+int(f2,x,R*L,L))
A_n = 
You can see the outputs above, but in my script where lamba is output this is the result.
I must have changed the settings at some point, how do I make pi show up as a symbol? If possible rational numbers like the 27/50 should be decimals, but if it's not possible to have both that's fine. My release is 2021b.

채택된 답변

Walter Roberson
Walter Roberson 2024년 2월 10일
sympref('FloatingPointOutput', 0)
  댓글 수: 5
Nathaniel H Werner
Nathaniel H Werner 2024년 2월 11일
Interesting, my run was able to recognize it.
Walter Roberson
Walter Roberson 2024년 2월 11일
lambda = (((2*n+1)*pi)/(2*L))^2
n is symbolic, so 2*n+1 is symbolic. symbolic times numeric pi results in numeric pi being converted to symbolic, which converts to symbolic π because there is an exact match to the value it knows as numeric pi. So (2*n+1)*pi converts to .
2*L is numeric 2 times symbolic L, which triggers converting numeric 2 to symbolic 2, giving
You now have which then gets squared.
By way of contrast,
sym(pi^2/4)
evaluates pi^2 numerically and divides by 4 numerically . Then it attempts to convert to symbolic. However, the numeric pattern matching is not able to recognize the result and so converts the result to a symbolic rational number.
Roughly speaking, the symbolic toolbox is able to recognize a/b*pi where a and b are in the range 0 to 999 . It might recognize some other patterns as well.

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

추가 답변 (1개)

Paul
Paul 2024년 2월 10일
Hi Nathaniel.
Option 1: Always use
sym(pi);
in symbolic expressions. For example
5.13445677*sym(pi)
ans = 
If you use the numeric pi in an expression and then convert to sym, sometimes the Symbolic Math Toolbox can figure it out, like this
sym(2.5*pi)
ans = 
But sometimes it can't
sym(5.13445677*pi)
ans = 
So use sym(pi) to be safe.
Option 2: Define a variable that's sym(pi) and just use it everywhere in symbolic expressions
Pi = sym(pi);
5.13445677*Pi
ans = 
  댓글 수: 9
DGM
DGM 2024년 2월 11일
From @Nathaniel H Werner's comment-as-flag:
"This comment is opinion, and not objectively based. Comments and answers should attempt to give objective answers to the questions not the opinion of the commenter."
There's plenty of room for opinion, but best practices or canonical code patterns aren't really all that subjective. Either way, you're going to have a hard time finding anyone with editor status who would disagree so severely with James on the matter, so flagging his comment isn't going to get any of us to delete it. Besides, it would break the continuity of the thread. If you don't need the attention of an editor or staff, please use a comment instead of a flag.
Walter Roberson
Walter Roberson 2024년 2월 11일
As a best practice:
Near the beginning of your code, assign
Pi = sym(pi);
after that, code Pi instead of pi -- a change that is easily made using global search and replace.

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

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by