How to correctly optimize parameter using gradient descent

44 views (last 30 days)
ShooQ on 7 Jul 2022
Commented: Rena Berman on 19 Jul 2022 at 20:52
I have tried to implement the gradient descent method to optimize the parameter of a system but it not identifying the true parameter 'g'. I think my implememtation is not up to the mark. Here is my code
clc;
clear all;
close all;
%Parameters
r0 = 0.05;
L = 0.1;
d = 0.005;
w0 = 1.5;
E=[-0.5 0 0; 0 -0.5 0; 0 0 -1];
F=[0; 0; -1];
max_itr=900;
Ts=0.33;
t=[0:Ts:530];
x=[0 0 1]'; % system initiall
xhat=[0 0 1]'; % estimated system initial condition
ghat0= (0.04*(cos(0.01*t)+1.12)); %initial condition of estimated g
ghat=[];
g=zeros(length(t),1);
values_g=[];
for i=max_itr
for k=1:(length(t)) % Number of Iterations
ghat=[ghat ghat0(i)];
values_g(1)=0;
%real system
g = (2*r0*L*sinh((d*(k))/2))./(d*cosh((d*(k))/2)+L*sinh((d*(k))/2)); %real gamma
A = [ -0.5*g -w0 0 ;
w0 -g*0.5 0 ;
0 0 -g];
B =[0; 0; -g];
Q=integral(@(u) expm(A*((k+1)*Ts-u))*B,(k*Ts),((k+1)*Ts), 'ArrayValued', true);
x=expm(A*Ts)*x+Q; %System state
C=[0 0 1];
y=C*x; %output
values_g(k)=g;
%Estimated system to find value of g
%Cost function J=sum_k=0^K-1(y-yhat)^2
R1=integral(@(u)((k+1)*Ts-u)*expm(A*((k+1)*Ts-u))*E*B,(k*Ts),((k+1)*Ts), 'ArrayValued', true);
R2=integral(@(u)expm(A*((k+1)*Ts-u))*F,(k*Ts),((k+1)*Ts), 'ArrayValued', true);
dg=Ts*expm(A*Ts)*E*x+R1+R2; % gradient of gamma equation
Ahat = [ -0.5*ghat(k) -w0 0 ;
w0 -ghat(k)*0.5 0 ;
0 0 -ghat(k)];
Bhat =[0; 0; -ghat(k)];
Q1=integral(@(s) expm(Ahat*((k+1)*Ts-s))*Bhat,(k*Ts),((k+1)*Ts), 'ArrayValued', true);
xhat=expm(Ahat*Ts)*xhat+Q1; %estimated system state
yhat=C*xhat; %estimated output
ghat0=ghat0-0.01*(y-yhat)*C*dg; %g update
end
end
whos
plot(t,ghat,t,values_g,'r-','linewidth',1);
xlabel('time'), ylabel('g(t)')
Rena Berman on 19 Jul 2022 at 20:52

Walter Roberson on 7 Jul 2022
for k=1:(length(t)) % Number of Iterations
ghat=[ghat ghat0];
It looks like you might be intending ghat to be a record of every ghat0 value.
ghat=ghat-0.01*(y-yhat)*C*dg; %g update
but there you are calculating with all of ghat, not just with the most recent value.