Prevent automatic simplification of a symbolic expression when using the latex command

9 views (last 30 days)
I have the following expression
f = @(k,n)1.0./n.^(2.0./3.0).*(6.0.^(1.0./3.0).*k.^(1.0./3.0).*4.0-k.*n.^(2.0./3.0).*1.2e1).*(-1.0./3.0)
and when I write
latex(f(k,n))
I get the hideous expression
\frac{12\, k\, n^{\frac{2}{3}} - \frac{8183583624766079\, k^{\frac{1}{3}}}{1125899906842624}}{3\, n^{\frac{2}{3}}}
Obviously matlab is doing some simplifications along the way. Is there any way to get the latex command to output the original expression for f that I typed in, without making any simplifications?
Thanks for any advice

Accepted Answer

Walter Roberson
Walter Roberson on 13 Oct 2017
No, that is not possible in any easy way.
When you invoked f(k,n) to turn the function handle into a sym so you could call latex(), then you requested evaluation of the function. Evaluation of symbolic functions always involves automatic simplification of constant expressions and automatic combination of constant terms, along with various automatic rewriting of term order according to rules that can be difficult to work out.
I have to wonder, though, where that code came from? It looks suspiciously like something that would be generated by matlabFunction() applied to a symbolic expression; if so then possibly you can latex() the symbolic expression directly.
  3 Comments
Walter Roberson
Walter Roberson on 13 Oct 2017
syms k_r k_n n positive
assumeAlso(k_n > k_r);
f = @(k_r,n) -sym(2)*( sym(6)^(sym(1)/sym(3))/sym(2)* (n*abs(k_r)).^(-sym(2)/sym(3)) - sym(1) ).*k_r.^sym(2) ;
df_form = simplify(diff(f(k_r,n),k_r));
df_latex = latex(df_form);
df = matlabFunction(df_form);

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!