Symbolic equation is too long to be displayed.

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*P2_1*P2_2*dd1*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2*s*P2_1*P2_2*dd1*dd2 + s*P2_2^2*dd2^2);
All help is appreciated. -Cecilia.

 Accepted 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.

4 Comments

Ok. I did it your way.
First, I found T = P^-1*s^2*(P^-1).'.
Then, I defined W = tc./dd,and U = 1./dd.
Then, I write v as: (W-x*U)*T*(W.'-x*U.') = x^2*(U*T*U.') + x*(-U*T*W.'-W*T*U') + W*T*W.'. and found the three coefficients in the quadratic equation a*x^2 + b*x + c, where a = (U*T*U.'), b = (-U*T*W.'-W*T*U') and c = W*T*W.'.
And solve the two roots: xpluss = (-b + sqrt(b^2-4*a*c))/(2*a); xminus = (-b - sqrt(b^2-4*a*c))/(2*a);
The answer is not the same as the answer I get using the solve function. I can not find an error. Have i done something wrong here? - Cecilia.
When you defined t2 you wrote t2 = (tc - x)./dd, and that carries with it an inherent ambiguity wherein you are subtracting a 1 x 1 vector, namely a scalar, from a 1 x 3 vector. On the numerical side of matlab that would produce an error message. Presumably you meant that the scalar x should be subtracted from each of the three components of tc before being divided by the corresponding three elements of dd. Are you sure your symbolic toolbox was interpreting it that way? On my own (admittedly very ancient) version with one interpretation of subtraction it only subtracts x from the first element. That is something for you to check on.
In any case you should be able to determine by plugging your values for x into the original 'v' whether it gets a zero or not with the two differing answers you are getting, and that should indicate which procedure is incorrect. I would think that is the first step to take in resolving the discrepancy.
It subtracts x from all the elements, that is:
t2 =
[ (tc1 - x)/dd1, (tc2 - x)/dd2, (tc3 - x)/dd3].
Solving the problem using solve(v,x) gives the correct answer, but it takes nearly two hours solving the whole thing. Solving it the way you suggested takes 5 minutes, but the answer is not correct. I do not understand why.
Oh, I forgot the -1 in the c-term, c = W*T*W.'-1. Im so sorry bothering you, everything works now, thank you for your help.

Sign in to comment.

More Answers (0)

Asked:

on 18 Aug 2014

Commented:

on 27 Aug 2014

Community Treasure Hunt

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

Start Hunting!