How do I solve a linear state observer using only Matlab ODE45 and NO toolboxes nor Simulink?
10 views (last 30 days)
Show older comments
I have attempted to simulate the observed states of a linear state observer.
Refer to the attached code below: I ultimately need to calculate Xhat, and by doing so I call the second ODE45. The anonymous function that ODE45 handles must make use of X in it's calculation (which is determined by the first call of ODE45). After calling the first ODE45 that calculates X, it returns a 3x50 matrix; 3 states over 50 time instances. But only that X at a specific time t, corresponding to the calculation of Xhat at that SAME time t, must be used.
i.e. find X at time t and then use that value to find Xhat at the same time t, and then do all that over the whole timespan [0 1].
I've tried using interp1 but could not get it working, perhaps my syntax was wrong.
CODE: --------
clear
clc
%%%%System matrices %%%
A = [ 1 2 0
3 4 1
1 2 0];
B = [1000 ; -1000 ; 0.1];
C = [ 0 1 1 ];
D = 0;
u = 1;
%%%Arbitrary observer matrices %%%
Ahat = A; % arbitrary values used just to test the ODE solver later on
Bhat = B; % same explanation as above
Ke = [1;1;1]; % same as above
Abar = Ahat - Ke*C;
%%%plotting the actual states
x1_0 = 1;
x2_0 = 1;
x3_0 = 1;
X_0 = [x1_0;
x2_0
x3_0];
x1timespan = linspace(0,1,25); % Generate t for X
dxdt = @(t,x) [A(1,1)*x(1) + A(1,2)*x(2) + A(1,3)*x(3) + B(1,1)*1;
A(2,1)*x(1) + A(2,2)*x(2) + A(2,3)*x(3) + B(2,1)*1;
A(3,1)*x(1) + A(3,2)*x(2) + A(3,3)*x(3) + B(3,1)*1]
[tx,X] = ode45(dxdt, [0 1], [x1_0 x2_0 x3_0]); % Generates the solution to the linear system
plot(tx,X)
title('Actual States')
%%Plotting the observed states
dxhatdt = @(t,Xhat) [Ahat*Xhat + Bhat*u + Ke*((C*X) - (C*Xhat))]
Xhat_0 = [0;0;0];
[txhat,Xhat] = ode45(dxhatdt, [0 1], Xhat_0)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0 Comments
Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations 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!