MATLAB Answers


Too many output parameters error in Fsolve when I turn Jacobian On

Asked by farzad on 6 Jun 2019
Latest activity Commented on by Walter Roberson
on 7 Jun 2019
Hi all
why do I get Too many output parameters error in Fsolve when I turn Jacobian On ? I have 9 variables to solve , where can be the problem ? and does the Jacobian matrix calculation help me if this is the iteration report ? :
Iteration Func-count f(x) step optimality radius
0 10 6.09647e+006 3.27e+005 1
1 20 5.00637e+006 1 2.98e+005 1
2 30 2.7289e+006 2.5 2.24e+005 2.5
3 40 37481.6 6.25 1.99e+004 6.25
4 50 5.77916 1.28439 192 15.6
5 60 2.82227e-007 0.0169644 0.0368 15.6
6 70 1.34282e-021 4.00165e-006 3.17e-009 15.6
7 80 8.78879e-025 2.98649e-013 1.58e-010 15.6


farzad - please show the code that generates this error so that we can see what you are passing into the function and what you are expecting as outputs. The error messages is telling you that are expecting more output parameters than will actually be returned by the function.
Sorry can't share the code, I am solving for 9 variables, are they too many for Matlab 2007 ?

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 7 Jun 2019
 Accepted Answer

When you turn on Jacobian, then you need to return the Jacobian as a second output, but only when it is asked for. You need to test nargout for that.
function [obj, J] = fun_to_solve(x)
obj = x.^2 - 10*x + 5;
if nargout > 1
J = 2.*x - 10;


Show 1 older comment
You should change your function so that it only assigns to the second variable if nargout > 1 . Your fsolve() call would not change.
x.^2 - 10*x + 5 is just some function to be solved, which I put in as a place holder. Your actual function is not named fun_to_solve, and your actual variables are not likely named obj and J, so the whole thing is intended to illustrate the pattern you should follow.
You cannot share the code, so I cannot possibly post the code with the exact change in terms of your variable names.
Thank you
Here you habe one function and the jacobian is the derivative of that one function, in my case I have muktuple functions, how Wil my Jacobian look like?
"If fun returns a vector (matrix) of m components and x has length n, where n is the length of x0, the Jacobian J is an m-by-n matrix where J(i,j) is the partial derivative of F(i) with respect to x(j). (The Jacobian J is the transpose of the gradient of F.)"

Sign in to comment.