Convert numerical equations to latex
조회 수: 10 (최근 30일)
이전 댓글 표시
Hi guys,
Im very very new to MATLAB and im trying to write some code to convert numerical equations to latex code. I have found that it is quite easy to do with symbolic variables, however not with "double" data types. I have tried converting the doubles to symbolic variables using
var = 1.234567890
sym_var = sym(var)
latex(sym_var)
However it often automatically converts the decimals to fractions which is undesireable.
Ive thought of workarounds but they are not very easy to replace and require a long time to code.
Ultimately, my goal is to acheive something like this very simple example:
%==== Numerical Values (Unrounded) ==============
a = 12838721234789
b = 2388284
c = 3822.2847272
%================================================
d = a + b/c
to return or print:
"d = 1280000+\frac{2340000}{3820} = 12384496.83"
or even:
I also wish for the code to work with very long and complicated equations, not just the simple example shown above.
Note that the output is a string containing all latex code required to display ANY numerical equation stored in "d" (including "d =" and the equivalent final answer for the equation). It also has all numerical values rounded to an easy to specify number of significant figures (in this case 3) excluding the final answer (which is always rounded to 10 sig figs).
댓글 수: 0
답변 (1개)
J. Alex Lee
2020년 8월 15일
Assuming the results will always be integers, here's a simple way
%==== Numerical Values (Unrounded) ==============
a = 1283872
b = 2388284
c = 3822.2847272
%================================================
d = a + b/c
s = 3
str = sprintf('d = %d + \\frac{%d}{%d}',...
round(a,s,'significant'),...
round(b,s,'significant'),...
round(c,s,'significant'))
참고 항목
카테고리
Help Center 및 File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!