Problem using fminbnd function

7 visualizaciones (últimos 30 días)
Milena Peixoto Rodrigues
Milena Peixoto Rodrigues el 23 de Sept. de 2021
Respondida: Alan Weiss el 24 de Sept. de 2021
The question gives the equation , and asks to generate a user-defined function for n=2 and 6; with n being the input vector and r being the output vector. The r value ranges from 1 to 5. It also asks to graph the U(r) curve for each n on the same plot and mark the points of the minimum.
I wrote this code:
function r_min=Problem_4_15(n,r1,r2)
close all
U=@(x)4*((1/x).^(2.*n) - (1/x).^(n));
options=optimset('Display','iter');
[r_min,U_min]=fminbnd(U,r1,r2,options); % Error Occurrence Line
rg=linspace(r1,r2);
Ug=U(rg);
plot(rg,Ug,r_min,U_min,'o')
grid on
xlabel('Distance Intermolecular r*')
ylabel('Potential Energy U')
title('Lennard-Jones-type dimensionless')
legend('function U','minimum r')
Matlab reports the following error:
Problem_4_15([2 6], 1, 5)
Error using fminbnd (line 238)
User supplied objective function must return a scalar value.
Error in Problem_4_15 (line 5)
[r_min,U_min]=fminbnd(U,r1,r2,options);
Part of the fminbnd function code, where line 238 appears:
% Check that the objective value is a scalar
if numel(fx) ~= 1
error('MATLAB:fminbnd:NonScalarObj',... % Line 238
getString(message('MATLAB:optimfun:fminbnd:NonScalarObj')));
end
How can I solve this problem to make the code work?
Thanks in advance.

Respuestas (1)

Alan Weiss
Alan Weiss el 24 de Sept. de 2021
All optimization solvers require that the objective function return a scalar value. You need to write your objective function in a way that it returns a scalar, not a vector or an array.
If you are trying to solve for a variety of parameter values, well, you will have to write a loop and solve for one parameter value at a time.
Alan Weiss
MATLAB mathematical toolbox documentation

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by