Euler Cromer Method for Mass-Spring System
13 views (last 30 days)
Show older comments
Jessica Jarosz
on 19 Sep 2017
Commented: Christopher Ubing
on 17 Oct 2019
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 Comment
Christopher Ubing
on 17 Oct 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.
Accepted Answer
Oussama Jarrousse
on 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;
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!