How to run a function
Infos
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Afficher commentaires plus anciens
Hi , its the first time I'm using Matlab, I would like to know how can I run this function (that's the implementation of the conjugate gradient
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 is the code of the function "linesearch"
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
These are my inputs and errors in the Command window. I'm sure the function is correctly implemented, maybe the problem is in the Command window.
>> 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));
1 commentaire
Azzi Abdelmalek
le 28 Juin 2015
Réponses (0)
Cette question est clôturée.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!