You are on page 1of 2

Problem 5)

MATLAB CODE:
function linreg
syms K11 K12 K22;
syms x1 x2 u;
syms s11 s22;
x=[x1;x2];
K=[K11 K12;K12 K22];
A=[0 1;-0.5 -1.5];
B=[0;1];
Q=[2 0;0 0];
R=[0.005];
r=[1;0];
s=[s11;s22];
kprime=-K*A-A'*K-Q+K*B*(R^-1)*B'*K;
sprime=-[A'-K*B*(R^-1)*B']*s+Q*r;
u=-(R^-1)*B'*K*x-(R^-1)*B'*s;
tperiod=[15,0];
z0=[2;0;0];
[c,y]= ode45 (@j, tperiod, z0);
plot (c,y(: ,1),'r',c,y(: ,2),'g',c,y(: ,3),'b');
grid on;
r0 =[-2 ; 0] ;
[t, a] = ode45 ( @(t,a) p(t , a , c , y) , [15,0] , r0 ) ;
plot ( t , a( : , 1 ) , 'r' , t , a( : , 2 ) , 'g' );
grid on;
x0=[0;0];
[q,x]= ode45(@(q,x) f(q, x, c, y, t, a), [0,15], x0);
plot(q,x(: ,1),'r',q,x(: ,2),'g');
grid on;
u = [-200*[0.1*x(:,1)+0.03*x(:,2)-0.1]];
plot(u,'b');
grid on ;
title('Optimum States and Optimal Control');
xlabel('time');

PLOT:

You might also like