Euler Cromer Method for Mass-Spring System

23 ビュー (過去 30 日間)
Jessica Jarosz
Jessica Jarosz 2017 年 9 月 19 日
コメント済み: Christopher Ubing 2019 年 10 月 17 日
Hello,
I've been trying to use the Euler Cromer method for a simple mass-spring system and my plot is not turning out as expected. Instead of getting a sinusoidal function, I get a straight line with 0 slope. If someone could take a look at the code to see what could be causing this, I would greatly appreciate it! Thanks.
% Euler Method for Spring%
clear;
m = 1;
k = 1;
timef = 60;
n = 1000;
ts = timef/n;
v = zeros(1, n+1);
x = zeros(1, n+1);
t = zeros(1, n+1);
a = zeros(1, n+1);
v(1) = 0;
x(1) = 1;
t(1) = 0;
for i = 1:n
t(i+1) = t(i) + ts;
a(i+1) = -k*x(i)/m;
x(i+1) = x(i) + v(i+1)*ts;
v(i+1) = v(i) + a(i)*ts;
end
plot(t, x, 'r')
xlabel('time')
ylabel('position')
  1 件のコメント
Christopher Ubing
Christopher Ubing 2019 年 10 月 17 日
At the start of the for loop, try n-1, instead of n. I was using your code for a drag problem and found that if I kept the n, I got into an infinite loop, but the n-1 generated the correct results.

サインインしてコメントする。

採用された回答

Oussama Jarrousse
Oussama Jarrousse 2017 年 9 月 19 日
Hello. It looks like you are using v(i+1) while it's value is still 0
Just switch the order in which you calculate v(i+1) and x(i+1)
So this line v(i+1) = v(i) + a(i)*ts; before this line x(i+1) = x(i) + v(i+1)*ts;
  1 件のコメント
Jessica Jarosz
Jessica Jarosz 2017 年 9 月 19 日
This was it. Thanks again..

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by