Input Argument Undefined for Passing a Parameter to a Function

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,

 采纳的回答

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 个评论

Hi Image Analyst,
I completely understand your response - I made a mistake when copying over the command line codes above. I have vec as the vector of ones and get the issue I initially mentioned.
Thanks again.
@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.
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

请先登录,再进行评论。

更多回答(1 个)

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

类别

帮助中心File Exchange 中查找有关 Language Fundamentals 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by