Vibration of a mass-spring-damper systemBeijing Institute of Technology | Ming-Jian Li
The following MATLAB code is a companion code for the course on Artificial Intelligence and Simulation Science. It functions to calculate the vibration of a mass point under the action of spring force, external excitation, and damping, with time integration performed using the forward Euler method.
1clear; clc;2m=0.1;3k=2; 4% forced vibration5Fmag=0.1; 6% natrual = sqrt(k/m)/2/pi 7% 0.7118 will cause resonance8freq=1; 9omg=freq*2*pi;10% damp11c=0;12% time13dt = 0.1; 14tend = 10;15t_ = 0 : dt : tend;16d = zeros(length(t_), 1);17d(2) = 0.001;18a=k*d(2);19d(1) = d(2)+dt^2/2*a;20for n = 2 : length(t_)-121 t = t_(n);22 F = Fmag*cos(omg*t);23 rhs = F - (k-2/dt^2*m)*d(n) - (m/dt^2-c/2/dt)*d(n-1);24 coef = m/dt^2 + c/2/dt;25 d(n+1) = rhs/coef;26 clf27 plot(t_, d, 'k', "LineWidth", 1.5)28 hold on29 pause(0.01);30endThe result is as follows. With different input parameters, the vibration differs a lot.
