Question

I'm trying to teach myself how to use MATLAB for solving state-space systems, I have what seems to be a pretty straight-forward system but have been unable to find any decent straight-forward examples for a novice thus far.

I'd like a simple walk-through of how to translate the system into MATLAB, what variables to set, and how to solve for about 50(?) seconds (from t=0 to 50 or any value really).

I'd like to use ode45 since it's a 4th order method using a Runge-Kutta variant.

Here's the 2nd-order equation:

θ''+0.03|θ'|θ'+4pi^2*sinθ=0

The state-space:

x_1'=x_2

x_2'=-4pi^2*sin(x_1)-0.03|x_2|x_2

x_1 = θ, x_2 = θ'

θ(0)=pi/9 rad, θ'(0)=0, h(step)=1
Was it helpful?

Solution

You need a derivative function function, which, given the current state of the system and the current time, returns the derivative of all of the state variables. Generally this function is of the form

function xDash=derivative(t,x)

and xDash is a vector with the derivative of each element, and x is a vector of the state variables. If your variables are called x_1, x_2 etc. it's a good idea to put x_1 in x(1), etc. Then you need a formula for the derivative of each state variable in terms of the other state variables, for example you could have xDash_1=x_1-x_2 and you would code this as xDash(1)=x(1)-x(2). Hopefully that clears something up.

For your example, the derivative function will look like

function xDash=derivative(t,x)
    xDash=zeros(2,1);
    xDash(1)=x(2);
    xDash(2)=-4*pi^2*sin(x(1))-0.03*abs(x(2))*x(2);
end

and you would solve the system using

[T,X]=ode45(@derivative,0:50,[pi/9 0]);

This gives output at t=0,1,2,...,50.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top