필터 지우기
필터 지우기

Convert numerical equations to latex

조회 수: 2 (최근 30일)
Oliver Wan
Oliver Wan 2020년 8월 15일
편집: Oliver Wan 2020년 8월 15일
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).

답변 (1개)

J. Alex Lee
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'))
  댓글 수: 1
Oliver Wan
Oliver Wan 2020년 8월 15일
Hi Alex, my apologies, I did not specify that the example I gave was only a very simple example. Im looking for a solution that works for very long equations with many constant terms. It is also the case that the results are not always integers.
I apologise once again for my poor communication on my behalf.
Thank you for your response!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by