MATLAB: Symbolic equation is too long to be displayed.

symbolic equations

Hello. I want to solve the equation v = 0 for x, many many times, for different values of P, dd and tc. Using matlabs solve-function to find the solution takes too much time. I am trying a different approach, to avoid using the solve-function. First: Use the solve-function to find the "symbolic" expression of x. Then: Copy the equation I get and paste it into my function, hoping this will be faster. When I try to do this, (as showed bellow), I dont get the whole expression for x, because the expression is too long, and I get the message: Output truncated. Text exceeds maximum line length of 25 000 characters for Command Window display. So, my questions are: How can I get the whole expression? Or, can I use the expression I get from solve to find x, without pasting the whole expression into my function? Is there a faster way to solve this problem?
x = sym('x');
P = sym('P', [3 3]);
dd = sym('dd', [1 3]);
tc = sym('tc', [1 3]);
t2 = (tc - x)./dd;
v = t2*P^-1*s^2*(P^-1).'*t2.'-1;
x = solve(v,x)
x =
(2*P1_1^2*P2_2^2*dd1^2*dd2^2*s^2*tc3 + 2*P1_2^2*P2_1^2*dd1^2*dd2^2*s^2*tc3 + ..............more letters............... + P1_3*P2_1*P3_2 - P1_3*P2_2*P3_1)) - (P1_1*P3_3 - P1_3*P3_1)/(dd2*(P1_1*P2_2*P3_3 - P1_1*P2_3*P3_2 - P1_2*... Output truncated. Text exceeds maximum line length of 25 000 characters for Command Window display.
(2*P1_1^2*P2_2^2*dd1^2*dd2^2*s^2*tc3 + .....More letters........ - (P1_1*P3_3 - P1_3*P3_1)/(dd2*(P1_1*P2_2*P3_3 - P1_1*P2_3*P3_2 - P1_2*... Output truncated. Text exceeds maximum line length of 25 000 characters for Command Window display.
It works for the two dimensional problem. I found the symbolic equation for x using solve, and pasted it into my function. The two solutions of x, xxP and xxM, are found and the equations looks like this: (Pi_j, ddi, s and tci are known)
xxP = (P1_1^2*dd1^2*s*tc2 + P1_2^2*dd2^2*s*tc1 + P2_1^2*dd1^2*s*tc2 + P2_2^2*dd2^2*s*tc1 + P1_1*P2_2*dd1*dd2*(P1_1^2*dd1^2 - 2*P1_1*P1_2*dd1*dd2 + P1_2^2*dd2^2 + P2_1^2*dd1^2 - 2*P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_1^2*dd1^2 - 2*s*P2_1*P2_2*dd1*dd2 + s*P2_2^2*dd2^2);
All help is appreciated. -Cecilia.

Best Answer

Your 25000-plus-character result demonstrates the perils of attempting to always obtain a single symbolic expression as the solution to a problem. In this case you have nothing more than a quadratic equation in the unknown x to solve, and the best way to solve it is to do so in steps. Step one would be to evaluate the inverse matrix, P^-1, from P. The second step is to compute
T = P^-1*s^2*(P^-1).'
The third step is to use T, tc, and dd to compute the three coefficients in the quadratic equation - that is, to gather all the terms containing x^2, those containing x, and those independent of x. The fourth step is the trivial one of solving for the two roots of the resulting quadratic equation.
Each of these steps can be accomplished symbolically, but as you have found out the hard way, it is unwise to attempt to combine the results of these steps into a single symbolic expression. There is nothing wrong with expressing a solution in terms of a sequence of symbolic procedures.
Related Question