Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using a loop to calculate all possible values of a function

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading