MATLAB Answers

fminsearch of the sum of an array of anonymous functions with two inputs?

3 views (last 30 days)
Hello world!
I'm facing some issues in finding the minimum of a function that comes from the sum of anonymous functions. I feel it's a silly thing, most probably in the anonymous function creation with which I still need some practice.
Real code is starting from data collected from measurements: for the sake of simpllicity I've just inserted a generic array.
d = 0:10:1000; n = length(d)
xg_w = [200 1]; % Initial guess
RO = 800; % run-outs
e = @exp;
L_W_temp = cell(n,1);
z1 = @(par) (log(RO)-log(par(1)))*par(2);
MLW = @(par) 0;
for i = 1:n
if d(i)>=RO
L_W_temp{i} = @(par) -e(z1(par(1),par(2)));
else
z2 = @(par) (log(d(i))-log(par(1)))*par(2);
L_W_temp{i} = @(par) log(par(2))-e(z2(par))+z2(par)-log(d(i));
end
MLW = @(par) MLW(par)-L_W_temp{i}(par);
end
x_weib = fminsearch(MLW,xg_w);
I needed to create function "e" since it returned me errors when I tried to use "exp(z1(par(1),par(2)))".
Anyway, main problem is with fminsearch function, which shows me the error:
Error using springsL3>@(par)(log(RO)-log(par(1)))*par(2)
Too many input arguments.
Error in springsL3>@(par)-e(z1(par(1),par(2)))
Error in springsL3>@(par)MLW(par)-L_W_temp{i}(par)
Error in springsL3>@(par)MLW(par)-L_W_temp{i}(par)
Error in springsL3>@(par)MLW(par)-L_W_temp{i}(par)
Error in springsL3>@(par)MLW(par)-L_W_temp{i}(par)
Error in springsL3>@(par)MLW(par)-L_W_temp{i}(par)
Error in springsL3>@(par)MLW(par)-L_W_temp{i}(par)
Error in springsL3>@(par)MLW(par)-L_W_temp{i}(par)
Error in springsL3>@(par)MLW(par)-L_W_temp{i}(par)
Error in springsL3>@(par)MLW(par)-L_W_temp{i}(par)
Error in springsL3>@(par)MLW(par)-L_W_temp{i}(par)
Error in fminsearch (line 189)
fv(:,1) = funfcn(x,varargin{:});
Error in springsL3 (line 46)
x_weib = fminsearch(MLW,xg_w);
where "springsL3" is the name of the script in my pc.
Thank you for your help,
Riccardo
  1 Comment
Walter Roberson
Walter Roberson on 24 Aug 2019
Note by the way that
MLW = @(par) 0;
would be better as
MLW = @(par) zeros(size(par));

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 24 Aug 2019
z1 = @(par) (log(RO)-log(par(1)))*par(2);
so z1 is defined with one argument.
L_W_temp{i} = @(par) -e(z1(par(1),par(2)));
and here you are calling z1() with two arguments, You should be using @(par) -e(z1(par))
  1 Comment
Riccardo Sorvillo
Riccardo Sorvillo on 25 Aug 2019
Thank you very much, Mr. Walter Roberson: I knew it was a silly mistake hidden somewhere.
I've also appreciated the suggestion about MLW initiliazation.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by