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 Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by