bugGNU Octave - Bugs: bug #67313, Document how to use fsolve with...

 
 

bug #67313: Document how to use fsolve with cell input for objective and jacobian

Submitter:  Liang Tang <lt1234>
Submitted:  Sun 13 Jul 2025 12:11:12 AM UTC
   
 
Category:  Documentation Severity:  2 - Minor
Priority:  5 - Normal Item Group:  Documentation
Status:  Confirmed Assigned to:  None
Originator Name:  lt1234 Open/Closed:  * Open
Release:  * 9.3.0 Operating System:  * Any
Fixed Release:  None Planned Release:  10.3.0
* Mandatory Fields

Post a Comment

Add a New Comment Rich Markup
   

Discussion

Jump to the original submission

Fri 31 Oct 2025 12:30:33 PM UTC, comment #11: 

Hi I am new to open source contribution and was looking for my first contribution. Can I work on this?

raunit26 <raunit26>
Wed 06 Aug 2025 12:48:23 PM UTC, comment #10: 

I correct myself. If "fun" input is a cell, it makes sense that the "Jacobian" option is "on" by default. However, it can be set to "off" by the user. If "fun" is not a cell, the "Jacobian" option should be "off" by default.

Here's a patch to do that (to be applied on top of the previous patch, from comment #9).

