Error using ode45 in matlab
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Torsten
2014-10-24
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.
回答(1 个)
Yu Jiang
2014-10-23
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 个评论
Yu Jiang
2014-10-24
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!