fsove is extremely slow
5 次查看(过去 30 天)
显示 更早的评论
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
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
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.
采纳的回答
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!