#include<iostream>
#include<cmath>
#include<ctime>
using namespace std;
double f(double x);
double g(double x);
double gd(double xs,double s)
{
double x=xs;
double y;
for(int i=0;i!=20;++i)
{
double grad= -1g(x);
x+=grads;
y=f(x);
cout <<"EPOCH "<<i<<" grad = " <<setprecision(15)<< grad<< " x = "<< x <<" y = " << y<< endl;
if(abs(grad) < 1e-6)
break;
}
return x;
}
int main()
{
double xk=-5,ak=01;
gd(xk,ak);
}
double f(double x)
{
return xx-2x+1;
}
double g(double x)
{
return 2x-2;
}
function f=conjugate_grad_2d(x0,t)
%please input this:conjugate_grad_2d([2,2],005)
x=x0;
syms xi yi a
f=xi^2-xiyi+3yi^2;
fx=diff(f,xi);
fy=diff(f,yi);
fx=subs(fx,{xi,yi},x0);
fy=subs(fy,{xi,yi},x0);
fi=[fx,fy];
count=0;
while double(sqrt(fx^2+fy^2))>t
s=-fi;
if count<=0
s=-fi;
else
s=s1;
end
x=x+as;
f=subs(f,{xi,yi},x);
f1=diff(f);
f1=solve(f1);
if f1~=0
ai=double(f1);
else
break
x,f=subs(f,{xi,yi},x),count
end
x=subs(x,a,ai);
f=xi^2-xiyi+3yi^2;
fxi=diff(f,xi);
fyi=diff(f,yi);
fxi=subs(fxi,{xi,yi},x);
fyi=subs(fyi,{xi,yi},x);
fii=[fxi,fyi];
d=(fxi^2+fyi^2)/(fx^2+fy^2);
s1=-fii+ds;
count=count+1;
fx=fxi;
fy=fyi;
end
x,f=subs(f,{xi,yi},x),count
以上就是关于怎么用C++编写梯度下降法全部的内容,包括:怎么用C++编写梯度下降法、共轭梯度法的MATLAB程序、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)