Spring motion using Euler method

조회 수: 1 (최근 30일)
Hayden Stricklin
Hayden Stricklin 2019년 2월 14일
댓글: Hayden Stricklin 2019년 2월 20일
I'm trying to plot the motion of a spring being pulled back and released using the Euler method. I think I have the code generally there, but when I add in omega it spits out a plot of a spiral. I think I just have some simple mistake in there and I can't seem to figure out what it is. Any help is greatly appreciated!
clc
clear all
close all
x(1) = 1;
v(1) = 0;
dt = 0.01;
k = -20;
%spring constant for pulling a spring .1 m with about 2 N of force
vx = 10;
%chose arbitrary value for vx
t = linspace(0, .01, 100);
m = .1;
%chose arbitrary m
w = sqrt((k/m));
F = 2;
for j=1:100;
vx(j+1) = vx(j) - sqrt(k/m) * x(j) *dt -( w* vx(j)* dt);
x(j+1) = x(j) + vx(j) * dt;
end
plot(x)

답변 (1개)

Jim Riggs
Jim Riggs 2019년 2월 14일
Since k has a value of -20, the value of w is a complex number, with a purely imaginary value.
  댓글 수: 3
Jim Riggs
Jim Riggs 2019년 2월 15일
편집: Jim Riggs 2019년 2월 15일
Yes. omega is computed as sqrt(k/m), so if k/m is positive, this is a real number, but if k/m is negative, you are taking the square root of a negative number, and Matlab automatically makes w a complex number.
Try it.
Note, I am also a little confused about the statement that sets v(1) = 0,then later vx=10.
Then, Inside the loop, you reference vx as a subscripted variable, but never reference variable v.
Hayden Stricklin
Hayden Stricklin 2019년 2월 20일
Thanks! I was able to work out what I had wrong. It's always things like that that catch me off gaurd and I don't notice the little errors until someone else points them out.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by