52
Dr. S.Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R An Introduction to Scientific Computer Programming

An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

An Introduction to Scientific Computer Programming

Page 2: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

R Programming Workshop An introduction to computer programming Using the R computing environment Creating computer programs Solving problems using a computer Using computers as a tool for:

–  Plotting graphs and pictures – Analysis of data –  Learning maths and physics…

Page 3: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Log on

Click on Start

Click on Program Installer

Scroll down, select Rstudio

Click on install Then close the Program Installer

Page 4: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Programming to do what?

Arithmetic – manipulating numbers Lexical analysis – characters and words Algebra – manipulating symbols Syntax – sentences and grammar Logic – Boolean algebra Procedure – algorithms, decision, repetition

Page 5: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

x = 0 0.1000 0.200 0.300 0.400 0.500 0.600 0.70 0.800 0.900 y = 0 0.0997 0.198 0.295 0.388 0.477 0.561 0.64 0.712 0.776

Page 6: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Page 7: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Page 8: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

What is R?

R is an open-source environment for –  statistical computing –  visualisation (plotting graphs, pictures…)

R is an excellent general programming environment beyond statistics R is free and freely available

–  http://www.r-project.org/

Download R onto your PC/Mac and use it!

Page 9: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Getting started with R Logon to a UoL computer.

Start ! All Programs ! RStudio [click]

Type commands at the ‘prompt’ in the R ‘console’.

Page 10: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Page 11: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

The console – Type new commands here

Page 12: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

The console – Type new commands here

Editor - Write “scripts” here

Page 13: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

The console – Type new commands here

Editor - Write “scripts” here Help files, history,

memory manager here

Page 14: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

The console – Type new commands here

Editor - Write “scripts” here Help files, history,

memory manager here

graphics – Plots appear here

Page 15: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

The R Working Directory, and Quitting

> setwd("Rwork") set the working directory

> getwd() get the working directory [1] "Z:/My Documents/Rwork"

> list.files() list files in the working directory

> q() quit R – don’t save workspace

NOTE: empty brackets

Page 16: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Getting Help

From the windows – click “Help” menu at the top of GUI screen – select “R Help”

From the console – type ? plot for help with the “plot” command

From the web - http://www.r-project.org/ Or http://stackoverflow.com/questions/tagged/r

Page 17: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Making the computer do something

> pi [1] 3.141593 > 3*pi [1] 9.424778 > sin(pi/2) [1] 1 > plot(1:20)

Inspect the object called ‘pi’

This is the answer

Page 18: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Making the computer do something

> pi [1] 3.141593 > 3*pi [1] 9.424778 > sin(pi/2) [1] 1 > plot(1:20)

Evaluate ‘3×pi’ and then inspect the result

This is the answer

Page 19: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Making the computer do something

> pi [1] 3.141593 > 3*pi [1] 9.424778 > sin(pi/2) [1] 1 > plot(1:20)

Evaluate sin(pi/2), and inspect the result

This is the answer

NOTE: function(…)

Page 20: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Making the computer do something

> pi [1] 3.141593 > 3*pi [1] 9.424778 > sin(pi/2) [1] 1 > plot(1:20)

Generate a plot using the plot(…) function

NOTE: function(…)

Page 21: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Variables, Objects and Assignment > x <- 3*pi > x [1] 9.424778

Page 22: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Variables, Objects and Assignment > x <- 3*pi > x [1] 9.424778

Evaluate this bit

Assign the result to an object called x

type the name of a variable to see its contents

Page 23: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Variables, Objects and Assignment > x <- 3*pi > x [1] 9.424778 > x <- 1:5 > x [1] 1 2 3 4 5

x is a list (or vector) containing 5 values

Page 24: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Variables, Objects and Assignment > x <- 3*pi > x [1] 9.424778 > x <- 1:5 > x [1] 1 2 3 4 5 > y <- sin(x) > y [1] 0.8414710 0.9092974 0.1411200 -0.7568025 -0.9589243

computes sin(x) for each value of x

Page 25: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Variables, Objects and Assignment > x <- 3*pi > x [1] 9.424778 > x <- 1:5 > x [1] 1 2 3 4 5 > y <- sin(x) > y [1] 0.8414710 0.9092974 0.1411200

-0.7568025 -0.9589243 > y[2] [1] 0.9092974

the second element of the object y

Page 26: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Variables, Objects and Assignment > x <- 3*pi > x [1] 9.424778 > x <- 1:5 > x [1] 1 2 3 4 5 > y <- sin(x) > y [1] 0.8414710 0.9092974 0.1411200

-0.7568025 -0.9589243 > y[3] [1] 0.14112

the third element of the object y

Page 27: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Plotting Graphs

> x <- 1:100 > y <- sin(x)*exp(-x/100) > plot(x, y, type="l")

Note: this is the letter l – for line

Page 28: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

R Script Files

Edit a script file and save (“mysource.R”) –  File ! New file ! R Script –  File ! Save / Save As (in working directory) –  File ! Open

Source the script file (run the script) > source(“mysource.R”) – Or click on Source button in editor window

Page 29: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Brownian motion & random walks Einstein (1905) developed physical theory of Brownian motion

Page 30: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

A 1-dimensional random walk

-4 -3 -2 -1 0 1 2 3 4

start here [x=0] y=0

Page 31: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

A 1-dimensional random walk

-4 -3 -2 -1 0 1 2 3 4

randomly jump +1 or -1 now at y=1

move ahead to x=1

Page 32: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

A 1-dimensional random walk

-4 -3 -2 -1 0 1 2 3 4

randomly jump +1 or -1 now at y=0

move ahead to x=2

