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 Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by