i just want to make a polynomial regression with gradient descent ,the funtion i make regression is sin(x),but i am surprised ,if i make regression other function ,just like ax+b ax^2+bx+c,it is succeed,but for sinx,however i change the alpha,it can't succeed,what is problem about my code?
%set the data
N=20X=(0.1:0.01:2*pi)Y=sin(X)r=randi(size(X),[N,1])X_T=X(r)Y_T=Y(r)Y_T=Y_T+randn(1,N)/10Y_T=Y_T'hold on;scatter(X_T,Y_T,'Red'); plot(X,Y)% GRADIENT DESCENT
W_T = randn(1,5); % initialize W to all zeros
N_W=W_TV =[ones(N,1) X_T' (X_T.^2)' (X_T.^3)' (X_T.^4)']alpha = 0.00000000005; % gradient descent step size
W_C=inv(V'*V)*V'*Y_T;% for each iteration
while 1for j = 1:5 % looping for each parameter W(i)
% calculate new value of W(i)
N_W(j) = W_T(j) -alpha *sum(((V(:,j)*W_T(j)') - Y_T).* V(:,j));end if(norm(W_T-N_W)<0.001) break end W_T=N_W;endT_X=(0.1:0.1:2*pi)T_Y=N_W(1)+T_X.*N_W(2)+(T_X.^2).*N_W(3)+(T_X.^3).*N_W(4)+(T_X.^4).*N_W(5)plot(T_X,T_Y, 'Color','Green');% output actual weights from formula in Red W = [5.775 0.474]'
% finish off
hold off;
[EDITED, Jan, code formatted]
Best Answer