fsove is extremely slow
Show older comments
Long story short I am working on a problem where I have 35 equations and 35 unknowns where each unknown can be up to power 3 (e.g. f1=x1^3+x1^2*x2+...+x35^3, ... f35=...). I have tried to use both fsolve and lsqnonlin to solve solve this system, but the run time is extremely long! I'm talking only 2 steps within fsolve over the course of an hour. Is this time frame to be expected for a system like this, or am I implementing something poorly? The basiscs of my implementation are as follows:
xhat_0s=sym('x_0s',[numStateVars,1],'real'); %numStateVars=35
xhat_0s_1=[xhat_0s;1];
xhat_0s_1Mat=xhat_0s_1*xhat_0s_1';
%Code to calculate the arrays Lambda_sr (35x35x36x36) and N_sr (35x36) is
%not shown but takes place here, Lambda_sr and N_sr are dense and 4-D and
%2-D doubles respectively
expandxmat=permute(repmat(xhat_0s_1Mat,[1,1,35,35]),[3,4,1,2]);
finalLambda=sum(sum(Lambda_sr.*expandxmat,4),3);
%An equivalent but slower way to calculate finalLambda is:
% finalLambda=zeros(numStateVars,numStateVars);
% for ind1=1:numStateVars+1
% for ind2=1:numStateVars+1
% finalLambda=finalLambda+Lambda_sr(:,:,ind1,ind2)*xhat_0s_1Mat(ind1,ind2);
% end
% end
finalN=N_sr*xhat_0s_1;
funct=finalLambda*xhat_0s-finalN;
bigfunct=matlabFunction(funct,'Vars',{xhat_0s}); %this might be the bottleneck
residualFunct=@(x) bigfunct(x); %or maybe this is the bottleneck
initialGuess=zeros(numStateVars,1);
options=optimoptions('fsolve','Display','iter',...
'FunctionTolerance',1e-12,'StepTolerance',1e-16,...
'OptimalityTolerance',1e-12,'MaxIterations',50,...
'MaxFunctionEvaluations',1000);
[xhat_0_fsolve,fval,exitFlag_fsolve,~]=fsolve(residualFunct,initialGuess,options)
My hunch is that the bottleneck is how I am setting up the equation residualFunct to be used by fsolve, but I really don't know. Maybe this problem isn't feasible but I feel like it should be doable. I was able to run the same code for 15 unknowns and it took a while to run, but was still doable (maybe 30 minutes for fsolve to find a solution). Any help here would be much appreciated as this is the last test case that I am attempting to run for my thesis.
7 Comments
Walter Roberson
on 30 Mar 2025
For testing, we need several more variables, such as Lambda_sr and N_sr
"For testing, we need several more variables, such as Lambda_sr and N_sr"
"Code to calculate the arrays Lambda_sr (35x35x36x36) and N_sr (35x36) is not shown here..."
Also would have to have the functional code as well.
I would also guess that mixing in 35 symbolic variables with arrays of this size and fsolve is a lethal combination for compute time.
It would also be better to describe the problem from first principles rather than asking folks to debug complex code that isn't working well...the approach may end up the same or similar, but generally speaking, the way to make orders of magnitude improvement in code performance is by modifying the algorithms rather than optimization of existing shown to be slow code---although my first approach would be to remove the Symbolic TB from the problem and go with a purely numerical solution -- after, of course, ensuring the correctness of the system function code first.
Walter Roberson
on 30 Mar 2025
My guess is that you would see performance improvements if you used
bigfunct = matlabFunction(funct, 'file', 'bigfunct.m', 'Vars',{xhat_0s}, 'optimize', true);
However I am uncertain whether optimize true is reliable in R2023b; in older versions it was not reliable.
Jacob
on 30 Mar 2025
Jacob
on 30 Mar 2025
Walter Roberson
on 30 Mar 2025
It is true that the optimization phase can take a very long time. In theory the optimization time required rises proportional to the square of the size of the expression.
Given the need for ODE solver, one algorithm alternative could be <Faster Ordinary Differential Equations Solvers>.
Of course, a smaller profiling run to discover where the actual performance bottle neck(s) is(are) first would be a first step in finding out what piece(s) of the code to concentrate on...reducing a 1% area by 50% won't make a noticeable change overall; finding a hot spot that is 70% of the time spent would be something different, indeed.
Accepted Answer
More Answers (0)
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!