MATLAB Answers

Error using ode45 in matlab

6 views (last 30 days)
tyler brecht
tyler brecht on 23 Oct 2014
Commented: Torsten on 24 Oct 2014
Hi guys
I have a block and spring system with no damping. I'm given the equation x'' = -k*(x1 - L1)/m + k*(x2-x1-w1-L2)/m. Where k = spring stiffness; x1 = initial position of block; L1 = length of spring; x2 = initial position of the end of block; w1 = width of block; L2 = length of second block.
I have converted the second order ode into 2 first order odes.
u1' = x1' = u2
u2' = -k*(u1 - L1)/m + k*(u3-u1-w1-L2)/m
This is my function
function ydot = jipo1(t,y)
m = 2; % mass of the block
k = 5; % Spring stiffness
L1 = 2; % length of unstretched spring
k2 = 5; % Spring stiffness
L2 = 2; % length of unstretched spring
w1 = 5; % length of block
yd1 = y(2);
yd2 = -k*(y(1)-L1)/m + k2*(y(3)-y(1))/m
ydot = [yd1;yd2];
end
This is my ode45 script
clc
m1 = 2; % mass of the block
k = 5; % Spring stiffness
k2 = 5; % Spring stiffness
wn1 = sqrt(k/m1); % Natural frequency
t_final = 20; % Calculation time
L1 = 2; % length of unstretched spring
L2 = 2; % length of unstretched spring
w1 = 5; % length of blockk
x1_0 = 2; % Initial displacement
x_dot_0 = 0; % initial velocity
X_0 = [x1_0,x_dot_0]; % form a vector (array) of initial conditions
[t,y] = ode45(@jipo1,[0,t_final],X_0);
When I run it Matlab gives me this error.
Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in jipo (line 18)
[t,y] = ode45(@jipo1,[0,t_final],X_0);
I would really appreciate any help that will help me understand what I'm doing wrong.

  1 Comment

Torsten
Torsten on 24 Oct 2014
You use the expression
yd2 = -k*(y(1)-L1)/m + k2*(y(3)-y(1))/m
although you only have y(1) and y(2).
Best wishes
Torsten.

Sign in to comment.

Answers (1)

Yu Jiang
Yu Jiang on 23 Oct 2014
In this line
yd2 = -k*(y(1)-L1)/m + k2*(y(3)-y(1))/m
you are using y(3)
However, both y and ydot should only have 2 elements.
What is the physical meaning of y(3), is it noise or disturbance?

  4 Comments

Show 1 older comment
Yu Jiang
Yu Jiang on 23 Oct 2014
I guess you were using piece of code where the system has multiple mass. Do you have one spring or two springs? If you only need one, you can remove the entire term
+ k2*(y(3)-y(1))/m
tyler brecht
tyler brecht on 23 Oct 2014
I have two masses and two springs which are attached to a wall (No damping). The equation for the 1st mass is x'' = -k*(x1 - L1)/m + k*(x2-x1-w1-L2)/m (This equation obeys newtons 2nd law of motion). it looks something like this -
Yu Jiang
Yu Jiang on 24 Oct 2014
In this example you need four states, namely, x1, x2, x1dot, x2dot. You need to simulate them together, instead of only simulating the first mass (the one on the left). In your code you only used 2 states, x1 and x1dot. The equations for x2 and x2dot are missing.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!