# Matlab ode45 numerical solution

I'm trying to solve a 2nd order differential equation, using the Runga Kutta's ode45 function in Matlab. It's for a bachelor project, where I'm trying to simulate the behavior of a spherical robot, with a pendulum swinging inside to cause it to roll.

So far it's limited to only roll in one direction and is tested where the pendulum is started at an angle of -pi/4 and should in this case just cause and oscillation, where the angle is measured in the equation.

The problem is, when I plot the graph, I always get some odd linear component. Tried the program on another computer, where it did not show up, so I'm kinda lost here. I also tried to simulate the pendulum itself, where the graph is as shown:

pendulum swing http://imagizer.imageshack.us/a/img801/1842/03s4.jpg

The sine with the greater amplitude is the displacement of the angle.

The program is as follows:

function xdot = pendulum(t,y);
xdot = zeros(2,1);

g = 9.82; L = 1;

xdot(1) = y(2);
xdot(2) = -(g*sin(y(1))/L;

x0 = [-pi/4 0 ];
[T Y] = ode45(@pendulum, [0 20], x0);
plot([T Y])


Hope that someone know how to correct this error, since it's making it nearly impossible to get an acceptable simulation, when I'm adding the sphere in the equations of motion.

## 1 Answer

Use plot(T,Y) instead of plot([T Y]). Also there are some other syntax errors that need correcting.

So to answer the person who downvoted me - what you are doing is overlaying the two graphs plot(T) and plot(Y). And of course plot(T) has a 'linear component.'

• This is not a very helpful answer and really could have been a comment in its current state. It may correct an programming error, but it doesn't answer the OP's question about the linear component (time) or help him understand plot, ode45, or numerical integration more deeply. – horchler Mar 23 '14 at 21:45
• @horchler His so called linear component is actually an artifact created by his syntax error in the plot command. His usage of ode45 is totally correct. – Stephen Montgomery-Smith Mar 23 '14 at 21:54
• Actually, it's perfectly correct syntax, just perhaps not the one the OP should be using. With a single argument, plot plots elements columnwise vs. their index (1:end). Unless the single input is complex, in which cast the imaginary part is plotted vs. the real part. – horchler Mar 23 '14 at 23:13
• @horchler Fair enough. – Stephen Montgomery-Smith Mar 23 '14 at 23:28