I want to calculate all possible values of W when it is the square root of (k / m) and k and m are a list of variables
k = arange(1, 7, 0.1)
m = arange(200, 1200, 1)
def stiff(k, m):
w = math.sqrt(k / m)
return w
print(stiff(k, m))
i have tried to use W = math.sqrt(k / m) but this doesn’t work as the lists are of different sizes so i think i need some sort of loop or iterative method to go through this calculation for all possible values of k and m to calculate each possible value of W.
Any help would be appreciated thanks
>Solution :
You can use numpy.meshgrid then create all possible combinations of two arrays then compute W like below:
import numpy as np
k = np.arange(1, 7, 0.1)
m = np.arange(200, 1200, 1)
kk , mm = np.meshgrid(k,m)
km = np.c_[kk.ravel(), mm.ravel()]
res = np.sqrt(km[:,0]/km[:,1])
Output:
>>> res
array([0.07071068, 0.07416198, 0.07745967, ..., 0.07475286, 0.07530865,
0.07586037])
>>> len(res) , len(k), len(m)
(60000, 60, 1000)