Printing out pi as a symbol in equations instead of the numerical value
16 次查看(过去 30 天)
显示 更早的评论
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 个评论
John D'Errico
2021-3-20
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.
采纳的回答
Konrad Bolembach
2021-3-20
1 个评论
Paul
2021-3-21
Can you show a simple code example illustrating how an_Id can be used with g or pi symbolically or numerically?
更多回答(2 个)
Star Strider
2021-3-20
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 个评论
dpb
2021-3-20
%%%%% Constants
pi = 3.14159265;
Also, don't alias builtin pi that returns full double precision.
Paul
2021-3-20
编辑:Paul
2021-3-20
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}}'
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/556967/image.png)
I don't know what the u_c function does, hopefully it can be modeified to accept the new form of an_Id
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!