How to change the reference ?
5 views (last 30 days)
Show older comments
we have this system by its state representation :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275511/image.png)
the formulation of the quardatic command is described by the following equations:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275512/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275513/image.png)
exemple :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275514/image.png)
we would like to apply the quardatic command so that the output y(k) = x1(k) follows a certain reference.
For reference = 5 , this is the code :
clear, clc
Ac=[0 1;0 0]; Bc=[0;1]; Cc=[1 0]; Dc=0;
[A,B,C,D]=c2dm(Ac,Bc,Cc,Dc,0.1) %Converting to discrete mode
N=600;
ref=5; %The reference
Q=[1 2;2 1] ; R=0.1;
P=Q;
x_ref=[ref;0];
for k=N:-1:0
F=R+B'*P*B; %Equation 1
K=inv(F)*B'*P*A; %Equation 2
M=P-P*B*inv(F)*B'*P; %Equation 3
P=A'*M*A+Q; %Equation 4
end
u=0 ; x=[0;0];
for i=1:N
x=A*x+B*u;
u=-K*(x-x_ref); %Equation 5
y(i)=C*x;
end
plot(1:N,ref*ones(1,N),'r',1:N,y)
I get the following figure:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275426/image.png)
And now i would like to use the same code by modifying the reference signal, so as to obtain nearly these 2 curves:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275427/image.png)
y= red curve , reference= black curve
The goal is that the signal y (i) follows a non-fixed reference:
ref = 8 in [0.200]
ref = 5 in [200,400]
ref = 12 in [400,600]
This is the reference to follow :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275515/image.png)
if anyone can help me, i will be very grateful
2 Comments
Image Analyst
on 5 Mar 2020
I'm not tackling that until you do two things: (1) add comments, and (2) use descriptive variable names. To me it just looks like an impenetrable alphabet soup of a program. And I'm not sure why you can't just make the changes since (evidently) you understand the original code far better than us. We're willing to help but you have to make it easy for us to help you.
Accepted Answer
Raj
on 6 Mar 2020
Edited: Raj
on 6 Mar 2020
I don't think this is right:
ref = 8 in [0.200]
ref = 5 in [200,400]
ref = 12 in [400,600]
You cannot have 2 references at same time instant. So I am assuming you meant
ref = 8 in [0.200]
ref = 5 in [201,400]
ref = 12 in [401,600]
Now coming to the solution, the easiest way is to simply shift the reference inside 'for' loop like this:
clear, clc
Ac=[0 1;0 0]; Bc=[0;1]; Cc=[1 0]; Dc=0;
[A,B,C,D]=c2dm(Ac,Bc,Cc,Dc,0.1) %Converting to discrete mode
N=600;
Q=[1 2;2 1] ; R=0.1;
P=Q;
for k=N:-1:0
F=R+B'*P*B; %Equation 1
K=inv(F)*B'*P*A; %Equation 2
M=P-P*B*inv(F)*B'*P; %Equation 3
P=A'*M*A+Q; %Equation 4
end
u=0 ; x=[0;0];
for i=1:N
if i<=200
ref=8; %The reference
elseif i>200 && i<=400
ref=5;
else
ref=12;
end
x_ref=[ref;0];
x=A*x+B*u;
u=-K*(x-x_ref);
y(i)=C*x;
end
plot([0 200 201 400 401 600],[8 8 5 5 13 13],'r',1:N,y)
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!