Symbolic expressions with pi are displaying as decimals

28 visualizzazioni (ultimi 30 giorni)
Nathaniel H Werner il 10 Feb 2024
Modificato: Paul il 11 Feb 2024
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.
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Risposta accettata

Walter Roberson il 10 Feb 2024
sympref('FloatingPointOutput', 0)
5 CommentiMostra 3 commenti meno recentiNascondi 3 commenti meno recenti
Nathaniel H Werner il 11 Feb 2024
Interesting, my run was able to recognize it.
Walter Roberson il 11 Feb 2024
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.

Accedi per commentare.

Più risposte (1)

Paul il 10 Feb 2024
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 CommentiMostra 7 commenti meno recentiNascondi 7 commenti meno recenti
DGM il 11 Feb 2024
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 il 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.

Accedi per commentare.

Categorie

Scopri di più su Symbolic Math Toolbox in Help Center e File Exchange

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by