Is there a way to set the MATLAB environment to assume everything is symbolic?

1 view (last 30 days)
Hi all,
I'm trying to use matlab to verify some hand written calculations. Specifically given some square matrix A, and the system , I'm solving for the fundamental solution matrix and particular solution for some given initial conditions η.
So lots of calculating eigenvalues, egienvectors, inverting matrices, etc.
I would like to see the results of these intermediate and final calculations in nice, exact form. Meaning show me the square roots.
I'm aware of symbolic math in MATLAB. But it seems like I have to wrap everything I do in sym(2+sqrt(t)) or sym([1/3, sqrt(3)/2, 4]) etc... And this seems to only work about half the time, as my square roots are at random converted to fractions and decimals even when wraped in the sym function.
Is there any setting that will just tell matlab I ALWAYS want symbolic math, at least temporarily?
Thanks

Accepted Answer

Walter Roberson
Walter Roberson on 28 Feb 2019
Sorry, no, there is not.
I usually define
Q = @(v) sym(v, 'r');
and then I put Q() around all numeric constants, with minor minor shortcuts. Like Q(2)+sqrt(t), or [1/Q(3), sqrt(Q(3)/2), Q(4)]
If I am adapting enough code to make this a nuisance, I take the code into a more powerful editor and do find/replace on patterns, such as vim command mode
1,$s/\(\D\)\(\d\)/\1Q(\2)/g
1,$s/\(\d\)\(\D\)/\1)\2/g
which is looking for a non-digit followed by a digit and puts a Q( between the two, and looks for digit followed by a non-digit and puts a ) after the digit. (To be honest, vim is a bit of a pain for regular expressions... but it is usually faster to put up with it then to write some code in awk or perl for the sake of better regular expressions.)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!