Symbolic expressions with pi are displaying as decimals

35 次查看(过去 30 天)
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 个评论
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