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 Kommentare

Jan
Jan am 28 Jun. 2015
Please post a complete copy of the error message and the line, which causes the error.
Geoff Hayes
Geoff Hayes am 28 Jun. 2015
Diego - which line of code is generating the error? The too many input arguments message is telling you that you are supplying too many input parameters to one of your functions. There probably isn't an error with gradiente but we need to know how the input function handle f is being used within that function. Perhaps it is being called with a second input parameter (i.e. two input parameters are being passed in whereas your code is assuming a single 2 element vector input is being used).
Note also the typo with your f. You are missing a multiply (?) between the 2 and x
f=@(x) 3*x(1)+2*x(2)
DIEGO FOSSO
DIEGO FOSSO am 28 Jun. 2015
This is the code of the function:
function [ x,fx ,iter,x_vett]=gradiente(f,grad,x0,toll,maxiter)
x=x0;
fx=feval(f,x(1),x(2));
d=-feval(grad,x(1),x(2));
err=norm(d);
iter=0;
x_vett=[x];
while (err>toll) & (iter <=maxiter)
alpha=linesearch(f,grad,x,d);
x=x+alpha*d;
x_vett=[x_vett,x];
d=-feval(grad,x(1),x(2));
err=norm(d);
iter=iter+1;
end
fx=feval(f,x(1),x(2));
if (err>toll) & iter>maxiter
error('Hai superato il numero max di iterazioni!!!')
end
This are my inputs and the errors
>> f=@(x) [2*x(1)+3*x(2)];
>> x0=[1,2];
>> grad=3;
>> maxiter=10;
>> toll=0.5;
>> [ x,fx ,iter,x_vett]=gradiente(f,grad,x0,maxiter)
Error using @(x)[2*x(1)+3*x(2)]
Too many input arguments.
Error in gradiente (line 8)
fx=feval(f,x(1),x(2));
DIEGO FOSSO
DIEGO FOSSO am 28 Jun. 2015
This is the funtion "linesearch" (if you want to try to run it)
function alpha=linesearch(f,grad,x,d)
gamma=0.1;
sigma=1/4;
alpha=1;
alphamin= 10^(-3);
xnew=x+alpha*d;
while feval(f,xnew(1),xnew(2))>feval(f,x(1),x(2))+... % I cond. di Wolfe
gamma*alpha*feval(grad,x(1),x(2))'*d & alpha>alphamin
alpha=sigma*alpha;
xnew=x+alpha*d;
end

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Thorsten
Thorsten am 28 Jun. 2015
Bearbeitet: Thorsten am 29 Jun. 2015

0 Stimmen

You have defined your function f with a single argument x but you call it with two arguments x(1) and x(2) in using fx=feval(f,x(1),x(2)); instead, use
fx=feval(f,x);

2 Kommentare

DIEGO FOSSO
DIEGO FOSSO am 29 Jun. 2015
How can I define a function of two variables? I can't change fx = feval (f, x(1), x(2)) because it's not a code written by me
Geoff Hayes
Geoff Hayes am 29 Jun. 2015
Try
f=@(x,y) [2*x+3*y];

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Geoff Hayes
Geoff Hayes am 28 Jun. 2015

0 Stimmen

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?

Kategorien

Mehr zu Data Distribution Plots finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by