Error using ode45 in matlab

조회 수: 2 (최근 30일)
tyler brecht
tyler brecht 2014년 10월 23일
댓글: Torsten 2014년 10월 24일
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
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
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
tyler brecht
tyler brecht 2014년 10월 23일
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 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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by