Page 33: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

A 1-dimensional random walk

-4 -3 -2 -1 0 1 2 3 4

randomly jump +1 or -1 now at y= -1

move ahead to x=3

Page 34: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

A 1-dimensional random walk

-4 -3 -2 -1 0 1 2 3 4

randomly jump +1 or -1 now at y= -2

move ahead to x=4

what happens to y as we keep advancing x…?

Page 35: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

# generate n.step steps n.step <- 100 jump <- 1 x <- 0:n.step

# compute the y steps dy <- jump * sample(c(-1, 1), size=n.step,

replace=TRUE) plot(dy)

# compute the cumulative y position y <- c(0, cumsum(dy))

# plot the walk plot(x, y, type="s”)

We are going to write a short program

Click in the “editor” window (top left)

Start typing the following commands

Anything starting with a # symbol is a comment – the computer ignores it

Comments are helpful to the reader/writer

Page 36: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

# generate n.step steps n.step <- 100 jump <- 1 x <- 0:n.step

# compute the y steps dy <- jump * sample(c(-1, 1), size=n.step,

replace=TRUE) plot(dy)

# compute the cumulative y position y <- c(0, cumsum(dy))

# plot the walk plot(x, y, type="s")

a command may run over more than one line

Page 37: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

A (short) random walk

Page 38: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Randomly choose a jump of +1 or -1

# generate n.step steps n.step <- 100 jump <- 1 x <- 0:n.step

# compute the y steps dy <- jump * sample(c(-1, 1), size=n.step,

replace=TRUE) plot(dy)

# compute the cumulative y position y <- c(0, cumsum(dy))

# plot the walk plot(x, y, type="s")

Add up all the jumps - cumulative sum

Page 39: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

now change number of steps to 10000 and Source again

# generate n.step steps n.step <- 100 jump <- 1 x <- 0:n.step

# compute the y steps dy <- jump * sample(c(-1, 1), size=n.step,

replace=TRUE) plot(dt)

# compute the cumulative y position y <- c(0, cumsum(dy))

# plot the walk plot(x, y, type="s”)

Page 40: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

A (longer) random walk run (Source) again and again

Page 41: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

# plot a line at y=0 abline(h=0, lty=2)

# plot the start and end points points(n.step, y[1], pch=16, cex=1.5) points(n.step, y[n.step+1], pch=16, cex=1.5)

# join with a line the start and end points lines(c(n.step, n.step), c(0, y[n.step+1]))

# compute the distance travelled r <- y[n.step+1] - y[1]

# print on the plot label <- paste("distance=", signif(r, 3)) text(max(x), max(y), labels=label, pos=2)

Page 42: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

A (longer) random walk

Page 43: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Brownian motion & random walks Now let’s try in 2 dimensions

Pick a random direction to move in, call it theta

Select this from a uniform distribution from 0 to 2π radians (equal probabilities)

Step a distance (jump=1) in direction theta

Resolve into components of the motion in x and in y

Page 44: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

# generate n.step random directions (angles) n.step <- 100 theta <- runif(n.step, 0, 2*pi) jump <- 1

# compute the x and y step sizes dx <- jump * cos(theta) dy <- jump * sin(theta)

# compute the cumulative x and y positions x <- c(0, cumsum(dx)) y <- c(0, cumsum(dy))

# plot the walk plot(x, y, type="l", bty="n", col="red")

Begin a new script file → New file → R Script

Page 45: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

# generate n.step random directions (angles) n.step <- 100 theta <- runif(n.step, 0, 2*pi) jump <- 1

# compute the x and y step sizes dx <- jump * cos(theta) dy <- jump * sin(theta)

# compute the cumulative x and y positions x <- c(0, cumsum(dx)) y <- c(0, cumsum(dy))

# plot the walk plot(x, y, type="l", bty="n", col="red")

Page 46: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Page 47: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

# plot the start and end points points(x[1], y[1], pch=16, cex=1.5) points(x[n.step+1], y[n.step+1], pch=16,

cex=1.5)

# join the start and end points lines(x[c(1, n.step+1)], y[c(1, n.step+1)],

lwd=3)

# compute the distance from start to end r <- sqrt(x[n.step+1]^2 + y[n.step+1]^2)

# print on the plot label <- paste("distance=", signif(r, 3)) text(max(x), max(y), labels=label, pos=2)

Page 48: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Page 49: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

n.step <- 100 n.walks <- 1000 jump <- 1 x <- array(0, dim=n.walks) y <- array(0, dim=n.walks)

for (i in 1:n.walks) { theta <- runif(n.step, 0, 2*pi) dx <- jump * cos(theta) dy <- jump * sin(theta) x[i] <- sum(dx) y[i] <- sum(dy) }

# compute the radial distances r <- sqrt(x^2 + y^2)

# plot the end point of each walk in (x,y) coordinates plot(x, y)

this is a loop - for each of i = 1,2,3,…,n.walks do whatever is {inside}

prepare two arrays to store the outputs

Pythagoras’ theorem

Page 50: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

Page 51: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R

# plot histogram of x's hist(x, breaks=20, col="blue",

border="white", prob=TRUE)

# compare to the Normal curve x.norm <- seq(-50, 50, by=0.2) y.norm <- dnorm(x.norm, mean=0,

sd=sqrt(n.step)*2/pi) lines(x.norm, y.norm, lwd=4)

Still want more…? add this to your 2d script

Page 52: An Introduction to Scientific Computer Programmingzrw/compshop/R_intro.pdf · 2014-07-07 · Dr. S. Vaughan and Prof. R. Willingale Programming with R Department of Physics and Astronomy

Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R