fsove is extremely slow

5 views (last 30 days)
Jacob
Jacob on 30 Mar 2025
Commented: dpb on 1 Apr 2025
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
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.
dpb
dpb on 31 Mar 2025
Edited: dpb on 31 Mar 2025
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.

Sign in to comment.

Accepted Answer

Jacob
Jacob on 1 Apr 2025
After days of testing I was able to rework my code so that it runs extremely quickly! With all the ode113 calls included my new way of doing things was able to go through 10 complete runs in approximately 3 hours. Again that is with the slow ode113 calls included, so the fsolve component is function MUCH quicker.
My problem (as usual) turned out to be the use of symbolic variables. I thought since I only used them once in the definition of residualFunct that they wouldn't be a problem. This wasn't the case however as it appears that they were slowing down fsolve drastically. I was able to resolve this issue by rewriting my code so that residualFunct was defined with exclusively function handles.
I guess I had to learn the hard way for the hundredth time to not use symbolic variables for actual calculations lol
  1 Comment
dpb
dpb on 1 Apr 2025
I was pretty sure the symbolics would be getting in the way even without the details of the code... :J>
Glad to hear you were able to recode numerically and solve the problem...thanks for posting the resolution.

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!