f=@(x) 0.5*(x(1)-1).^2+10*(x(2)+2).^2-2
miter=10000
and i have loop
for i=1:miter
then,who i can to calculate f(i)-f(i-1)?

댓글 수: 6

Mario Malic
Mario Malic 2020년 9월 19일
saja mk
saja mk 2020년 9월 19일
i need to clculate
this ,
Walter Roberson
Walter Roberson 2020년 9월 19일
That f needs a vector of length 2 for its parameter, not a scalar i.
saja mk
saja mk 2020년 9월 19일
i al ready get the thev optimal by Gradient Descent Algorith
now ,i just need to change the tol as the equation in the previous comment
saja mk
saja mk 2020년 9월 19일
@Walter Roberson
so how can i implement it ?
Walter Roberson
Walter Roberson 2020년 9월 19일
You cannot. Your gradient descent formula is only valid for functions of one variable.

댓글을 달려면 로그인하십시오.

답변 (1개)

saja mk
saja mk 2020년 9월 19일

0 개 추천

but i get the optimal in GDA
x0=[2 2]';
% tol=1e-6;
miter=10000;
% dxmin=1e-6;
alpha=0.01;
% gnorm=inf;
x=x0;
niter=1;
% dx=inf;
for i=1:miter
g=[(x(1)-1); 20*(x(2)+2)];
f=@(x) 0.5*(x(1)-1).^2+10*(x(2)+2).^2-2 ;
xnew = x - alpha*g;
fnew=f(xnew);
if abs(xnew-x)<=0.00001
break
end
% niter=niter+1;
x=xnew;
end
xopt=x;
niter=niter+1;
after run :
xopt =
1.0010
-2.0000

댓글 수: 1

Walter Roberson
Walter Roberson 2020년 9월 19일
this does not use the epsilon formula from https://www.mathworks.com/matlabcentral/answers/596404-value-in-each-iteration#comment_1015369

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2020년 9월 19일

댓글:

2020년 9월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by