(file #57501)

Fernando <tutissanalio>
Tue 05 Aug 2025 06:41:40 PM UTC, comment #9: 

Here's a possible patch for it.

However, there something strange in the behavior that could be changed: when "fun" input is a cell of two functions, the second one computes the Jacobian, but it is not taken into account if the "Jacobian" option is not set to "on". Maybe it makes more sense to always take into acount the second function, if there is one, regardless of the "Jacobian" option.

(file #57500)

Fernando <tutissanalio>
Fri 25 Jul 2025 06:28:28 PM UTC, comment #8: 

My opinion is that fmincon is too important to be supported as a community package.  Thanks. 

This is my suggestion of added wording to the document. It is derived from sqp documentation. The assumption is that there is no fsolve code change. 


The first argument fcn may also be a two cell array of function handles. The first element of the cell should point to the objective function and the second should point to a function that computes the gradient of the objective function.  The user provided Jacobian function in the cell array is activated by setting the parameter "Jacobian" in the options to "on".  If parameter "Jacobian" is not specified, the gradient is computed by finite differences.

Liang Tang <lt1234>
Fri 25 Jul 2025 09:07:12 AM UTC, comment #7: 

Answering questions in comment #6.

The origin of the split between core functions and package functions lies with Matlab.  They put a small number of optimization functions into their core product, and then put extended versions and more functions into their optimization toolbox (for which they could charge more money).

Octave doesn't charge money for its software so we don't have the same motivation.  Nevertheless, it is easier for Matlab users if we mimic the same structures and division of functions.

Matlab has also evolved over time.  They started out with the function optimset() in their core package, and then extended that function in the optimization package.  This has been confusing and hasn't worked well.  They are now trying to get programmers to use optimoptions().

As Fernando has said, Octave does support a user-specified Jacobian just as Matlab does (https://www.mathw ... im/ug/fsolve.html).

If you could suggest a change in language for the documentation we can make that change in Octave.

Rik <rik5>
Group administrator
Mon 14 Jul 2025 09:47:44 PM UTC, comment #6: 

For fsolve documentation item, I would like to take advantage this opportunity to get some historical background on Octave core optimization and package optim. 

(1) what separated them into two?
(2) why would they not merge by now?

For fsolve cell input, my opinion is that option.Jacobian should apply to ML alike interface only, i.e. a m file with fval and fjac outputs.  If func is a cell with two components, it should be treated similar to how sqp handles cell.  Or sqp should handle cell input similar to fsolve.

Thanks,

Liang Tang <lt1234>
Mon 14 Jul 2025 05:53:02 PM UTC, comment #5: 

comment #4:

> Thanks.  I just did not realize that jacobian-on also interacts with cell input. 
>
> I would recommend that documentation update.  If cell works, it helps a lot.
>


You mean updating the documentation to clarify that you can pass a cell array with two functions, right? I agree.

> I think the bug report system will require me to open a new ticket for outputfun option.  fsolve only calls outputfcn with "iter". It should be "init", "iter" and "done".


Yes, I think that should go to another bug report.

Fernando <tutissanalio>
Sun 13 Jul 2025 04:02:14 PM UTC, comment #4: 

Thanks.  I just did not realize that jacobian-on also interacts with cell input. 

I would recommend that documentation update.  If cell works, it helps a lot.

I think the bug report system will require me to open a new ticket for outputfun option.  fsolve only calls outputfcn with "iter". It should be "init", "iter" and "done".

Liang Tang <lt1234>
Sun 13 Jul 2025 02:35:29 PM UTC, comment #3: 

Please note that, as I said, you also have to use the option "Jacobian" set to "on", i.e.:


>> [X, FVAL, INFO, OUTPUT, FJAC]=fsolve({f jac}, X0, optimset("Jacobian","on"))
X =

   0
   0
   0

FVAL =

   0
   0
   0

INFO = -2
OUTPUT =

  scalar structure containing the fields:

    iterations = 1
    successful = 0
    funcCount = 2

FJAC =

Diagonal Matrix

   0   0   0
   0   0   0
   0   0   0


Fernando <tutissanalio>
Sun 13 Jul 2025 12:51:51 PM UTC, comment #2: 

Thanks. 

Please run this simple case below and see if you get the same as I did. function jac, if exercised, should provide a zero jac matrix.  The jac below appears to come from finite diff. 

>> f=@(x) x.^2; jac=@(x) 2*diag(x); X0=[0 0 0]';
>> [X, FVAL, INFO, OUTPUT, FJAC]=fsolve({f jac}, X0)

X =

   0
   0
   0

FVAL =

   0
   0
   0

INFO = 1
OUTPUT =

  scalar structure containing the fields:

    iterations = 2
    successful = 0
    funcCount = 5

FJAC =

   1.4901e-08            0            0
            0   1.4901e-08            0
            0            0   1.4901e-08

Liang Tang <lt1234>
Sun 13 Jul 2025 12:08:25 PM UTC, comment #1: 

fsolve does accept a cell input to pass both the function and the jacobian. Unfortunately, this is not documented, as far as I can see.

You can call fsolve in this way, e.g.:


[x, fval, info, output] = fsolve ({@f, @fj}, [ 0.5; 2.0; 2.5 ], optimset ("Jacobian", "on"));


where f is the function corresponding to the nonlinear equations and fj is the function for the jacobian. You also have to use the option "Jacobian" set to "on, to indicate that there is a function to compute the Jacobian.

Maybe this could be improved, because the "Jacobian" option seems unnecessary here (the user is clearly providing a function to evaluate the Jacobian).

On the other hand, you can also have normal functions that use parameters that are not input arguments. E.g., just copy this in the command window:


function f = setfun (a,b,c)
  function fx=fun(x)
    fx = a*x^2+b*x+c;
  end
  f = @fun;
end

f = setfun (3, 2, 1);


Then, f has only one input argument x, but it will use values of parameters a=3, b=2 and c=1. E.g.:


octave:> f(3)
ans = 34


Fernando <tutissanalio>
Sun 13 Jul 2025 12:11:12 AM UTC, original submission:  

A number of Matlab/Octave optimizers accept user objective function, jacobian and hessian. To pass in user's own parameters in the user functions call sequence is a very well known issue.
If only objective function is used, anonymous function provides a solution.  However, anonymous function has only one output.  When jacobian or hessian is also provided by the user, I am not aware of any relief without going back to the standard interface plus global variable etc usage to pass parameters. 

Octave sqp allows a very convenient cell input, which contains both objective and jacobian. Anonymous function together with the cell input significantly reduces the bad code to write.  However, the same cell input format is not accepted in fsolve.  This note is to request your consideration for adding the cell input functionality to fsolve. 

I don't full understand what fsolve is supposed to do.  I can only modify fsolve to meet what I need.

Attached please find a slightly modified version of fsolve.  A few points to note:
(1) the modification is very limited. 
(2) the inclusion of cell input does not interference with the existing fsolve input format.

Thanks.

Liang Tang <lt1234>

 

Attached Files

Attached Files
file #57501:  bug67313-2.cset added by tutissanalio (4.0KiB - application/octet-stream)
file #57500:  bug67313.cset added by tutissanalio (3.3KiB - application/octet-stream)
file #57405:  fsolve.m added by lt1234 (24KiB - application/octet-stream)

(Note: upload size limit is set to 4.0MiB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

 

Dependencies

This item does not depend on any other items.

No items depend on this one.

 

Mail Notification Carbon-Copy List

Carbon-Copy List
  • -email is unavailable- added by raunit26 (Posted a comment)
  • -email is unavailable- added by raunit26 (Subscribed to discussion)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by tutissanalio (Posted a comment)
  • -email is unavailable- added by lt1234 (Submitted the item)
  •  

    Votes

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    History

    Follow 11 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2025-10-31 raunit26 Carbon-Copy- Added raunit26
    2025-08-06 tutissanalio Attached File- Added bug67313-2.cset, #57501
    2025-08-05 tutissanalio Attached File- Added bug67313.cset, #57500
    2025-07-25 rik5 CategoryOctave Function Documentation
        Severity3 - Normal 2 - Minor
        Item GroupFeature Request Documentation
        StatusNone Confirmed
        Operating SystemMicrosoft Windows Any
        Planned ReleaseNone 10.3.0
        Summaryfsolve with cell input for objective and jacobian Document how to use fsolve with cell input for objective and jacobian
    2025-07-13 lt1234 Attached File- Added fsolve.m, #57405

    Back to the top

    Powered by Savane 3.16-1b88.
    Corresponding source code