A question of speed - Evaluate a function repeatedly or solve for it once
Show older comments
Hi people
I am developing a quite large steady state fuel cell model in Matlab. It consists of a number of nonlinear equations, which I solve using fsolve.
I have a limited number of variables but I have a lot of parameters that depend on the variables. The parameters are calculated from the variables and other parameters using separate functions.
My question is whether the most efficient solution is to evaluate the functions that calculate the parameter values each time a parameter is needed or to treat the parameters as variables and solve for them also.
The difference in the code for the function supplied to fsolve would look something like this:
function sol=model(X)
%Version with parameter functions evaluated everywhere they are used
sol(1) = current(X(1),X(2),parfunc1(X(1),X(2)));
sol(2) = current(X(1),X(3),parfunc2(X(1),X(2),parfunc1(X(1),X(2))));
sol(3) = current(X(1),X(2),X(3),parfunc1(X(1),X(2)),...
parfunc2(X(1),X(2),parfunc1(X(1),X(2))),...
parfunc3(X(1),X(2),X(3)));
And:
function sol=model(X)
%Version where parameters are treated as variables
sol(1) = current(X(1),X(2),par(1));
sol(2) = current(X(1),X(3),par(2));
sol(3) = current(X(1),X(2),X(3),par(1),par(2),par(3));
sol(4) = par(1) - parfunc1(X(1),X(2));
sol(5) = par(2) - parfunc2(X(1),X(2),par(1));
sol(6) = par(3) - parfunc3(X(1),X(2),X(3));
The second approach is of cause much more tidy and easy to read but speed is the most important factor in my case.
My real code has between 1000 and 3000 equations depending on my choice of discretisation. I currently evaluate my parameter functions each time I use them.
I know that there probably is no simple answer but if anyone has a rule of thumb for when each solution is the more efficient I would be happy. :-)
Regards
Jakob Rabjerg Vang
Accepted Answer
More Answers (2)
Walter Roberson
on 26 Sep 2011
0 votes
If you have 1000 to 3000 non-linear equations, then MuPad's solve() routine is likely to take a very very long time. The solve() time usually grows exponentially in the number of variables, with the exponent base being proportional to the order of the equation (for polynomials). Usually about 5 non-linear equations can be solve()'d in a "reasonable" time, usually going to 6 is plausible if you don't mind waiting 40 or so minutes. I find that in general, the jump to 7 non-linear equations is often the killer, often taking more than a day to solve (if it doesn't fill up memory completely). Unless each equation is fairly simple, by the time you get to 10 non-linear equations, computing time is often in the "months".
Equations that are sparse in each variable might take less time than generalized above.
1 Comment
Jakob Rabjerg Vang
on 27 Sep 2011
Jakob Rabjerg Vang
on 5 Oct 2011
0 votes
Categories
Find more on Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!