Sum of functions created by loop for minimization
1 回表示 (過去 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
2019 年 3 月 26 日
Is E a scalar? If so fminunc seems like overkill. You could just as well use fminsearch.
採用された回答
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 件のコメント
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 Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!