Spring motion using Euler method

3 vues (au cours des 30 derniers jours)
Hayden Stricklin
Hayden Stricklin le 14 Fév 2019
Commenté : Hayden Stricklin le 20 Fév 2019
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)

Réponses (1)

Jim Riggs
Jim Riggs le 14 Fév 2019
Since k has a value of -20, the value of w is a complex number, with a purely imaginary value.
  3 commentaires
Jim Riggs
Jim Riggs le 15 Fév 2019
Modifié(e) : Jim Riggs le 15 Fév 2019
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 le 20 Fév 2019
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming 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!

Translated by