Error using fminunc with array as function output and multiple inputs
1 次查看(过去 30 天)
显示 更早的评论
We are trying to optimize three parameters using fminunc.
Our function contains two for-loops and gives us a probability over time in a vector.
Our parameters for optimization are used in this function as x(1), x(2), x(3)
function P = probabilityFunction(x,T,Tmed)
P = ones(length(T),1)*x(3);
for t=0:T(end)
numPills = sum(Tmed < t);
for i=1:numPills
P(t) = P(t) - x(1)*exp(-x(2)*(t-Tmed(i)));
end
end
end
When we try to run the fminunc, it gives us the error "Supplied objective function must return a scalar value"
We call the fminunc like this:
% pooling the data into one array and sorting
Tmed = [deltaT_mo deltaT_ev]';
Tmed = sort(Tmed);
T = (1:max(Tmed))';
noDrug = 0.5;
E = 0.0025;
v = 0.009;
x0 = [E; v; noDrug];
%%
f = @(x)probabilityFunction(x,T,Tmed);
%%
[x,fval] = fminunc(f,x0);
Should we use another function or is there a way to make the probability as a scalar?
- Thanks
0 个评论
回答(1 个)
Alan Weiss
2019-2-27
It is not clear what you are trying to do. Your P(x,T,Tmed) is not a scalar, so it is not clear what it means to minimize it.
You might be looking for a least-squares minimization, something like sum(P.^2). If so, you can take your existing objective function and ask lsqnonlin to minimize it. Or you can reformulate your objective function as the sum of squares. For details, see Nonlinear Data-Fitting.
Alan Weiss
MATLAB mathematical toolbox documentation
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear Least Squares 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!