fminsearch of the sum of an array of anonymous functions with two inputs?
조회 수: 1 (최근 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
2019년 8월 24일
Note by the way that
MLW = @(par) 0;
would be better as
MLW = @(par) zeros(size(par));
채택된 답변
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))
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!