Double Variable Second order Differential Equation

I am attempting to solve a double mass-spring-damper system. I already solved for single mass using ode45. However I am unable to figure out how to use this with two variables and also solve a second order differential.
The Equation I used was
The function I used is shown below.
function dy=damped_spring_mass(F,m,D,w0,b,t,y)
x=y(1);
v=y(2);
dy=zeros(2,1);
dy(1)=v;
dy(2)=(F*sin(w0*t)-D*x-c*v)/m;
I used this function to in ode45 as shown below
y0=[0;0];
tspan=[0 30];
%% Spring constants
k1= 5;
k01 = 1;
k12 = 2;
c=0.1;
D1= k1+c*(k01+k12);
%% mass constants
m1=1;
%% Damper C0fficients
b1=3;
%% Force
F= 0.1;
w0=2;
%% Diff Eq and plot
[Td,Yd]=ode45(@(t,y) damped_spring_mass(F,m1,D1,w0,b1,t,y),tspan,y0);
plot(Td,Yd(:,1));
For a double mass system, I have two equations
The new constants are provided below
%% Spring constants
k2= 2;
k23 = 3;
k34 = 1;
c=0.1;
D2= k2+c*(k23+k34);
%% mass constants
m2=2;
%% Damper C0fficients
b2=5;
Any help would be greatly appreciated. If you need more information please let me know!

 採用された回答

James Tursa
James Tursa 2021 年 4 月 9 日

1 投票

Just write a derivative function using four states instead of two. The states will be x, y, dxdt, and dydt. The derivitives of these states will be dxdt, dydt, d2xdt2, and d2ydt2. E.g.,
function dy = damped_spring_mass(t,y, other constants )
dxdt = y(3);
x = y(1);
dydt = y(4);
y = y(2);
d2xdt2 = your expression for this in terms of constants and x, y, dxdt, dydt
d2ydt2 = your expression for this in terms of constants and x, y, dxdt, dydt
dy = [dxdt;dydt;d2xdt2;d2ydt2];
end

5 件のコメント

Shabeel Samad
Shabeel Samad 2021 年 4 月 12 日
function dy=damped_spring_mass(F,m,n,K,k,J,w0,d,b,t,y)
dxdt = y(3);
x = y(1);
dydt = y(4);
y = y(2);
dy = [dxdt;dydt;d2xdt2;d2ydt2];
d2xdt2 =(F*sin(w0*t)-d*dxdt+d*dydt-K*x+K*y)/m;
d2ydt2 =((d-b)*dydt-d*dxdt+k*x-J*y)/n;
end
I changed the function and added the new constants. However there seems to be an error when cimpiling the function. Would you know why this happpens.
% Not enough input arguments.
%
% Error in damped_spring_mass (line 3)
% dxdt = y(3);
%
% Error in massdamped>@(t,y)damped_spring_mass(F,m1,n1,K1,k1,J1,w0,b1,t,y)
%
% 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 massdamped (line 57)
% [Td,Yd]=ode45(@(t,y) damped_spring_mass(F,m1,n1,K1,k1,J1,w0,b1,t,y),tspan,y0);
James Tursa
James Tursa 2021 年 4 月 12 日
編集済み: James Tursa 2021 年 4 月 12 日
You need to pass a 4-element y0 vector with the initial values for x, y, xdot, ydot. And you need to make sure all of the constants F,, m1, n1, etc. are defined before you make the ode45( ) call.
Shabeel Samad
Shabeel Samad 2021 年 4 月 12 日
This is what I used when I got the error.
y0=[0; 0; 0; 0]; % 4*1
tspan=[0 30]; % 1*2
Should I change the ode45 equation to 4 variables. If so how do I do it.
[Td,Yd]=ode45(@(t,y) damped_spring_mass(F,m1,n1,K1,k1,J1,w0,b1,t,y),tspan,y0);
plot(Td,Yd(:,1));
Thank you so much for helping
James Tursa
James Tursa 2021 年 4 月 12 日
The dy should be the last line in your derivative function code. E.g.,
function dy=damped_spring_mass(F,m,n,K,k,J,w0,d,b,t,y)
dxdt = y(3);
x = y(1);
dydt = y(4);
y = y(2);
d2xdt2 =(F*sin(w0*t)-d*dxdt+d*dydt-K*x+K*y)/m;
d2ydt2 =((d-b)*dydt-d*dxdt+k*x-J*y)/n;
dy = [dxdt;dydt;d2xdt2;d2ydt2];
end
Shabeel Samad
Shabeel Samad 2021 年 4 月 12 日
編集済み: Shabeel Samad 2021 年 4 月 12 日
The mistake was on my end. Thank you so much MVP!!

サインインしてコメントする。

その他の回答 (1 件)

Merve Buyukbas
Merve Buyukbas 2021 年 4 月 6 日

0 投票

2 件のコメント

Shabeel Samad
Shabeel Samad 2021 年 4 月 9 日
編集済み: Shabeel Samad 2021 年 4 月 9 日
I looked through the links, but none of them solve for both variables with a static input. Thank you for taking your time.
Sajawal Feroze
Sajawal Feroze 2022 年 4 月 20 日
https://www.mathworks.com/matlabcentral/answers/434127-how-to-solve-system-of-2nd-order-differential-equations-using-ode45

サインインしてコメントする。

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by