Input Argument Undefined for Passing a Parameter to a Function

12 views (last 30 days)
Hello,
I apologize if I am asking a very simple question but it's been something that I have been trying to work out and so far have been unsuccesful.
I am trying to write code to employ the gradient descent method on the camel function. Displayed below is the code:
Gradient Descent:
function [ dx ] = grad_d(v,f,gamma )
[Fx, Gx, Hx]= f(v);
dx = A - gamma*Gx;
end
In this case I want to use the function 'camel':
function [ Fx, Gx, Hx ] = camel(vec)
V = num2cell(vec);
[x,y] = V{:};
Fx = 2*x^2 - 1.05*x^4 + (x^6/6) + x*y+y^2;
Gx = [ x^5 - (21/5)*x^3 + 4*x + y;
x+2*y];
Hx = [ 5*x^4 - (63/5)*x^2 + 4, 1;
1, 2];
end
When I try to run one evaluation with the above code I get the error below:
I run the code as follows:
vec = ones(2,1)
grad_d(vec,camel,.1)
and receive the following:
_Input argument "vec" is undefined._
I know that I am overlooking something small but I can't pin it down. Any help would be much appreciated.
Thank you,

Accepted Answer

Image Analyst
Image Analyst on 16 Feb 2015
Well you wrote the function to take in vec. What values do you want to pass in for it? You have to assign them. It can't read your mind.
  3 Comments
James Tursa
James Tursa on 16 Feb 2015
Edited: James Tursa on 16 Feb 2015
@Peter: Do you still have this line:
grad_d(vec,camel,.1)
The 2nd argument is camel. That will call the function camel without any input arguments to camel. Then when it gets inside the camel function and tries to use the variable vec, you get the error.
Image Analyst
Image Analyst on 16 Feb 2015
Don't pass in camel (If you did you'd have to use a @ in front of it).
vec = ones(2,1)
grad_d(vec,.1) % Call it without camel
Just call camel() inside the function:
function dx = grad_d(v,gamma)
[Fx, Gx, Hx]= camel(v);
dx = A - gamma*Gx;
end

Sign in to comment.

More Answers (1)

Peter
Peter on 16 Feb 2015
Hello,
I am still having this problem. I made a more simple function:
function [ fin] = testfunction( vec )
V = num2cell(vec);
[x,y] = V{:};
fin = x+y;
end
If I try to call it using feval I get the same error. Any other thoughts on this? I am sure that it is a very trivial problem but I don't see what it is.
Thanks again

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!