Finding Minimum value of an anonymous function using fminbnd
19 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Emily Davenport
le 8 Sep 2020
Commenté : Emily Davenport
le 8 Sep 2020
Hi,
I am trying to find the minimum value of my anonyus function G, which depends on the variable na. I am getting the error "User supplied obective function mus return a scalar value" when I try to run my code. From some debugging, I can see that if I type G(0) in the command line, I get a 1X1000 double that is just the correct value for G(na=0), but repeated 1000 times. Can anyone help me fix this? Thanks for any advice, I've never posted on here before.
clear all
% Given :
R = 1.987; %[cal/mol*K]
T = 500; %[K]
G0 = -1000; %[cal/mol]
%% Find values to plot G vs. E
% Calulate na for a bunch of E:
% E: [0,1]
E = linspace(0,1,1000); % get 1000 evenly spaced values of E between 0 and 1
na = (.5.*(1-E)); % CHANGE THIS FOR THE EQUATION IN THE PROBLEM
% Calculate G-(ua/2)-(ub/2)
A = (1-(2.*na)); % define this bc it gets used a lot below
G = @(na) ((A.*G0)+((R*T).*((2.*na.*log(na))+(A.*log(A)))));
% plot it
figure();
plot(E, G(na))
title('Free Energy of System, G');
ylabel('G [cal/mol]'); xlabel('E');
% find minimum G value (equilbrium)
[na_min, Gmin] = fminbnd(G,na(end), na(2))
0 commentaires
Réponse acceptée
Bruno Luong
le 8 Sep 2020
Modifié(e) : Bruno Luong
le 8 Sep 2020
You should not mix between discrete sampling of your function G and continuous evaluation required by FMINBND
% Given :
R = 1.987; %[cal/mol*K]
T = 500; %[K]
G0 = -1000; %[cal/mol]
%% Find values to plot G vs. E
% Calulate na for a bunch of E:
% E: [0,1]
E = linspace(0,1,1000); % get 1000 evenly spaced values of E between 0 and 1
na = (.5.*(1-E)); % CHANGE THIS FOR THE EQUATION IN THE PROBLEM
% Calculate G-(ua/2)-(ub/2)
A = @(na) (1-(2.*na)); % define this bc it gets used a lot below
G = @(na) ((A(na).*G0)+((R*T).*((2.*na.*log(na))+(A(na).*log(A(na))))));
% find minimum G value (equilbrium)
[na_min, Gmin] = fminbnd(G,na(end), na(2))
Emin = 1 - na_min/0.5 % recover corresponding energy
figure();
plot(E, G(na),'b',Emin,Gmin,'+r')
title('Free Energy of System, G');
ylabel('G [cal/mol]'); xlabel('E');
Plus de réponses (1)
Alan Stevens
le 8 Sep 2020
You don't need fminbnd to find the minimum of G. Just
min(G(na))
will do it (or am I missing something?).
0 commentaires
Voir également
Catégories
En savoir plus sur Systems of Nonlinear Equations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!