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

조회 수: 2 (최근 30일)
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
Walter Roberson
Walter Roberson 2019년 8월 24일
Note by the way that
MLW = @(par) 0;
would be better as
MLW = @(par) zeros(size(par));

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2019년 8월 24일
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
Riccardo Sorvillo
Riccardo Sorvillo 2019년 8월 25일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by