Matlab ode45 numerical solution

1.8k Views Asked by At

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

There are 1 best solutions below

4
On

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.'