How can I write simulations in Erlang?

Posted by rick on Stack Overflow See other posts from Stack Overflow or by rick
Published on 2010-03-05T11:31:57Z Indexed on 2010/03/17 15:31 UTC
Read the original article Hit count: 182

Hi guys,

I want to do some numerical stuff in Erlang like this:

You've got an array with the following values:

[2,3,4]

In each iteration, you calculate

0.1 * [n-1] + 0.7 *[n] + 0.2 * [n+1]

This becomes the new [n].

If n == 0 then [n-1] = 0. If [n] == length of array then [n] = 0.

So I try an example:

[2,3,4]

calculations:

0.1 * 0 + 0.7 * 2 + 0.2 * 3 = 2

0.1 * 2 + 0.7 * 3 + 0.2 * 4 = 3.1

0.1 * 3 + 0.7 * 4 + 0.2 * 0 = 3.1

So [2,3,4] becomes to [2, 3.1, 3.1] after one iteration.

I know how to write this in a non-functional language like C. But I have difficulties to imagine, how this could be done in Erlang. I found some tutorials on how you read a file to a list. So this is not the problem.

How can I spawn different Erlang processes that each process has one element of the list? How can I do calculations by communicating with 'the neighbors', so that the neighbors know where their neigbors are generically, without specifying each? How can I collect the data into a list?

Eventually, is it possible to solve that problem in Erlang at all?

Thanks for your help.

© Stack Overflow or respective owner

Related posts about erlang

Related posts about simulation