loop over expression in Mathematica

Viewed 218

I have an expression containing 3 variables. Two of which are datasets ( p and k) I want to compute this expression for every p and k and have a list of sigma. I am very basic at Mathematica. How can I write a loop over this expression for the data set to have a list of sigma?

p = Import["C:\\Users\\Maryestein\\onlyp.csv", "Dataset"]
K = Import["C:\\Users\\Maryestein\\onlyk.csv", "Dataset"]

Sigma_i^2 = 
 9/(2*Pi^2)* [Sin[K_i*R] - K_i*R*Cos[K_i*R]]^2/(K_i*R)^6*p_i*
  K_i^2*(K_i - K_ (i - 1))

1 Answers

Here are two toy working examples depending on what you exactly want. They should be easy to adapt to your precise function. pi and Ki are dummy variables used for legibility.

If you want to make a computation for every possible couple of something in p and something in K

p = {10, 20, 30};
K = {.1, .2, .3, .4};
Outer[Function[{pi, Ki}, Ki + pi ], p, K, 1]

output

{{10.1, 10.2, 10.3, 10.4}, {20.1, 20.2, 
  20.3, 20.4}, {30.1, 30.2, 30.3, 30.4}}

If your two lists are of the same length and are meant to be used in parallel (as your use of a common indice suggests)

p = {10, 20, 30, 30};
K = {.1, .2, .3, .4};
MapThread[Function[{pi, Ki}, Ki + pi], {p, K}, 1]

output

{10.1, 20.2, 30.3, 30.4}
Related