Mass Spring System ode45 with multiple damping values

조회 수: 8 (최근 30일)
Logan Parrish
Logan Parrish 2022년 2월 22일
댓글: Sulaymon Eshkabilov 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

채택된 답변

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.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by