Spring motion using Euler method

1 visualización (últimos 30 días)
Hayden Stricklin
Hayden Stricklin el 14 de Feb. de 2019
Comentada: Hayden Stricklin el 20 de Feb. de 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)

Respuestas (1)

Jim Riggs
Jim Riggs el 14 de Feb. de 2019
Since k has a value of -20, the value of w is a complex number, with a purely imaginary value.
  3 comentarios
Jim Riggs
Jim Riggs el 15 de Feb. de 2019
Editada: Jim Riggs el 15 de Feb. de 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 el 20 de Feb. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by