Sum of functions created by loop for minimization

2 次查看(过去 30 天)
I have a cell 4x4 of functions of activation energy E named s. I am attempting to make a function of sum of all of the elements to be subject to minimization by function 'fminunc'. But it appears all sum Matlab functions need values, not functions as input so I run to errors. Could you please help with some nice command? Thanks.
p.s.
I'm trying to avoid creating additional function files.
fun = @(T)exp((-E/R)./T);
%for i=1:no_alphas
%Ti = T_sets(i,:);
Ti = [350 400 450 500];
%see function activation min
for j=1:no_rates
for k=1:no_rates
if j~=k
% creates a cell array n x n of all functions
part = @(E)(integral(fun,Tmin,Ti(j))*(beta(k)))/(integral(fun,Tmin,Ti(k))*beta(j));
s{j,k} = @(E)(integral(fun,Tmin,Ti(j))*(beta(k)))/(integral(fun,Tmin,Ti(k))*beta(j)); % cell 4x4 of functions
summm = @(E)(summm(E) + part(E)) % sum of functions not working
%cumsum(s) % does not work
end
end
end
[E0,fval] = fminunc(summm,70e3)
  2 个评论
Matt J
Matt J 2019-3-26
Is E a scalar? If so fminunc seems like overkill. You could just as well use fminsearch.
Martin Privara
Martin Privara 2019-3-26
E should be a scalar after it is found by minimization

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2019-3-26
编辑:Matt J 2019-3-26
I'm trying to avoid creating additional function files.
Then why not an anonymous function or a local function, e.g.,
function [E0,fval] = myOptimization
R=...
T=...
Tmin=...
beta=...
Ti = [350 400 450 500];
[E0,fval] = fminunc(@summm,70e3)
function out=summm(E) %Anonymous
fun = @(T)exp((-E/R)./T);
I=Ti; %pre-allocate
for n=1:numel(I)
I(n)=integral(fun ,Tmin,Ti(n))*beta(n);
end
parts=I./I(:);
parts(1:n+1:end)=0; %exclude j==k
out=sum(parts(:));
end
end
  3 个评论
Martin Privara
Martin Privara 2019-3-26
I'm trying the new version you just uploaded but I'm getting an error because E is not defined. Regardless, I've solved it using another function file in similar way like you are suggesting yesterday.
Matt J
Matt J 2019-3-26
Don’t run your optimization in a script. Make it it’s own function file. Or, take the advice of the error message and move function definitions to appropriate locations.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by