fsove is extremely slow

조회 수: 5 (최근 30일)
Jacob
Jacob 2025년 3월 30일
댓글: dpb 2025년 4월 1일
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
Walter Roberson
Walter Roberson 2025년 3월 30일
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 2025년 3월 31일
편집: dpb 2025년 3월 31일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Jacob
Jacob 2025년 4월 1일
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
dpb
dpb 2025년 4월 1일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by