MATLAB Answers

How to solve a mass-spring-damper system with external time-dependent forces.

조회 수: 3(최근 30일)
Muhammad Ali
Muhammad Ali 2021년 8월 12일
댓글: William Rose 2021년 8월 15일
I have developed a code for this mass-spring-damper system, but right now it only works without any exernal time-dependent forces. Below you can see the code in which I have tried to add a force to the second mass but it doesn't work. I have tried the interp1 method in this code. It would be extremely helpful for me if someone can tell me why it's not working and what can I do to fix it. Thank you.
% --------------------------------------------------------
n=2; %Nunber of Masses
pointMass = [1 2 ]; % [kg]
massMatrix = zeros(n,n);
for i = 1 : 1 : length(massMatrix)
massMatrix(i, i) = pointMass(i);
% ---------------------------------------------------------
% --------------------------------------------------------
k=[1000 2000 3000 ];
%First row of the stiffness matrix
%Last row of the stiffness matrix
for i = 2 : n-1
stiffnessMatrix(i,i)= k(i)+k(i+1);
% --------------------------------------------------------
% --------------------------------------------------------
d=[1 2 3 ];
%First row of the stiffness matrix
%Last row of the stiffness matrix
for i = 2 : n-1
dampingMatrix(i,i)= d(i)+d(i+1);
% --------------------------------------------------------
%coefficient matrices
O=zeros(length(massMatrix)); I=eye(length(massMatrix));
A=[O I; -inv(massMatrix)*stiffnessMatrix -inv(massMatrix)*dampingMatrix];
B=[O; inv(massMatrix)];
%Force vector
F1=0; F2=10;
w=5; %frequency
f=[f1 f2 ]';
%time step and time vector
time = 0:dt:4;
y0= [0.01 0 0 0]; %initial condtiions (x1(0)... xn(0) x1dot(0)... xndot(0))
[tsol,ysol] = ode45(@(t,y) testode_2D(t,y,ft,f,A,B), time, y0);
for p = 1 : n
txt = ['x',num2str(p)];
plot(time, ysol(:,p),'DisplayName',txt),
if p == 1
title('Displacement as a function of time','FontSize',30)
ylabel('displacement (m)','FontSize',20)
if p == n
xlabel('time (s)' ,'FontSize',20);
grid on
legend show
function dy=testodeF_2D(t,y,ft,f,A,B)
f = interp1(ft,f,t);
dy= A*y+B*f;

채택된 답변

William Rose
William Rose 2021년 8월 13일
I am not familiar with the method you used in your code to derive the matrices A and B that describe the system. Therefore I derived formulas for A and B using a different approach. My approach is probably less elegant than your way, but I understand my way. It results in the same values for A and B as what your program gives - which is good! See attached document for my approach.
We had a problem like this in my first semster of college, in 1977. There is a way to diagonalize the system matrix and find eigenvectors. The solution is a linear combination of eigenvectors. If the system is symmetric (in other words, the masses are the same, and the springs and dampers on the sides have the same respective values), then the two eigenvectors are 1. a solution in which the two masses move opposite to one another, like a mirror image, and 2. a solution in which the two masses move in parallel, both left, then both right, etc. In the second solution, the distance between the masses never changes.
  댓글 수: 2
William Rose
William Rose 2021년 8월 13일
Here is a link to an eigenvector analysis of the problem. I googled "coupled masses eigenvector solution". The search returned these lecture notes from Harvard, which is where I was a freshman in 1977, when I first met this problem. It seems they are still teaching the same problem, 44 years later.

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

추가 답변(2개)

William Rose
William Rose 2021년 8월 13일
I downloaded your code and saved it as coupledMasses1DextForce0.m. When I ran it, I got an error message about the function definition:
>> coupledMasses1DextForce0
Undefined function or variable 'testode_2D'. ...
This error occurs because the function name in the declaration at the end of the script is "testodeF_2D", which has "F" in it. I removed the "F" from the function name at the end of the script, and I ran the script again. I got a new error message:
>> coupledMasses1DextForce0
Error using interp1>reshapeAndSortXandV (line 424)
X and V must be of the same length. ...
Error in coupledMasses1DextForce0>testode_2D (line 86) ...
This happens because you have defined f so that it is 50x1. It should be 2x25 (given your choice for ft, which, as I explain below, is not a good choice - but we'll get to that later). Thefore I change
f=[f1 f2 ]';
and re-run. Now I get a new error:
>> coupledMasses1DextForce0
Error using *
Incorrect dimensions for matrix multiplication. ...
Error in coupledMasses1DextForce0>testode_2D (line 87)
dy= A*y+B*f;
This happens because interp1() returns a row vector for interpolated f, but you need a column vector. Therefore we change
dy= A*y+B*f;
dy= A*y+B*f';
and re-run. Now the program runs to completion, with no errors. This is good!
However, the figure that is generated shows not data, and the time range is not 0 to 4 seconds as we expect it to be. Here is a screenshot of the figure:
What is going on?
For one thing, the choice of the vector of times, ft, is not good. You have:
However, you also specify a time span of 0 to 4 for integration. Therefore your range for ft should be 0 to 4 (or wider). Also, you specify 25 points between t=1 and t=4, i.e. about 8 Hz sampling. That is not enough, because your sine wave frequency is 5 Hz. You shoudl sample at more than twice the sine wave frequency. I suggest
which gives 50 samples per second from t=0 to t=4. This is 10 samples per cycle, which is enough for a fairly smooth re-creation of the sinusoid by interpolation.
I am also deleting the 'Fontsize' instructions in the plotting section, since the labels are not displaying well. Then re-run. The plot is not much different. The reason is that the array of results, ysol(), is all NaNs. Why?
To be continued.

William Rose
William Rose 2021년 8월 13일
You do not need to use interpolation to add external forcing. I think a better approach is to compute the forces directly in the function that returns the derivatives. The new function is
function dy=testode_2D(t,y,A,B)
F1=0; %amplitude 1
F2=10; %amplitude 2
w=5; %frequency (cycles/s)
dy= A*y+B*f;
and the call to ode45() is now
[tsol,ysol] = ode45(@(t,y) testode_2D(t,y,A,B), time, y0);
Also, you define the time span as a vector of evenly spaced times:
time = 0:dt:4;
I recommend defining the beginning and end times only. Let ode45() compute the time steps. It is designed to do that efficiently, in order to mantain a high level of accuracy. Therefore define time as follows:
time = [0,4]; %time span
With these changes, the script gives nice results. This is good! The script produces the figure below.
The script is attached.

Community Treasure Hunt

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

Start Hunting!

Translated by