Mass Spring System ode45 with multiple damping values

15 ビュー (過去 30 日間)
Logan Parrish
Logan Parrish 2022 年 2 月 22 日
I am working on this problem where I have to model multiple mass spring systems that all have different damping values. My model uses the equation of with the initial conditions of m = 5 kg, k = 0.5 N/m, and . I converted this ODE into a system of 1st order ODEs so I could use ode45. I need to model the above equation for when (overdamped) , (critcally damped), and (underdamped). I am confused on how I could be able to specify the different damping conditions with ode45, I was thinking to use it within a for loop for each value c but I am having errors occur within my ode45 function. Any help would be greatly appreciated.
% Constants
m = 5; % kg
k = 0.5; % N/m
c = [0 , sqrt(4*m*k)-1 , sqrt(4*m*k)+1, sqrt(4*m*k)]; % damping constants
A = 1.00002;
B = 3.16228;
w0 = sqrt(k/m);
t = 20;
% Function
% Let
% u1 = y ---> u1' = y' = u2
% u2 = y' ---> u2' = 1/m*(-k*u1-c*u2)
f = @(t,u) [u(2) ; 1/m*(-k*u(1)-c*u(2))];
fA = @(t) A*cos(w0*t)+B*sin(w0*t);
% IC
u0 = [1 1];
tspan = [0 t];
% Solve
for tspan = [0 t]
[t,u] = ode45(f,tspan,u0);
if abs(c^2) > 4*m*k
plot(t,u(:,1))
end
end
  1 件のコメント
KSSV
KSSV 2022 年 2 月 22 日
You can change C here in a loop and get the equation solved.

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

採用された回答

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022 年 2 月 22 日
It is realtively easy to attain the issues of solving the exercises for all damping values and plot them all with appropriate legends:
% Constants
m = 5; % kg
k = 0.5; % N/m
c = [0 , sqrt(4*m*k)-1 , sqrt(4*m*k)+1, sqrt(4*m*k)]; % damping constants
A = 1.00002;
B = 3.16228;
w0 = sqrt(k/m);
t = 20;
% IC
u0 = [1 1];
tspan = [0 t];
% Solve, simulate and plot
figure('name', 'One case of damping: c')
for ii=1:numel(c)
f = @(t,u) [u(2) ; 1/m*(-k*u(1)-c(ii)*u(2))];
fA = @(t) A*cos(w0*t)+B*sin(w0*t);
[t,u] = ode45(f,tspan,u0);
if abs(c(ii)^2) > 4*m*k
plot(t,u(:,1))
end
end
grid on; xlabel('time, [s]'), ylabel('Solution, y(t)')
%% OR you can plot all values of c
clearvars
% Constants
m = 5; % kg
k = 0.5; % N/m
A = 1.00002;
B = 3.16228;
w0 = sqrt(k/m);
c = [0 , sqrt(4*m*k)-1 , sqrt(4*m*k)+1, sqrt(4*m*k)]; % damping constants
t = 20;
% IC
u0 = [1 1];
tspan = [0 t];
figure('name', 'All cases of damping: c')
for ii=1:numel(c)
f = @(t,u) [u(2) ; 1/m*(-k*u(1)-c(ii)*u(2))];
fA = @(t) A*cos(w0*t)+B*sin(w0*t);
[t,u] = ode45(f,tspan,u0);
plot(t,u(:,1)), hold all
L{ii} = ['c = ', num2str(c(ii))];
legend(L{:})
end
grid on; xlabel('time, [s]'), ylabel('Solution, y(t)')
  2 件のコメント
Logan Parrish
Logan Parrish 2022 年 2 月 22 日
Thanks for the help I was really lost on how to set up the loop for the systems.
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022 年 2 月 22 日
Most welcome!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by