How can I run this function?
4 views (last 30 days)
Show older comments
Hi, I can't run this function called gradiente. f should be a function of two variables, grad should be the gradient of that function, x0 is a vector with number of elements like the number of variables; toll and maxiter should be numbers. I have difficult to run it
function [ x,fx ,iter,x_vett]=gradiente(f,grad,x0,toll,maxiter)
% x = ottimo
% fx = valore ottimo
% iter = numero di iterazioni
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 for 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
Can anyone try to run the function "gradiente" and tell me the inputs in the command window?
0 Comments
Answers (1)
Azzi Abdelmalek
on 10 Jul 2015
Assign values to f,grad,x0,toll,maxiter
f=
grad=
x0=
toll=
maxiter=
[ x,fx ,iter,x_vett]=gradiente(f,grad,x0,toll,maxiter)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!