Symbolic expressions with pi are displaying as decimals

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.

 Accepted Answer

sympref('FloatingPointOutput', 0)

5 Comments

I suspect that preference is, in fact, the issue for the particular problem by the OP, and I always forget about this preference.
But, if I'm not mistaken, this preference only controls the display of expressions but not their internal representation. If correct, this preference is a neccesary condition to have the constant pi in symbolic expressions be displayed as pi, but not sufficient.
sympref('default');
sympref('FloatingPointOutput')
ans = logical
0
With the default value of false, pi can still be absorbed
f = sym(pi^2/4)
f = 
So sym(pi) is still needed
f = sym(pi)^2/4
f = 
If the preference is set to true, it does change the display
sympref('FloatingPointOutput',1);
f
f = 
2.4674
But not the internal representation
sympref('FloatingPointOutput',0);
f
f = 
TLDR: IMO, use sym(pi) in all symbolic expressions, either explicitly, or defining Pi = sym(pi) at the top of the code and use that, or define a function that returns sym(pi) and use that. Use FloatingPointOutput to control display of symbolic numbers.
@Paul thank you for the comment, but @Walter Roberson's answer achieved everything I was looking for. Using sym(pi) in every single script is error prone and easy to forget, so it is not a viable option.
The particular context has
lambda = (((2*n+1)*pi)/(2*L))^2
where n is symbolic. That results in a symbolic expression being multiplied by numeric pi, which triggers numeric pi being converted to symbolic, which happens perfectly.
Whereas sym(pi^2/4) results in numeric pi being squared and divided by 4, and the result being converted. The automatic conversion is able to recognize "small" multiplies of pi (roughly multiplication or division by up to 1000) but it is not able to recognize pi^2 .
Interesting, my run was able to recognize it.
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.

Sign in to comment.

More Answers (1)

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 Comments

I already tried using sym(pi) I understand for long expressions it won't give what I want. But it should not be combining other numbers with pi. Pi=sym(pi) only fixes it for this script. I want to change the settings for any future scripts I write while I have this license. Isn't there a setting I can change?
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
ans = 
function Pi = sympi
Pi = sym(pi);
end
All I mean is that I do not want to have to code sym(pi) for each new script.
I added your sympi function, and nothing changes for me.
syms n L
Pi = sympi;
lambda = (((2*n+1)*Pi)/(2*L))^2 % eigenvalues, pi changed to Pi
lambda = 
function Pi = sympi
Pi = sym(pi);
end
Based on previous answers and comments I do not see why pi^2/4 cannot be represented as a fractional form instead of this decimal.
" ... 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
lambda = 
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.
@James Tursa why is this still an ongoing discussion?
I asked the question based on my prerequisites, so I decide what I should be doing.
When you write your scripts you can do what you want.
pi like e is a number programmed into MATLAB for calculations. You're just adding extra steps.
And if you read my question, I am using 2021b.
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.
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.

Sign in to comment.

Categories

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!