Info

この質問は閉じられています。 編集または回答するには再度開いてください。

How to run a function

4 ビュー (過去 30 日間)
DIEGO FOSSO
DIEGO FOSSO 2015 年 6 月 28 日
閉鎖済み: Walter Roberson 2015 年 6 月 29 日
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 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2015 年 6 月 28 日

回答 (0 件)

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by