I have a question. yes it's a homework question. I already gave it in so please no "we won't do your homework for you" replies

this is the problem: a mass (1kg) is attached to a rod (0.5 m) the rod is attached at the base and can rotate in one direction

the control variable "u" is the torque applied at the base of the rod in the clockwise direction

the variable "x" is the angle the rod makes with the vertical axis, in the clockwise direction.

in the beginning the rod is at 90 degrees towards the right, in other words the angle "x" is pi/2 and to bring the rod up we apply negative torque "u"

the equation I have is d/dt (dx/dt) = ( g/L ) sin (x) + u/(m***L***L) where d/dt (dx/dt) is the second derivative of x (angular acceleration) , g is gravitational acceleration, m is mass of object and L is length of rod

we "linearize" this by assuming sin(x) ~ x

so d/dt (dx/dt) = C1*** x + C2*** u

I chose state variables X1 = x and X2 = dx/dt so X1 prime = X2 X2 prime= C1*** X1 + C2*** u

is everything correct so far?

I have to minimize a performance criterion J = 0.5*** integral from 0 to final time of ( (a***x)^2 + u^2) where the final time is specified (1s or 0.5 s) X1(0) = pi/2 final position of X1 is not specified (free) constant "a" in the performance index is 4 I assumed that we start from a 0 velocity (X2(0)=0) and final velocity is free

applying the optimal contol theory, using lagrange multipliers lambda1 and lambda2, my hamiltonian is H=8*X1^2 + 0.5*u^2 + lambda1***X2 + lambda2***C1***X1+lambda2***C2*u

from this I get that u = -C2*lambda2 and the following differential equations

X1 prime = X2 X2 prime = C1*X1-C2^2 *** lambda2 lambda1 prime = -16 ***X1 - C1*lambda2 lambda2 prime = -lambda1

with X1(0)=pi/2, X2(0)=0 (my assumption, is it correct?) and lambda1(tf)=lambda2(tf)=0

I used Riccati's equation to find my lambdas at 0

in sumulations with matlab I see that within 1 second the position of the pendulum reaches the vertical however the velocity does not reach 0 and the pendulum swings to the other side and the system is unstable. it oscillates wildly.

I can see from my differential equations that a negative lambda2 will make lambda 1 grow more positive which makes lambda 2 grow more negative. this is clearly unstable

however I have redome my calculations so many times and I don't see the problem. what am I missing?