MATLAB solve function is taking a very long time

81 views (last 30 days)
Hello,
I have a system of 17 equations and 23 unknowns and I'm using the MATLAB "solve" function to solve symbolically for 17 unknowns in terms of the other 6 unknowns. However, the "solve" function has been running for several hours and still has not gotten a solution.
I have used the MATLAB "solve" function in this way for several small systems of equations and it finds a solution very quickly (within minutes). Does anyone have any ideas as to why the "solve" function is taking so long in this instance? What is generally the upper limit of equations that the "solve" function can solve in a reasonable amount of time? (I know there is an upper limit to the number of equations that can be handled, but I'm very surprised that 17 equations is taking so long.) Does anyone have any ideas as to anything I can try (maybe options to use with the solve function, or some other function to use) in order to get a solution for my system of equations?
Thank you,
Kevin

Accepted Answer

Walter Roberson
Walter Roberson on 10 Apr 2013
The upper limit to the number of equations that solve() can resolve in a reasonable amount of time depends upon whether the equations are linear or not. If they are non-linear (including even just squared terms) then 4 can usually be done fairly quickly, 5 is about the practical limit, and 6 might take days.
The limits are slightly higher if you happen to have the Maple based symbolic toolbox and are using just squared terms, but even for that, 6 might take overnight and 7 is unlikely to finish after several days.
  6 Comments
Kevin Bachovchin
Kevin Bachovchin on 11 Apr 2013
In the case which is taking a very long time, my system is non-linear. In my previous comment, I just wanted to clarify what was meant by linear. I've done some examples with linear equations too, and it's solved very quickly even for over 10 equations.
In the non-linear case, I did one example with 8 equations and it worked relatively quickly (a few minutes). I guess 17 equations is too computationally intensive though for the non-linear case. Are there any alternative functions that might worth trying or is it hopeless to try to solve the system of equations?
Kevin Bachovchin
Kevin Bachovchin on 11 Apr 2013
Edited: Kevin Bachovchin on 11 Apr 2013
Also, this is unrelated, but I thought maybe you might be able to help. I'm running into an issue with using a symbolic expression in an equation. I have one function that creates a symbolic expression, which is often lengthy. In another function used in conjunction with the differential equation solver ode45, I am loading that symbolic expression and would like to evaluate that symbolic expression based upon parameters defined above.
For example, in my first function I solve for the symbolic expression SymExpr = a*b*c. In my second function, I have a, b, and c defined as numeric values. When I just copy paste in the expression for x, it takes less than a minute for ode45 to run. However when I load the saved symbolic expression SymExpr and use x=subs(SymExpr) or x=eval(SymExpr), it ran for over an hour without finishing.
I know I could do this by just copying and pasting in the second function x = a*b*c, but my expressions from the first function can get very lengthy (in fact, so long that it can exceed the maximum line length and get truncated when printed to the screen), so I'm hoping to do this without having to copy/paste. It seems like if I can copy and paste and get the result quickly, that there should be some automated way to do this.
Any help you could give me on this would be greatly appreciated.

Sign in to comment.

More Answers (1)

Ahmed A. Selman
Ahmed A. Selman on 10 Apr 2013
Sometimes trying the (simplify) command successively after each solve command, helps making the syms math incredibly faster. It helps when the result of a mathematical operation in a certain operation is to be used in a following one. For example, it might take long time to find D if B was a complicated expression in:
...
B=solve(function_Of_x_y_z);
D=solve(function_of_B);
adding the simplify usually helps reducing the runtime as:
...
B=solve(function_Of_x_y_z);
B=simplify(B);
D=solve(function_of_B);
even multiple simplifies can be useful as:
...
B=solve(function_Of_x_y_z);
B=simplify(simplify(B));
D=solve(function_of_B);
Note that simplify command by itself takes some extra time to finish, yet it helps since syms math operations depends on the length of the expressions. So
u = x^2 + 4*x + 4*cos(2*x)^2 + 4*sin(2*x)^2; % This is a lengthy expression
u = simplify(u);% Results, equivalently, u = (x + 2)^2.
  2 Comments
Kevin Bachovchin
Kevin Bachovchin on 10 Apr 2013
I'm only using one "solve" command. For example
Solution = solve(Equations==0, XSolveCell{:})
where Equations is a 17x1 vector and XSolveCell is a cell array consisting of the 17 unknowns I want to solve for.
Ahmed A. Selman
Ahmed A. Selman on 10 Apr 2013
17 unknowns with a SINGLE equation? How is that mathematically possible.. even if they are in a linear form. Besides, suppose I have the equation:
y(x)= a*x+c,
it might be mathematically equivalent to, e.g.,
y(x1,x2)= a*x1 + b*x2 + c,
if we let A={a,b}, X={x1,x2}, then Y={y(x1,x2)} leading to
Y=A*X +b .. etc,
But, the function y(x) can be solved when
y(x)=0 -- single variable (x), with a single condition
but not
y(x1,x2)=0 -- two variables, with a single condition.
Simply, it is impossible to solve for x1 or x2 only (as x1=a or x2=b/c.. etc). The only possibility is to have a solution like :
x1= -(c+b*x2)/a -- still a function of x2.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!