- To provide you the plot results, I have assigned system parameters (m, k, c, t_total, dt) to some values
- Create a time array (t) from 0 to t_total with a step size of dt. Also create an array (x_new) to store position and velocity values
- Assign initial position and velocity to the first element of x_new
- Perform numerical integration using the Euler method
- Plot the position-time and velocity-time graphs, respectively
Can someone explain this piece of code:
3 ビュー (過去 30 日間)
古いコメントを表示
I'm very new to Matlab and coding in general and I have to write a program for a spring mass system.
I'm having trouble writing the equations out in Matlab form...
m_c=m_c; % container mass in kg
k_s1=k_s1; % spring constant of s1 in N/m
k_s2=k_s2; % spring constant of s2 in N/m
c=c; % damping coefficient in Ns/m
d=d; % gap in metres
t_total=t_total; % total integration time in seconds
dt=dt; % integration step in seconds
g=9.8; % gravitational acceleration in m/s^2
function[t,x_new]=Massspringdamper(m_c,c,k_s1,k_s2,t_total,dt);
t=0:dt:t_total;
x_new(:,1)=x_start;
for i=2:length(t)
x_delta=[0,1;-k_s1/m_c,-c/m_c]*x_new(:,i-1)*dt;
x_new(:,i)=x_new(:,i-1)+x_delta;
end
% figure()
% plot(t, x_new(1,:), 'o')
% title('Position-time')
% xlabel('time')
% ylabel('position')
[t,x_new]=Massspringdamper(m_c,c,k_s1,k_s2,t_total,dt);
figure()
plot(t, x_new(2,:), 'o')
title('Velocity-time Response')
xlabel('time')
ylabel('velocity')
0 件のコメント
回答 (1 件)
Harshavardhan Putta
2023 年 6 月 22 日
Hi,
I understand that you are facing difficulties in writing the equations for a spring mass system.
The code snippet provided simulates the position and velocity of a spring-mass system using Euler method for numerical integration.
Below is the refactored code snippet. You can review each section and the plots. I have added comments to explain each section for better understanding.
t_total = 10; % total integration time (s)
dt = 0.01; % integration time step (s)
% System parameters
m_c = 1; % container mass in kg
k_s1 = 10; % spring constant of s1 in N/m
k_s2 = 10; % spring constant of s2 in N/m
d = 0; % gap in meters (not used in this version)
g = 9.8; % gravitational acceleration in m/s^2 (not used in this version)
% Initialize variables
t = 0:dt:t_total; % time array
x_new = zeros(2, length(t)); % position and velocity array
% Initial conditions
x_new(:, 1) = [0; 1]; % initial position and velocity
% loop to update the position and velocity at each time step
for i = 2:length(t)
x = x_new(:, i-1); % current position and velocity
x_delta = [x(2); -(k_s1/m_c) * x(1) - (k_s2/m_c) * x(1) - (c/m_c) * x(2)] * dt; % change in position and velocity based on system dynamics
x_new(:, i) = x + x_delta; % update position and velocity
end
% Plot the velocity-time graph
figure();
plot(t, x_new(2, :));
title('Velocity-time Response');
xlabel('Time (s)');
ylabel('Velocity (m/s)');
Here is a breakdown of the code:
I hope this revised code snippet is helpful to you.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!