# Simplifying a matematical expression

1 view (last 30 days)
Michael Vaughan on 17 Sep 2020
Commented: Michael Vaughan on 17 Sep 2020
I have this following beastly expression typed up very nicely in LaTeX formatting, as you can see. Is there anyway that I can enter this into Matlab to simplify it?? I feel like even if I type it in, then it will be hard for me to know if I typed in the correct expression. How do i simplify this beast!? Thank you!
$W_{(1,1)}(t,v)=\frac{-t^{-2k}v^k}{3}(\frac{v^{\frac{3}{2}}-v^{\frac{-3}{2}}}{t^{\frac{3}{2}}-t^{\frac{-3}{2}}})(\frac{v^{\frac{1}{2}}-v^{\frac{-1}{2}}}{t^{\frac{1}{2}}-t^{\frac{-1}{2}}})+\frac{t^{-2k}v^k}{4}(\frac{v-v^{-1}}{t-t^{-1}})^2+\frac{t^{-2k}v^k}{12}(\frac{v^{\frac{1}{2}}-v^{\frac{-1}{2}}}{t^{\frac{1}{2}}-t^{\frac{-1}{2}}})-\frac{t^{-k}v^k}{4}(\frac{v^2-v^{-2}}{t^2-t^{-2}}) + \frac{t^{-k}v^k}{8}(\frac{v-v^{-1}}{t-t^{-1}})^2+\frac{t^{-k}v^k}{4}(\frac{v-v^{-1}}{t-t^{-1}})(\frac{v^{\frac{1}{2}}-v^{\frac{-1}{2}}}{t^{\frac{1}{2}}-t^{\frac{-1}{2}}})^2-\frac{t^{-k}v^k}{8}(\frac{v^{\frac{1}{2}}-v^{\frac{-1}{2}}}{t^{\frac{1}{2}}-t^{\frac{-1}{2}}})^4+\frac{-v^kt^{k}}{4}(\frac{v^2-v^{-2}}{t^2-t^{-2}})+\frac{v^kt^{k}}{3}(\frac{v^{\frac{3}{2}}-v^{\frac{-3}{2}}}{t^{\frac{3}{2}}-t^{\frac{-3}{2}}})(\frac{v^{\frac{1}{2}}-v^{\frac{-1}{2}}}{t^{\frac{1}{2}}-t^{\frac{-1}{2}}})+\frac{v^kt^{k}}{8}(\frac{v-v^{-1}}{t-t^{-1}})^2-\frac{v^kt^{k}}{4}(\frac{v-v^{-1}}{t-t^{-1}})(\frac{v^{\frac{1}{2}}-v^{\frac{-1}{2}}}{t^{\frac{1}{2}}-t^{\frac{-1}{2}}})^2+\frac{v^kt^{k}}{24}(\frac{v^{\frac{1}{2}}-v^{\frac{-1}{2}}}{t^{\frac{1}{2}}-t^{\frac{-1}{2}}})^4$

Steven Lord on 17 Sep 2020
After inserting it into Answers using the sigma button on the toolstrip: Honestly, I wouldn't enter this as one term. As you called out, there's a decent amount of risk in missing a term or typing it in incorrectly. I'd enter it in one term or one part of a term at a time.
function result = W11(t, v)
k = 2; % Arbitrarily chosen value
% These expressions are used quite often, so compute them once and reuse the shorter expressions
sv = sqrt(v);
sv3 = sv.^3;
st = sqrt(t);
st3 = st.^3;
term(1) = -(t.^(-2*k).*(v^k))/3 * (sv3-1./sv3)./(st3-1./st3) * (sv-1./sv)./(st-1./st);
term(2) = ...
% Continue with the rest of the terms
result = sum(term);
end
This way if there's a problem with one of the terms its individual piece can be debugged more easily. You could even compute more of the common subexpressions once like I did with sv, sv3, etc. The expression (sv-1./sv)./(st-1./st) looks like it occurs frequently so it may be a good candidate.

Michael Vaughan on 17 Sep 2020
Thanks man, this helps a ton. I have 0 programming experience so what you've showed here really helps.
Michael Vaughan on 17 Sep 2020
Exscuse me, but I am trying to get this code to run and I am having issues. Even when I deleted the part about term(2), so the result should just give term(1) as the output. I'm getting the following error:
function result = W11(t, v)
Error: Function definition not supported in this context.
Create functions in code file.