Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R
An Introduction to Scientific Computer Programming
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…
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
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
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
Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R
Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R
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!
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’.
Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R
Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R
The console – Type new commands here
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
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
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
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
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
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
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
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(…)
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(…)
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
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
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
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
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
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
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
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
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
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
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
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
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
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…?
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
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
Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R
A (short) random walk
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
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”)
Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R
A (longer) random walk run (Source) again and again
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)
Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R
A (longer) random walk
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
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
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")
Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R
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)
Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R
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
Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R
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
Dr. S. Vaughan and Prof. R. Willingale Department of Physics and Astronomy Programming with R