Euler Cromer Method for Mass-Spring System

조회 수: 15 (최근 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;

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by