How to run a function?
206 次查看(过去 30 天)
显示 更早的评论
Hi , its the first time I'm using Matlab, I would like to know how can i run this function
function [ x,fx ,iter,x_vett]=gradiente(f,grad,x0,maxiter)
considering that f is a function of more variables, grad is a number (gradient of f), x0 is a vector and maxiter the maximum number if iterations. I use the command
[ x,fx ,iter,x_vett]=gradiente(f,grad,x0,maxiter)
with the f=@(x) 3*x(1)+2x(2) but it displays an error : "too many input arguments"
4 个评论
采纳的回答
更多回答(1 个)
Geoff Hayes
2015-6-28
Diego - when I run the above code, I observe the same error. This is because your anonymous function f is defined as a function that accepts a single input parameter
f=@(x) [2*x(1)+3*x(2)];
In gradiente, the following call is made against this function using feval
fx=feval(f,x(1),x(2));
and so we are passing two input parameters to a function that is expecting one (and so the error makes sense).
It is probably easier to just modify the function f to accept to input parameters as
f=@(x,y) [2*x+3*y];
and you should be able to proceed from there.
I did notice that the code in gradiente is assuming that the input grad is a function too
d=-feval(grad,x(1),x(2));
yet you are passing in the constant three. This will lead to the following error here (and in linesearch)
Error using feval
Argument must contain a string or function_handle.
Error in gradiente (line xxx)
d=-feval(grad,x(1),x(2));
How should grad be written as a function?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!