Error using fminbnd function

Hello,
I want to compute the max of a function on an interval [a,b].
For that I am using the function fminbnd as follows:
syms x;
f=sqrt(3-2sqrt(x)); a=0; b=1;
dfdx2=-abs(diff(f,2));
dfdx4=-abs(diff(f,4));
M2=-fminbnd(dfdx2,a,b);
M4=-fminbnd(dfdx4,a,b);
The derivatives are calculated just fine but for M2, I get the following error message:
??? Error using ==> fcnchk at 103
If FUN is a MATLAB object, it must have an feval method.
Error in ==> fminbnd at 183
funfcn = fcnchk(funfcn,length(varargin));
Error in ==> programme_test at 5
M2=-fminbnd(dfdx2,a,b);
What am I doing wrong ?

 采纳的回答

Since you have created your objective function using symbolic variables, you need to convert them to function handles before you can use them to call FMINBND,
Try the following:
syms x;
f=sqrt(3-2*sqrt(x)); a=0; b=1;
dfdx2=-abs(diff(f,2));
dfdx4=-abs(diff(f,4));
F2 = matlabFunction(dfdx2)
F4 = matlabFunction(dfdx4)
M2=-fminbnd(F2,a,b);
M4=-fminbnd(F4,a,b);

2 个评论

Thank you Benji :)
function f = Pnl(r, a_0)
e = 1.6E-19 ; % Electron charge in Coulomb
m_e = 9.1E-31;% mass of electron in Kg
h = 6.626E-34;
hbar = h/(2*pi); % in eV.s
epsilon_0 = 8.854E-12; % in F/m
a_0 = (4*pi*epsilon_0*hbar^2)/(m_e*e^2); % Bohr radius
r_min = 0;
r_max = 100*a_0;
r_range = r_max-r_min;
r = linspace(r_min,r_max, 100);
x = r/a_0;
N = 1/(209952*sqrt(105*a_0.^3));
R = N*(x.^2).*(9072-1512*x+72*x.^2-x.^3).*exp(-x./6);
f = (4*pi*r.^2).*R.^2;
F = matlabFunction(Pnl);
end
Error :

请先登录,再进行评论。

更多回答(0 个)

类别

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by