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!