Effacer les filtres
Effacer les filtres

Printing out pi as a symbol in equations instead of the numerical value

14 vues (au cours des 30 derniers jours)
I have the following code:
%%%%% Constants
pi = 3.14159265;
g = 9.80665; % [m/s^2]
%%%%% Copy constants
pi_ = 3.14159265;
g_ = 9.80665; % [m/s^2]
%%% Solve an equation for some values
an_Id = @(T, m, d) T^2 * m * g * d / (4 * pi^2);
syms T m d;
u_Id_1 = u_c(an_Id, [T m d], [T1 m_bar d1_bar], [u_T1 u_m u_d1]);
u_Id_2 = u_c(an_Id, [T m d], [T2 m_bar d2_bar], [u_T2 u_m u_d2]);
u_Id_3 = u_c(an_Id, [T m d], [T3 m_bar d3_bar], [u_T3 u_m u_d3]);
%%% Print the general equation
disp('u_c(Id_i) [T m d]')
clear pi g;
syms Id pi g;
dT = diff(an_Id, T)
dm = diff(an_Id, m)
dd = diff(an_Id, d)
eq_Id = Id == sqrt(dT^2 + dm^2 + dd^2);
latex(eq_Id)
pi = pi_; g = g_;
It is meant for solving for uncertainties given an equation. It all works fine, but the trouble is with printing the equation in latex. Specifically, whenever there's a numerical constant like pi or g, somehow the output is in the form of a ratio of big integers, instead of pi and g being written as symbols, even though I've cleared them and assigned them as symbolic values.
u_c(Id_i) [T m d]
dT =
(5520653160719109*T*d*m)/11112186650365012
dm =
(5520653160719109*T^2*d)/22224373300730024
dd =
(5520653160719109*T^2*m)/22224373300730024
ans =
'\mathrm{Id}=\sqrt{\frac{30477611320957888346985997753881\,T^4\,d^2}{493922768610201541788451335040576}+\frac{30477611320957888346985997753881\,T^4\,m^2}{493922768610201541788451335040576}+\frac{30477611320957888346985997753881\,T^2\,d^2\,m^2}{123480692152550385447112833760144}}'
Is there any way in which I can prevent those messy outputs and get a clean looking equation?
  1 commentaire
John D'Errico
John D'Errico le 20 Mar 2021
Never define pi as a number. The approximatino you used is a terrribly poor one anyway. And what you want really is pi in there, NOT an approximation. So don't define pi at all. It already exists in MATLAB.

Connectez-vous pour commenter.

Réponse acceptée

Konrad Bolembach
Konrad Bolembach le 20 Mar 2021
The way I've dealt with it is to define the an_Id function like so:
an_Id = @(T, m, d) T^2 * m * sym(g) * d / (4 * sym(pi)^2);
This way I can use g and pi symbolically or numerically, whichever I need at the time.
  1 commentaire
Paul
Paul le 21 Mar 2021
Can you show a simple code example illustrating how an_Id can be used with g or pi symbolically or numerically?

Connectez-vous pour commenter.

Plus de réponses (2)

Star Strider
Star Strider le 20 Mar 2021
When I try to run your posted code, I get:
Unrecognized function or variable 'T1'.
However, you can get less messy results with the vpa function. (I simply cannot demonstrate the effect with your code.)
  1 commentaire
dpb
dpb le 20 Mar 2021
%%%%% Constants
pi = 3.14159265;
Also, don't alias builtin pi that returns full double precision.

Connectez-vous pour commenter.


Paul
Paul le 20 Mar 2021
Modifié(e) : Paul le 20 Mar 2021
If I understand the problem, it appears the idea is to define a function, an_Id, that can be used for both numerical evaluation and symbolically. I think the issue you're seeing is that, when an_Id is defined, it takes in the numerical values of g and pi as defined in the workspace at the time it's defined, so it never will see g and pi as sym objects. One approach would be to add g and pi as arguments, and then they can be input either as numbers or sym objects:
an_Id = @(T,m,d,g,pi) T^2 * m * g * d / (4 * pi^2);
% evaluate numerically
an_Id(1,1,1,9.81,pi)
% differentiate symbolically
syms T m d g Id
dT = diff(an_Id,T)
dm = diff(an_Id,m)
dd = diff(an_Id,d)
eq_Id = Id == sqrt(dT^2 + dm^2 + dd^2);
latex(eq_Id)
ans =
0.248490202882833
dT =
(T*d*g*m)/(2*pi^2)
dm =
(T^2*d*g)/(4*pi^2)
dd =
(T^2*g*m)/(4*pi^2)
ans =
'\mathrm{Id}=\sqrt{\frac{T^4\,d^2\,g^2}{16\,\pi ^4}+\frac{T^4\,g^2\,m^2}{16\,\pi ^4}+\frac{T^2\,d^2\,g^2\,m^2}{4\,\pi ^4}}'
I don't know what the u_c function does, hopefully it can be modeified to accept the new form of an_Id

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by