2 degrees of freedom mass-spring system
조회 수: 21 (최근 30일)
이전 댓글 표시
Hi, I tried to code a script and a function in order to resolve a system composed by two masses and two springs of different values, but I had some troubles in writing the function that resolves the equations of motion.
m1=1; %mass 1 [kg]
m2=2; %mass 2 [kg]
k1=100; %spring 1 [N/m]
k2=150; %spring 2 [N/m]
M=[m1 0;0 m2]; %mass matrix
K=[k1+k2 -k2; -k2 k2]; %stiffness matrix
%% Solving system
tRange=[0 20];
x0=[0.3 0.1 1 4] ;
odefun=@(t,y) solve_sys_2nd_order(t,y,M,K);
[tsol,ysol]=ode45(odefun,tRange,x0);
%%Plot of the response
subplot(2,1,1)
plot(tsol,ysol(:,1),'b');
xlabel('tempo [s]');
ylabel('spostamento [m]');
subplot(2,1,2)
plot(tsol,ysol(:,2),'r')
xlabel('tempo [s]');
ylabel('velocità [m/s]');
The function I wrote is given by
function dy=solve_sys_2nd_order(t,y,m1,m2,k1,k2)
dy=zeros(4,1);
dy(1)=y(3);
dy(2)=y(4);
dy=[y(2);y(4);dy(3);-inv(M)*K*y];
But when I run the code, an error appears saying:
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of
rows in the second matrix. To perform elementwise multiplication, use '.*'.
Error in solve_sys_2nd_order (line 8)
dy=[y(2);y(4);dy(3);-inv(M)*K*y];
Error in two_dof_system>@(t,y)solve_sys_2nd_order(t,y,M,K)
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in two_dof_system (line 24)
[tsol,ysol]=ode45(odefun,tRange,x0);
Can anyone help me please?
댓글 수: 0
답변 (1개)
James Tursa
2019년 4월 18일
In this line
dy=[y(2);y(4);dy(3);-inv(M)*K*y];
looks like M and K are both 2x2 but y is 4x1, hence the error.
Also, your arguments don't match in these two line:
odefun=@(t,y) solve_sys_2nd_order(t,y,M,K);
function dy=solve_sys_2nd_order(t,y,m1,m2,k1,k2)
In the first case you have M and K, but in the second case you have m1, m2, k1, k2.
댓글 수: 4
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!