Error "Jacobian not correct size" while trying to supply system's Jacobian within fsolve
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hello,
I am having some trouble supplying the Jacobian matrix of a system of non-linear equations to fsolve. I thought adding "jacobian" to the fsolve output would generate the system's Jacobian (which it does); however, whenever I add either " 'SpecifyObjectiveGradient',true " or " 'Jacobian','on' " to the solver's options, hoping that it would recognise jacobian as the Jacobian matrix, I receive the following error:
Supplied Jacobian is not the correct size: the Jacobian matrix should be 562-by-593.
The code I have is as follows:
options = optimoptions('fsolve','Display','iter','Algorithm','Levenberg-Marquardt','SpecifyObjectiveGradient',true);%'Jacobian','on');
.
.
.
[xss,x,exitflag,output,jacobian] = fsolve(xss,x0,options); % Where xss is a function handle, function with simultaneous equations in another file
If I run the simple check
size(jacobian)
I get the size mentioned above, i.e. 562x593:
ans =
562 593
If jacobian has the correct dimensions but it is not recognised by options, does this mean that I have to supply the Jacobian manually? And if so, how may I go about doing it? I tried doing so, but I did not know where to place the 'jacobian' function itself. I am assuming it should be placed at the end of the function file, but I never managed to generate it successfully. For reference, my function file, when summarised, looks like this:
function [xss,xout] = function_name(x,inputs)
% Endogenous variables
A = x(1:12);
.
.
.
Z = x(587:593);
%Equations
xss1 = (equation here); xss{1} = xss1;
.
.
.
xss59 = (equation here); xss{59} = xss59;
xss = cell2mat(xss);
xout = [(vector of endogenous variables)];
end
Would anybody be able to help with this? I have had a look at the documentation for both jacobian and fsolve, but as I could not find an example similar enough to mine I could not figure it out. Thank you very much for the time and help!
0 Commenti
Risposta accettata
Matt J
il 30 Ago 2021
Modificato: Matt J
il 30 Ago 2021
When you set SpecfiyObjectiveFunction=true it means you are planning to do your own manual calculation of the Jacobian within your objective function. Assuming you are not using the JacobianMultiplyFcn or JacobPattern options, your objective function code should look like this.
function [xss,jacobianMatrix] = function_name(x,inputs)
...
xss=____
if nargout>1
jacobianMatrix=_______
end
end
In the present case, the output jacobianMatrix should be 562x593.
3 Commenti
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!