A question of speed - Evaluate a function repeatedly or solve for it once

1 view (last 30 days)
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

UJJWAL
UJJWAL on 26 Sep 2011
Hi !!! This is a question of efficiency and cannot be answered objectively. You do understand that the efficiency of a process depends heavily on the application and the local requirements. Sometimes we calculate something we need instantanously and sometimes we store them at a single place and are happy with them.. The basic constraints in programming and modelling are two :- Time and Space. That is why complexity is calculated in these terms. Now everytime we want the time and space complexity to be as less as possible. Now sometimes this must be possible but in most of the cases it is not. Normally, when speed is the concern, we keep things stored in a single place after solving them as variables and then we retrieve them each time we want them. (NOTE :- In this case it is also important that how complicated the calculations can get by following this approach. In some cases of Electromagnetics I have seen things getting too nasty.) However in the case of memory constraints we prefer to use things on site rather than storing them. The way you are explaining things, it seems that it is better for you to calculate the parameters separately and store them conveniently sat some location and use them whenever required. But again it depends on the local requirements of your applications.
For your case my final answer is that since the number of equations is quite large it is best to solve for the parameters before rather than calculating them as and when required . Hope this assists you.
Happy to Help
Ujjwal

More Answers (2)

Walter Roberson
Walter Roberson on 26 Sep 2011
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.

Jakob Rabjerg Vang
Jakob Rabjerg Vang on 5 Oct 2011
I tried comparing my new code to the old code. The old one calculates the parameters each time they are used. The new code introduces a new variable for each parameter and the old one calculates the parameters each time they are used. It turns out that the speed of the old code is much fater than the new one. A factor of three (3!) approximately. It seems that the overhead from introducing a new variable is far larger than evaluatin a function many times, unless the function is a really nasty one.

Products

Community Treasure Hunt

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

Start Hunting!