How to correctly optimize parameter using gradient descent

13 views (last 30 days)
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
%Gradient of g=dJ/dy.dy/dx.dx/dg
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)')
  2 Comments
Rik
Rik on 8 Jul 2022
The code you removed was still available in the Google cache, but the image wasn't. @ShooQ, please re-upload the image yourself.
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];
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 k=1:(length(t)) % Number of Iterations
ghat=[ghat ghat0];
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
%Gradient of g=dJ/dy.dy/dx.dx/dg
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
ghat=ghat-0.01*(y-yhat)*C*dg; %g update
end
plot(t,ghat,t,values_g,'r-','linewidth',1);
xlabel('time'), ylabel('g(t)')

Sign in to comment.

Answers (1)

Walter Roberson
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.
  2 Comments
ShooQ
ShooQ on 7 Jul 2022
If you see the attached equations of estimated system, I think it g is not upating intervatively, may be i need to used to nested loop to update g at each iterations.
Walter Roberson
Walter Roberson on 7 Jul 2022
g = (2*r0*L*sinh((d*(k))/2))./(d*cosh((d*(k))/2)+L*sinh((d*(k))/2)); %real gamma
that statement is inside the for loop, so g is being updated each iteration.

Sign in to comment.

Categories

Find more on System Composer in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!