Euler Cromer Method for Mass-Spring System

13 Ansichten (letzte 30 Tage)
Jessica Jarosz
Jessica Jarosz am 19 Sep. 2017
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 Kommentar
Christopher Ubing
Christopher Ubing am 17 Okt. 2019
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Oussama Jarrousse
Oussama Jarrousse am 19 Sep. 2017
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;

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by