Info

此问题已关闭。 请重新打开它进行编辑或回答。

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

1 次查看(过去 30 天)
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 个评论
Guillaume
Guillaume 2015-5-24
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.

回答(1 个)

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

此问题已关闭。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by