How do I write a spring program?

I have a spring program:
x=((m*g+a*(a+1)*k*d))/((2*a+1)*k)
where m = mass, g = gravity, k = spring constant, d = equals the distance in height between each pair of springs, a = the number of pairs of springs the user inputs, and x = the distance the mass compresses the springs.
This equation works great on one spring and one pair of springs, however, if I used (for example: 10kg, 9.8m/s^2, 200N*m, .2m and 5 pairs of springs) it compresses more than if I used just one spring and one pair.
How do I adjust this to account for an infinite amount of springs where no matter what the user requests it would only engage the 3 total springs?

1 Comment

It is difficult to understand your statement without at least a better explanation about your setup (if not a schematics). I don't understand how these pairs are setup, why d is not a vector if there are multiple pairs, why there would even be a difference in the height between pairs, and why you mention 3 total springs in the end when you seem to say that you are dealing with pairs of springs.

Sign in to comment.

 Accepted Answer

Cedric
Cedric on 13 Mar 2013
Edited: Cedric on 14 Mar 2013
Without having more information from you, the best answer that I can say is that you wait 8 more days and then whatever program you'll write, it will be a "spring program" ;-)
More seriously, if we assume that you have n pairs of springs that support some mass m, and that they all have the same spring constant k, we can write
F = sum(-(2*k) * delta_x) = -(2*k) * sum(delta_x) = mg
where F is the resultant force applied by all springs to the mass (when the mass is steady), and delta_x is a vector of displacements (one component per pair of springs). Now if the mass is homogeneously distributed over all pairs of springs, all delta_x are equal and we can compute a delta_x_m that represents the displacement of the center of mass of m
sum(delta_x) = n * delta_x_m
delta_x_m = - m*g / (2*k*n)
Here we see that n lies in the denominator, so the more pairs of springs you have, the closer to 0 the displacement (negative) of the center of mass is.
Now if the mass is not homogeneously distributed, there is little that you can say unless you know the distribution. The only thing that you know is that delta_x_m will lie between two limit cases:
1. What we've just seen with the homogeneous distribution.
2. All the mass lies on one unique pair of springs, in which case we have
delta_x_m = - m*g / (2*k)
To be more specific to your setup, I would need to know what I mentioned in my comment above.

9 Comments

