Book Image

Mastering Scientific Computing with R

Book Image

Mastering Scientific Computing with R

Overview of this book

Table of Contents (17 chapters)
Mastering Scientific Computing with R
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Simulating physical systems


As a brief introduction to simulating physical systems, we will show you how to simulate Brownian motion in R. In physics, Brownian motion is defined as the random movement of particles suspended in liquid or gas caused by the collision of these particles in its surrounding medium. As a result, Brownian motion can be seen as a stochastic process continuous in time. We can simulate this process by successively adding random variables from a normal distribution, where the total number of normal random variables to be simulated represents the total number of discrete time intervals. For example, let's plot Brownian motion in one dimension using 10,000 discrete time intervals as follows:

> motion <- rnorm(10000, 0, 1)
> motion <- cumsum(motion)

> plot(motion, type="l", main="Brownian Motion in 1-Dimension", xlab="time", ylab="displacement")

The result is shown in the following plot:

Alternatively, we could plot simple Brownian motion in two dimensions...