I have a file called F.m which contains the following code:
1 % INPUT:
2 % 3D vector x
3 %
4 % OUTPUT:
5 % function value at x
6
7 function val = f(x)
8 val= x(1)^2 + x(1)*x(2)^3 - 12*x(3) + 4;
9 end
I am trying to call F.m in an algorithm in another file, but when I run the code, I get the following error:
"Error using F (line 8) Not enough input arguments."
I'm new to MATLAB, so I'm not sure what I'm doing wrong here. Any help would be greatly appreciated!

댓글 수: 2

Aaron Powers
Aaron Powers 2015년 4월 26일
Could you include the code that calls your function f, with an example of what x is?
Thank you for your interest in my question, Aaron!
x is a 3D point/vector, for example x = (x(1), x(2), x(3)) = (0,1,2).
Here's the code (it also calls on 2 other algorithms and requires input of other values too):
function [xminest, fminest,k] = Algorithm1(f, gradf, x0, tol1, tol2, T)
k = 0;
xk = x0;
while ( norm(feval(gradf, xk)) >= tol1 )
%du = feval(gradf, xk);
%dk = -du/norm(du);
dk = -1*feval(gradf, xk);
[a, b] = Algorithm2(f, xk, dk, T);
[tmin, fmin] = Algorithm3(f, a, b, tol2, xk, dk);
k = k + 1;
xk = xk + tmin*dk;
end
fminest = feval(f,xminest);
To run the code, I enter the command:
Algorithm1(F, gradf, [0,1,2], 0.1, 0.1, 10)

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

 채택된 답변

the cyclist
the cyclist 2015년 4월 26일

1 개 추천

With the syntax
Algorithm1(F, gradf, [0,1,2], 0.1, 0.1, 10)
MATLAB is looking for a variable named F (and similarly with gradf). You need to tell it that F is a function, by passing the function handle:
Algorithm1(@F, @gradf, [0,1,2], 0.1, 0.1, 10)
It looks like you have similar problems deeper in your code, but I could not resolve them completely because I don't have all the functions you are using.

댓글 수: 1

Anonymous Chupacabra
Anonymous Chupacabra 2015년 4월 27일
Thank you very much, cyclist! After prefacing each file name with @ as you said, the code is executing as intended!

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

추가 답변 (1개)

Jan
Jan 2015년 4월 26일
편집: Jan 2015년 4월 26일

1 개 추천

Inside this line:
[a, b] = Algorithm2(f, xk, dk, T);
you call the function "f" without arguments. But You want top provide the function handle of this file instead:
[a, b] = Algorithm2(@f, xk, dk, T);
Note: In modern Matlab versions you can omit the feval and call the function handle directly, which looks nicer:
while norm(gradf(xk)) >= tol1

댓글 수: 1

Anonymous Chupacabra
Anonymous Chupacabra 2015년 4월 27일
Thank you very much, Jan! Using the function handle as you showed worked! Thank you also for your tip, as I have the 2015 MATLAB version, but I think the algorithm code I am using may be quite old.

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

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by