0

I'm solving the eqaution motion and I need numerical integration, so I decided to use ode45 in Matlab. I found the displacement and the velocity, now I need to find the Angular acceleration and make plot with time.

There's my code and function:

Code:

global r b m2 m3 m4 g I M

% Quantities
r=0.2;
b=0.1;
m2=1;
m3=0.2;
m4=2;
g=9.81;
tspan=0:0.01:2;

% Calculation
I=(m2*r^2)/3
M0=m2*g*b+g*r*(m3+m4)
M=1.2*M0

% Runge- Kutta
[t,x]=ode45(@funkce,tspan,[0 0]);

% Plotting 
figure(1);
plot(t,x(:,2));
grid;

figure(2);
plot(t,x(:,1));
grid;

Function:

function v=funkce(t,x);

global r b m2 m3 m4 g I M

% Method
v(1,1)= (x(1)^2*(m4*r^2*cos(x(2))*sin(x(2)))+M-cos(x(2))*m2*g*b+(m3+m4)*g*r)/(I+m3*r^2+m4*r^2*cos(x(2))^2);
v(2,1)= x(1);
Steve Klösters
  • 9,427
  • 2
  • 42
  • 53
MrPitivier
  • 71
  • 9

1 Answers1

1

Maybe, I'm missing something but if you have the displacement and velocity, the only thing you need to do to get the acceleration is to differentiate the velocity:

% Assuming x(:,1) is the velocity, haven't checked your equations
accel = zeros(size(x(:,1)));
accel(2:end) = diff(x(:,1))./diff(t);

However, when I tried to run your code on Octave, ode45 wasn't able to solve the equations. The plots looked like this:

enter image description here

Are you able to obtain meaningful results for the displacement and velocity in MATLAB? If not, I suggest you check your equations and make sure you get sensible results before trying to derive the acceleration.

am304
  • 13,758
  • 2
  • 22
  • 40
  • Thank you, if I understand well, maybe you interpose displacement and velocity: x(:,2) ... displacement, x(:,1) ... velocity – MrPitivier Dec 02 '13 at 09:57
  • Okey, can be correct this? alpha=gradient(x(:,1), t(:)); What do you think? – MrPitivier Dec 02 '13 at 10:06
  • As I said, I didn't know which of `x(:,1)` or `x(:,2)` was the displacement or the velocity. It wasn't clear from your code, and I wasn't going to check your equations for you. You can try `gradient` but `diff` is much simpler for your problem in my opinion. However, you need to check the numerical stability and validity of the solution, and check your equations if need be. – am304 Dec 02 '13 at 10:12
  • Yeah, of course. I know, that the solution is correct, the displacement is x(:,2) and the velocity is (x:,1). But I think your code is correct for acceleration, cause your code is working in my program, so Thank you :) – MrPitivier Dec 02 '13 at 10:16