Euler Cromer Method for Mass-Spring System
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Jessica Jarosz
le 19 Sep 2017
Commenté : Christopher Ubing
le 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 commentaire
Christopher Ubing
le 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.
Réponse acceptée
Oussama Jarrousse
le 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;
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!