필터 지우기
필터 지우기

not enough input arguments error

조회 수: 1 (최근 30일)
Erfan Hamdi
Erfan Hamdi 2016년 10월 6일
댓글: Erfan Hamdi 2016년 10월 6일
Hi everybody ! I have this code as my main function :
function [x,fx,ea,iter]=goldmin(f,xl,xu,es,maxit,varargin)
% goldmin: minimization golden section search
% [x,fx,ea,iter]=goldmin(f,xl,xu,es,maxit,p1,p2,...):
% uses golden section search to find the minimum of f
% input:
% f = name of function
% xl, xu = lower and upper guesses
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by f
% output:
% x = location of minimum
% fx = minimum function value
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'),end
if nargin<4||isempty(es), es=0.0001;end
if nargin<5||isempty(maxit), maxit=50;end
phi=(1+sqrt(5))/2;
iter=0;
fprintf('xl f(xl) xu f(xu)\n')
fprintf('%.3f %9.3f %9.3f %9.3f\n',xl,f(xl,varargin{:}),xu,f(xu,varargin{:}) )
while(1)
d = (phi-1)*(xu - xl);
x1 = xl + d;
x2 = xu - d;
if f(x1,varargin{:}) < f(x2,varargin{:})
xopt = x1;
xl = x2;
else
xopt = x2;
xu = x1;
end
iter=iter+1;
fprintf('%.3f %9.3f %9.3f %9.3f\n',xl,f(xl,varargin{:}),xu,f(xu,varargin{:}) )
if xopt~=0, ea = (2 - phi) * abs((xu - xl) / xopt) * 100;end
if ea <= es || iter >= maxit,break,end
end
x=xopt;fx=f(xopt,varargin{:});
and this is the function which i want to find the minimum point :
function y=p3function(x)
y=(1.5.*x.^6-2.*x.^4+12.*x);
each time i call the function i encounter :
>> gm(p3function,-3,3)
Error using p3function (line 2)
Not enough input arguments.
what should i do ?
  댓글 수: 2
Guillaume
Guillaume 2016년 10월 6일
편집: Guillaume 2016년 10월 6일
Can you show the complete error message? It should also tell you on which line of gm (same as goldmin ?) p3function was called before it failed.
Erfan Hamdi
Erfan Hamdi 2016년 10월 6일
yes gm is the goldmin
>> gm(p3function,-3,3)
Error using p3function (line 2)
Not enough input arguments.

댓글을 달려면 로그인하십시오.

채택된 답변

dbmn
dbmn 2016년 10월 6일
Two suggestions:
- you used gm instead of goldmin. Are you shure it is wanted that way?
gm(p3function,-3,3)
- Instead of passing a string with the functionname you could try passing a function handle
goldmin(@p3function,-3,3)
This last command worked on my machine
  댓글 수: 1
Erfan Hamdi
Erfan Hamdi 2016년 10월 6일
Thanks ! that function handle helped !!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2016년 10월 6일
When you write this:
gm(p3function,-3,3)
MATLAB will call p3function with zero input arguments and use the value it returns as the first input to gm. But your p3function requires one input argument, so you receive an error at the first part of that process. Since you want gm to be able to call the function you specify as the first input argument, pass that first argument as a function handle.
gm(@p3function, -3, 3)

카테고리

Help CenterFile Exchange에서 Elementary Math에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by