Message Boards Message Boards

0
|
4288 Views
|
1 Reply
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Looping over a list of values

Posted 10 years ago

Hello, it would be great if you can help me to solve this problem I am facing. Let us suppose to have three different list: x={0.0,0.1,0.2,0.2,0.3,0.3,.....} y={0.1,0.2,0.3,0.4,1.0,1.5,2.0,3.0,4.0,5.0,....} z={0.01,0.02,0.022,0.023,0.02344,.....} All three arrays have the same length. Then, I have this function to apply over the list above: f where P is an interpolation function which depends on f[], namely it is created using the past data computed with the function f[] and assuming that P[0]=1. Is there any elegant way to implement this loop ?

Thank you Paolo

POSTED BY: Tarpanelli Paolo

I used your data and adapted the function to make it easy to demonstrate

In[3]:= x = {0.0, 0.1, 0.2, 0.2, 0.3, 0.3} 
y = {0.1, 0.2, 0.3, 0.4, 1.0, 1.5}
 z = {0.01, 0.02, 0.022, 0.023, 0.02344, 0.02345}

Out[3]= {0., 0.1, 0.2, 0.2, 0.3, 0.3}

Out[4]= {0.1, 0.2, 0.3, 0.4, 1., 1.5}

Out[5]= {0.01, 0.02, 0.022, 0.023, 0.02344, 0.02345}

In[7]:= myList = Transpose[{x, y, z}]

Out[7]= {{0., 0.1, 0.01}, {0.1, 0.2, 0.02}, {0.2, 0.3, 0.022}, {0.2, 
  0.4, 0.023}, {0.3, 1., 0.02344}, {0.3, 1.5, 0.02345}}

In[10]:= f[{x_, y_, z_}] := (1/(1 + z y)) Sin[x]

In[11]:= Map[f, myList]

Out[11]= {0., 0.0994357, 0.197367, 0.196858, 0.288752, 0.285479}

The trick is to use Transpose on the three lists to make a list of ordered triples.

I replaced p[x] with Sin[x] to make it easy for me.

You then Map[] the function over the list.

There may be a way to eliminate the braces in the argument list for the function, but I don't remember it off-hand.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract