Error using ode45 in matlab

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 comentario

Torsten
Torsten el 24 de Oct. de 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.

Iniciar sesión para comentar.

Respuestas (1)

Yu Jiang
Yu Jiang el 23 de Oct. de 2014

0 votos

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 comentarios

tyler brecht
tyler brecht el 23 de Oct. de 2014
I have no idea why I did put y(3). I have been searching the internet for example codes and most put in a y(3). I have no idea on how to put my ODES into my function that incorporates my initial conditions
Yu Jiang
Yu Jiang el 23 de Oct. de 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 el 23 de Oct. de 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 el 24 de Oct. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre General Applications en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 23 de Oct. de 2014

Comentada:

el 24 de Oct. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by