# Find minimum error function using gradient descent

조회 수: 13 (최근 30일)
Elysi Cochin 2023년 1월 17일
댓글: Elysi Cochin 2023년 1월 18일
Find the minimum error function using gradient descent
for the function (x-1).^2 - 2
I found a code in matlabcentral, but I'm not sure how to edit it according to my requirement.
%% Code I got from matlabcentral
X = -2:0.1:2;
Y = -2:0.1:2;
[X,Y] = meshgrid(X,Y);
% Z = 2*X.^2+3*Y.^2;
Z = (x-1).^2 - 2;
surf(X,Y,Z)
hold on
x(1) = 2; % initial value of x
y(1) = 2; % initial value of y
z(1) = 2.*x(1).^2 + 3.*y(1).^2;
stepsize = 0.1;
for i = 1:30
zx = 4*x(i);
zy = 6*y(i);
x(i+1) = x(i) - stepsize*zx; %gradient descent
y(i+1) = y(i) - stepsize*zy;
z(i+1) = 2.*x(i+1).^2 + 3.*y(i+1).^2
end

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

### 채택된 답변

Dyuman Joshi 2023년 1월 17일
편집: Dyuman Joshi 2023년 1월 18일
I don't know about the code you found, the method of operation for the given optimization algorithm should be like this -
funcChoice=2;
%error function
switch funcChoice
case 1
fun = @(x) (x-1).^2 - 2;
minval = -2;
case 2
fun = @(x) exp(x) - 2*x;
minval = 2*(1-log(2));
case 3
fun = @(x) -log(x) + 2*x;
minval = 1+log(2);
end
disp(fun)
@(x)exp(x)-2*x
@(x)exp(x)-2
disp(minval)
0.6137
%starting point, can be changed as per wish
x=4;
%learning rate
rate = 0.01;
%counter
itr=0;
tol=1e-3;
%main algorithm
while abs(fun(x(end))-minval)>=tol
itr=itr+1;
end
%number of iterations
itr
itr = 169
finalx=x(itr+1)
finalx = 0.7242
%minimum function value
finalval=fun(x(itr+1))
finalval = 0.6147
%minimum error
minError=abs(minval-finalval)
minError = 9.7524e-04
##### 댓글 수: 8이전 댓글 6개 표시이전 댓글 6개 숨기기
Dyuman Joshi 2023년 1월 18일
편집: Dyuman Joshi 2023년 1월 18일
That doesn't make any sense to me or doesn't strike any logic
Was a justification given for it? If not, check notes or ask the instructor what does it mean by a good point.
Elysi Cochin 2023년 1월 18일
Ok Sir. Thank you so much for your help. Thanks a lot.

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

### 카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by