a problem with implementing Gradient descent

2 次查看(过去 30 天)
Hi
I wrote the following programm implementation of gradient descent. But when I run the programm it works really slow . I guess it is because of the symbolic differentiation or maybe there is another problem in the implementaion . Can, you tell me what is wrong ?
function y = grad(x0,y0,z0,f,m1,m2)
syms a;
symsb;
symc c;
n=0;
if (length(symvar(diff(f,a))==3)&& length(symvar(diff(f,b))==3) && length(symvar(diff(f,c))==3))
f_a = matlabFunction(diff(f,a),[a,b,c]);
f_b=matlabFunction(diff(f,b),[a,b,c]);
f_c=matlabFunction(diff(f,c),[a,b,c]);
else
y= 0;
return;
end
delta = -1*[f_a(x0,y0,z0), f_b(x0,y0,z0), f_c(x0,y0,z0)];
phi = @(t) ( f(x0+t*delta(1), y0+t*delta(2), z0+t*delta(3)) );
min = GRSMethod( phi,m1,m2 );
while ( abs ( min * ( delta(1)^2+delta(2)^2+delta(3)^2 ))> 10^ (-5) || n<100)
x0=x0+min*delta(1);
y0= y0+min*delta(2);
z0=z0+min*delta(3);
delta = -1*[f_a(x0,y0,z0), f_b(x0,y0,z0), f_c(x0,y0,z0)];
min = GRSMethod( phi,m1,m2 );
n=n+1;
end
y = [ x0,y0,z0];
end
Sincerely, Maria
  1 个评论
Walter Roberson
Walter Roberson 2015-11-22
Do not use "min" as the name of a variable: it interferes with using "min" as the name of the MATLAB and MuPAD routine.

请先登录,再进行评论。

回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by