I am trying to write a program where a single mass determined by the user is set up on springs. The layout of the springs is a pyramid, where only one spring touches the original mass. When the d variable is assigned by the user, that is how far the first single spring must compress to reach the first pair and so forth for each pair. The user may wish to use 50 pairs by the original equation, but the first pair are truly only necessary. my current equation, given in original question, if I use 5 pairs, states that it compresses all 11 springs .49m. That I know to be incorrect. I know there is a loop where the program does the equation for the first set then if x>n*d then it does the equation again until x<(n+1)*d.
I am looking forward to 8 days :)
Cedric
Cedric on 13 Mar 2013
Edited: Cedric on 14 Mar 2013
A pyramid, ..so numbering top down the first level has one spring, the second level has 2 pairs = 4 springs, the 3rd level has 3 pairs = 6 springs, and so one, and that's why there are 11 springs when the user chooses 5 pairs?
If so, assuming weight-less springs, the first level will have a displacement of
delta_x_1 = - m*g / k
the second level
delta_x_2 = - m*g / (2*k*2)
the third level
delta_x_3 = - m*g / (2*k*3)
and so on. The total displacement will just be the sum of them. Does that describe your situation, and if so, how/where do you define d in this context?
Also, in such situation, only a definite set of pairs can b chosen by the user, in the sense that not all numbers of pairs allow to build a "regular" pyramid.. (?)
Almost correct. The first level does have one spring, then the second level has 2 springs and the third has 2 springs and so forth, only adding 2 springs every time. The d that the user is defining is basically the difference in heights till the next set. When the user choses 5 pairs, they are really assuming that the compression will get to the 5th level. So the input of how many pairs is really a limit to the problem. If x>n*d then it states the springs have bottomed out. So the code needs to account for the n limit but also see if the solution is before the nth pair.
Cedric
Cedric on 14 Mar 2013
Edited: Cedric on 14 Mar 2013
Let's discuss some code then, so I get the last bits. Don't get scared by the anonymous functions ( ..=@(..).. ) that I define below; it is just a way to proceed so you don't have to create m-files.
We define a function that returns a vector of numbers of springs per level, when we provide the max level (counting from top down):
>> getSpringsNumbers = @(n) [1, 2:2:2*(n-1)] ;
that we can test:
>> getSpringsNumbers(1)
ans =
1
>> getSpringsNumbers(2)
ans =
1 2
>> getSpringsNumbers(3)
ans =
1 2 4
>> getSpringsNumbers(4)
ans =
1 2 4 6
It seems to be ok. We then define a function that computes the variation in length of levels with a given number of springs, parameterized by the nominal length of springs L0, the spring constant k, and the force F:
>> getDeltaLength = @(nSprings, L0, k, F) max(F./(k*nSprings), -L0) ;
Note the MAX that limits the variation to -L0 (bottom out).We can test it:
>> ns = getSpringsNumbers(4) ; % Vector of # of springs for 4 levels.
ns =
1 2 4 6
>> k = 2 ; L0 = 10 ; F = -15 ; % F = -m*g, but I take 15 for the test.
>> deltaL = getDeltaLength(ns, L0, k, F)
deltaL =
-7.5000 -3.7500 -1.8750 -1.2500
It seems to be working; let's test a case where a few levels bottom out, by increasing the force:
>> F = -50 ;
>> deltaL = getDeltaLength(ns, L0, k, F)
deltaL =
-10.0000 -10.0000 -6.2500 -4.1667
and we see the first two elements (levels 1 and 2) limited to -L0. Now for knowing the total displacement, we can just sum up elements of deltaL:
>> sum(deltaL)
ans =
-30.4167
.. which should not be 0.49m in your case if I understand well ;-). We can also plot the contraction as a function of the level (and # of springs):
>> plot(getDeltaLength(getSpringsNumbers(50), L0, k, F)) ;
.. but I still don't know how your d fits in this context, or even if it is still defined!
first off, talking with you has been very educational. One of my classmates has found a possible solution to the issue and we have come up with a code, more them than me :( but I digress and choke it up to learning a new skill. Here is the code we(they) came up with.
m=input('Please state mass in kg \n');
k=input('Please state spring constant in N/m \n');
d=input('Please input distance from top that the first set of springs will sit \n');
n=(1:2:input('How many pairs of springs would you like to use? \n')*2+1)
a=1;
f=n(a);
s=0;
g=9.8;
x=(m*g/(f*k))+(s*d/f);
while x>a*d
a=a+1;
s=2*(a-1)+s;
f=n(a);
x=(m*g/(f*k))+(s*d/f)
if x>(n(end)+1)*d
disp('All springs have bottomed out. \n')
end
end
ns=n(a);
fprintf('You used %.0f springs to support the weight. \n',ns)
only downside is when we overweight the springs it errors.
Please copy and paste the error, and give inputs that will generate the error so we can try it ourselves.
Cedric
Cedric on 15 Mar 2013
Edited: Cedric on 15 Mar 2013
Thank you for the positive feedback, Chris. Who (them or you) finds the solution is unimportant actually, as long as everybody "experiences the pleasures" of MATLAB programming ;-p Seeing the solution, I think that we are not describing the same system actually.. you mentioned a pyramid; is it like the following (where 3 represent springs)?
___
|_M_|
_3_ top level: 1 spring
_3_3_ 2nd level: 2 springs
_33_33__ 3rd level: 4 springs
33 33 33 4th level: 6 springs
I guess not, because in such system, springs at all level would support the same total weight (assuming that springs are weight-less). Also, only specific numbers of pairs would be allowed in order to build a "regular" pyramid.
Cheers,
Cedric
Chris
Chris on 16 Mar 2013
Edited: Chris on 16 Mar 2013
here is the error.
Attempted to access limitofSprings(5); index out of bounds because numel(limitofSprings)=4.
Error in Final (line 48) functionofspringpositioninlimits=limitofSprings(springPosition); %the new value of n at position a
The values I used are 50kg, 100N/m, .2m, 3 pairs.
___
|_M_|
_3_
_333_
_33333_
_3333333_
Cedric, this was the layout.
However, maybe you could help with the plotting of it. It needs 2 plots one is mass vs number of springs second is compression distance vs spring constant. We have the first one working kinda correctly.
Cedric
Cedric on 17 Mar 2013
Edited: Cedric on 17 Mar 2013
Ok, it's a simpler setup than what I though interpreting the "pairs of springs" that you mentioned.
In the same time, I still don't understand what you model with your d and what you mean when you output
fprintf('You used %.0f springs to support the weight. \n',ns)
In any case, all springs are used to support the weight in the sense that they will all be compress and they will hence all exert a vertical force supporting part of the weight of m.
It is easy to write an analytical expression for the variation of height of each level:
delta_h(ii) = F / (ii*k) = -m*g / (ii*k)
where k is the spring constant and ii is the level ID (starting at 1 at the top). If the mass is not too heavy, all variations are smaller (in absolute value) than the nominal length of springs L0. In such case, you can write an analytical expression for the total variation of height over all levels (which is the distance the center of mass of m will move after being released) for a system with n levels:
delta_h_total = F/k + F/(3*k) + F/(5*k) + .. + F/((2*n-1)*k)
= -m*g/k * (1 + 1/3 + 1/5 + .. + 1/(2*n-1)
Here, you can compute a partial sum of the series 1/ii and obtain a simple expression. This expression fails, however, if any level (especially the top one) bottoms out, and it would be difficult to obtain an analytical expression that includes the fact that springs/levels can bottom out.
Now it is not too complicated to manage such situation numerically. I define a vector of variation in height for e.g.
>> F = -50*9.81 ;
>> k = 100 ;
>> lIds = 1:4 ; % Corresponds to your illustration wiht 4 levels.
>> delta_h = F/k * 1./(2*lIds-1)
delta_h =
-4.9050 -1.6350 -0.9810 -0.7007
Now these are values assuming that the nominal length of springs is greater than 4.905. So if the nominal length is 10, the total variation in height will just be the sum of these variations per level:
>> delta_h_total = sum(delta_h)
delta_h_total =
-8.2217
If the nominal length of springs is L0=3 however, the first spring will bottom out. This can be easily managed; first we detect and count elements of delta_h that are greater than L0 in absolute value:
>> L0 = 3 ;
>> isBottomOut = abs(delta_h)> L0 % Flag indicating "bottom out".
isBottomOut =
1 0 0 0
>> nBottomOut = sum(isBottomOut )
nBottomOut =
1
Then we set these elements to -L0, which is the variation that will happen in practice:
>> delta_h(isBottomOut) = -L0
delta_h =
-3.0000 -1.6350 -0.9810 -0.7007
Now you see the variation in height of the 1st/top level limited to the nominal length of the spring. You can then compute the real total variation in height:
>> delta_h_total = sum(delta_h)
delta_h_total =
-6.3167
and print something, e.g.
>> fprintf('Total var. in height = %.2fm (%d spring(s) bottom out).\n', ...
delta_h_total, nBottomOut) ;
Total var. in height = -6.32m (1 spring(s) bottom out).
Finally, what happens if the user enters 6 for the number of springs? There is no regular pyramid that can be built using 6 springs with the structure that you indicated. Wouldn't it be better if the user would enter the number of levels?
PS: the error that you indicate involves limitofSprings that doesn't appear in your code.

Sign in to comment.

More Answers (0)

Categories

Find more on Argument Definitions in Help Center and File Exchange

Products

Tags

Asked:

on 12 Mar 2013

Community Treasure Hunt

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

Start Hunting!