Use Euler's method for Mass-Spring System

17 ビュー (過去 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 Center および File ExchangeAssembly についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by