Solving Finite Difference Equation using Matlab

194 Views Asked by At

From the original Equation of Motion of an Inverted Pendulum:

$$ 0 = \ddot\theta - \frac{g}{l} \sin\theta + \frac{1}{2ml}\cdot C_D\rho A (\dot\theta\cdot l)^2\cdot\operatorname{sign} (\dot\theta) + \frac{k}{ml^2}\theta $$

I got the Non-Linear Equation by subbing in known Variables:

$$0 = \ddot\theta - 24.525 \sin\theta + 0.01112 (\dot\theta^2)\cdot\operatorname{sign} (\dot\theta) + 245.25\theta$$

VARIABLES

  • Mass - m = 0.5 (kg)
  • Initial Angle with the Vertical Axis - $\theta_o = 10$ (Degrees)
  • Initial Angular Velocity - $\dot\theta_{o} = 0.01 $ (rad/s)
  • Length - l = 0.4 (m)
  • Cross Sectional Area of Mass - A = 0.05 ($m^2$)
  • Spiral Spring Constant - k = 19.62 (Nm/rad)
  • Air Density - $\rho = 1.183 (kg/m^3)$
  • Drag Coefficient - $C_D = 0.47$

From here I derived the Finite Difference Equation of

$$0 = \frac{(\theta_{n+1}-2\theta_n+\theta_{n-1})}{\Delta t^2}-24.525\sin(\theta_n)+0.01112\frac{\theta_{n+1}-\theta_{n-1}}{2\Delta t}\cdot\operatorname{sign}\frac{\theta_{n+1}-\theta_{n-1}}{2\Delta t}+245.25(\theta_n)$$

I am trying to implement this equation into Matlab code but am having trouble in doing so. I need to create two forms of code, one neglecting Drag Force and one including Drag Force.

So far I have created code that creates a value for each variable but am confused as to how I can create further code that actually implements the Finite Difference Equation. How would this next step be done?