Spring motion using Euler method

1 次查看(过去 30 天)
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