Use Euler's method for Mass-Spring System

조회 수: 21 (최근 30일)
Sander Z
Sander Z 2019년 3월 26일
편집: James Tursa 2019년 3월 27일
Hello everybody.
I need to implement Euler's method on a equation based in Mass-Spring System which is:
(m((d^2)x)/(d(t^2)))+(c(dx/dt))+kx=0
Where my x is the displacement (meters), t is the time (seconds), m the mass which is stated as 20kg, my c=10, is the cushioning coefficient and k is the spring value of 20N/m.
So, as my inicial x=1, I need to solve this by Euler with the time interval between 0<=t<=15 seconds.
And them plot the results.
Can you guys help me with the code to solve this?
Thanks so much in advance.
Sander
  댓글 수: 3
Sander Z
Sander Z 2019년 3월 26일
Is there any example that I can view how to implement a code for this type of equation? I'm kinda lost on where to begin with.
James Tursa
James Tursa 2019년 3월 26일
You can read here for starters. It even has a MATLAB code example for one variable (but your case will have two variables):

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

답변 (1개)

James Tursa
James Tursa 2019년 3월 26일
편집: James Tursa 2019년 3월 26일
I will get you started. Let y be a 2-element vector containing your states. Define y as follows
y(1) is defined to be x
y(2) is defined to be xdot
Write out the derivative equations for y in terms of y. E.g.,
d( y(1) ) / dt = dx/dt = xdot = y(2)
d( y(2) ) / dt = d(xdot)/dt = xdotdot = ___________ <- this is what I asked you to solve for above
Just be sure to write that second equation in terms of y(1) and y(2). Once you have these equations figured out, you can write your code. Set initial values for y in your code and then write a loop to implement the Euler method.
Get started on this and then come back here to show us your progress and where you are stuck.
  댓글 수: 9
John D'Errico
John D'Errico 2019년 3월 27일
편집: John D'Errico 2019년 3월 27일
Moved an answer to a comment (by Sander Z):
Got it, but now I'm a little confused, what would be the definition for the variable "x"?
The code must be like this below right?
clear;
clc;
close('all');
% Mass-Spring System with Euler’s Method: (m((d^2)x)/(d(t^2)))+(c(dx/dt))+kx=0
Dt = 0.5; %response time [s]
m = 20; %mass [kg]
k = 20; %spring value [N/m]
c = 40; %cushoning value [Ns/m]
t0 = 0; %integration inicial time [s]
tf = 15; %integration final time [s]
t = t0:Dt:tf; %time vector
y0 = [1.0;0.0]; %inicial state y0 = [x;xp]
n = 1; %number of iterations
y(1) = x;
y(2) = xdot;
for i=1:n
dy(1) = y(2);
dy(2) = (-1/m)*(c*y(2) + k*y(1));
end
James Tursa
James Tursa 2019년 3월 27일
편집: James Tursa 2019년 3월 27일
These lines:
n = 1; %number of iterations
y(1) = x;
y(2) = xdot;
for i=1:n
dy(1) = y(2);
dy(2) = (-1/m)*(c*y(2) + k*y(1));
end
Should be something like this instead:
n = numel(t); %number of iterations
y = y0; % y0 contains the initial state
dy = zeros(2,1); % force dy to be a column vector
for i=1:n-1
dy(1) = y(2);
dy(2) = (-1/m)*(c*y(2) + k*y(1));
y = y + dy * Dt; % you need to update y at each step using Euler method
end
However, this will not store all the intermediate values of y ... it will simply overwrite y with the updated values. If you want to store the intermediate values (e.g., for plotting), you need to modify the above code to do so. Hint: One way to do it is to define y as a 2 x n matrix, where each column y(:,i) ( i.e., the elements y(1,i) and y(2,i) ) is the state at time t(i). See if you can implement that. You will need to change the syntax everywhere you use y to use two indexes.

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

카테고리

Help CenterFile Exchange에서 Assembly에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by