Home > OS >  Using an iterative formula to calculate all possible values of a function (python)
Using an iterative formula to calculate all possible values of a function (python)

Time:11-03

This is what i've tried so far to calculate all possible values of w. I am aware i will need some kind of loop but i am still quite new to python so i am unsure of what to use

m = arange(200, 1200, 0.1)
k = arange(1, 7, 0.1)    
w = math.sqrt(k / m)

i would like this to give me a list of all possible values of w from the given possible values of m and k

CodePudding user response:

I think you are looking for the below (not using numpy - just pure python)

import math

m_lst = (200, 1200, 0.1)
k_lst = (1, 7, 0.1)    
w_lst = []

for m in m_lst:
  for k in k_lst:
    w_lst.append(math.sqrt(k / m))
print(w_lst)

output

[0.07071067811865475, 0.18708286933869708, 0.022360679774997897, 0.02886751345948129, 0.07637626158259733, 0.00912870929175277, 3.1622776601683795, 8.366600265340756, 1.0]

CodePudding user response:

If you are trying to do this purely using numpy, then this is something you can do:

import numpy as np
from numpy import arange

m = arange(200, 1200, 0.1)
k = arange(1, 7, 0.1)
w = np.sqrt(k[: np.newaxis] / m)

The k[: np.newaxis] is necessary so that numpy is able to generate a result of the right dimensions, otherwise, a solution similar to what the other answer does, will be necessary.

  • Related