Symbolic expressions with pi are displaying as decimals
Show older comments
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
A_n = 2/L*(int(f1,x,0,R*L)+int(f2,x,R*L,L))
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.
Accepted Answer
More Answers (1)
Hi Nathaniel.
Option 1: Always use
sym(pi);
in symbolic expressions. For example
5.13445677*sym(pi)
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)
But sometimes it can't
sym(5.13445677*pi)
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
9 Comments
Nathaniel H Werner
on 10 Feb 2024
Edited: Nathaniel H Werner
on 10 Feb 2024
Can you give an example that illustrates when sym(pi) "won't give what I want"?
I'm not aware of any settings for this issue.
One option would be to create a simple function that returns sym(pi), make sure that function is on your matlabpath in the usual way, and use that function in sympolic expressions instead of the numeric pi function. For example
5.13445677*sympi
function Pi = sympi
Pi = sym(pi);
end
Nathaniel H Werner
on 10 Feb 2024
Paul
on 10 Feb 2024
See answer below from @Walter Roberson
" ... All I mean is that I do not want to have to code sym(pi) for each new script. ..."
But, that is exactly what you should be doing, using sym(pi) in all your scripts and not relying on settings or hoping that the Symbolic parser will bail you out. Writing good code is sometimes more tedious but in the end you get good code.
Here is what I get running online directly:
syms n L
Pi = sym(pi);
lambda = (((2*n+1)*Pi)/(2*L))^2
And here is what I get with R2023a on my computer using the sympi function:
>> syms n L
>> Pi = sympi;
>> lambda = (((2*n+1)*Pi)/(2*L))^2
lambda =
(pi^2*(2*n + 1)^2)/(4*L^2)
So, the symbolic pi is retained in both cases.
Nathaniel H Werner
on 11 Feb 2024
Edited: Nathaniel H Werner
on 11 Feb 2024
DGM
on 11 Feb 2024
"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
on 11 Feb 2024
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.
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



