Finding Minimum value of an anonymous function using fminbnd
Ältere Kommentare anzeigen
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))
Akzeptierte Antwort
Weitere Antworten (1)
Alan Stevens
am 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?).
Kategorien
Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!