How to correctly optimize parameter using gradient descent

조회 수: 14 (최근 30일)
ShooQ
ShooQ 2022년 7월 7일
댓글: Rena Berman 2022년 7월 19일
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
Rik
Rik 2022년 7월 8일
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)')
Rena Berman
Rena Berman 2022년 7월 19일
(Answers Dev) Restored edit

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2022년 7월 7일
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
ShooQ
ShooQ 2022년 7월 7일
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 2022년 7월 7일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Surrogate Optimization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by