Hi Anton,
To resolve the error, you need to update the function handle so that the method is bound to the object instance. Instead of writing:
fminunc(@(x) F(obj, x), x0, options);
resolve
fminunc(@(x) obj.F(x), x0, options);
Here is a code that illustrates the change in context:
classdef MyProblem < handle
properties
N % Number of variables
end
methods
% Constructor to set the number of variables
function obj = MyProblem(N)
obj.N = N;
end
% Objective function (for example, the sum of squares)
function f = F(obj, x)
f = sum(x.^2);
end
% Optimization method using fminunc
function [x, fval] = runOptimization(obj, x0)
options = optimoptions('fminunc', 'Display', 'iter');
% Bind the function handle to the object using dot notation
[x, fval] = fminunc(@(x) obj.F(x), x0, options);
end
end
end
By using ‘@(x) obj.F(x)’, MATLAB correctly recognizes ‘F’ as a method of ‘obj’, which avoids the "Dot indexing is not supported" error.
For a better understanding of the above solution, refer to the following MATLAB documentation: