Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

Problem with creating an array containing position of a mass on spring with external force.

1 view (last 30 days)
I’m trying to simulate the position of a mass on a spring. An external force either pushes the mass so the spring is pulled, pushes the masse in the opposite direction so the spring is pushed or it doesn’t push at all. The following array contains the external force as a function of time.
F_c = [0,0,0,1,1,1,1,1,1,1,0,0,0,-1,-1,-1,0,0,0,-1,-1,-1,-1,-1,0,0,0,0,0,0,1,1,0,0,0,-1,-1,-1,0,0,0,0,0,0,1,1,1,0,0,0,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,-1,-1,0,0,0,0,0,0,0,0,-1,-1,-1,-1,0,0,0,1,1,1,0];
I’ve defined an empty array F_s with the same size as F_c. This should contain the spring force.
F_s = zeros(1,size(F_c,2));
Similarly I’ve also defined three arrays a, v, and f which should contain the acceleration of the mass, the velocity of the mass and the distance from rest respectively.
a = zeros(1,size(F_c,2));
v = zeros(1,size(F_c,2));
f = zeros(1,size(F_c,2));
Now I try to calculate the data for F_s, a, v and f in a loop.
for s = 2:size(F_c,2)
F_s(s) = f(s-1)*(-k);
a(s) = (F_c(s)+F_s(s))/m;
v(s) = v(s-1)+a(s);
f(s) = f(s-1)+v(s);
end
The first thing that happens in the loop is the spring force F_s is calculated. k is the spring constant. Second, the acceleration is calculated as the sum of the forces divided by the mass. This is derived from Newton’ second law. Then I calculate the velocity and distance from rest, from the acceleration and velocity. The problem is that when I look at the calculated data, it doesn’t seems like the spring force changes before the last element in the array, which is 6.0629. I can’t see why this happens. The data for a, v and f behaves the same way. In addition, I’ve found out that, if I try to add the two force arrays, I get F_s, which doesn’t make sense. In other words: if I type
F_s+F_c == F_s
it spits out 1s. So I guess it is in the loop when calculating a(s) the error occurs.
Do you have any solution for this, so I can simulate properly?
  2 Comments
Guillaume
Guillaume on 24 May 2015
Your algorithm works without any problem for me. The only thing you've not specified is the value of m.
This is a plot of F_s I obtained for m = 100 without any change to your code:
A minor modification, which will have no impact on the result is that I would initialise the arrays with:
xx = zeros(size(F_c)); %does not change much since size(Fc, 1) == 1. It's just more logical.

Answers (1)

Kim Høg
Kim Høg on 24 May 2015
It works now. After playing a little around with different values for m and k it began to make sense.

This question is closed.

Products

Community Treasure Hunt

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

Start Hunting!