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
.