Sum of functions created by loop for minimization
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Martin Privara
am 26 Mär. 2019
Kommentiert: Matt J
am 26 Mär. 2019
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 Kommentare
Matt J
am 26 Mär. 2019
Is E a scalar? If so fminunc seems like overkill. You could just as well use fminsearch.
Akzeptierte Antwort
Matt J
am 26 Mär. 2019
Bearbeitet: Matt J
am 26 Mär. 2019
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 Kommentare
Matt J
am 26 Mär. 2019
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!