Download presentation
Presentation is loading. Please wait.
Published byKerrie McDaniel Modified over 9 years ago
1
Scientific Computing (w2) An Introduction to Scientific Computing workshop 2
2
Scientific Computing (w2) R Programming Workshop 2 An introduction to computer programming using the R computing environment Solving problems using a computer Using computers as a tool for: –Plotting graphs and pictures –Analysis of data –Learning maths and physics…
3
Scientific Computing (w2) Getting started with R Logon to a UoL computer. Start All Programs RStudio [click] Type commands at the ‘prompt’ in the R ‘console’.
4
Scientific Computing (w2) Getting Help From Rstudio – click “Help” menu (top) – select “R Help” From the console – type ?plot for help with the “plot” command From the web - http://www.r-project.org/http://www.r-project.org/ Or http://stackoverflow.com/questions/tagged/rhttp://stackoverflow.com/questions/tagged/r
5
Scientific Computing (w2) Refresher: variables and objects
6
Scientific Computing (w2) Variables, Objects and Assignment > x <- 1.2 [1] 1.2 in R, the things you manipulate and store are objects. There are different classes of object and R has methods for treating an object in a manner appropriate to its class. “1.2” is just a number, a scalar. In fact, it’s a floating point number (not an integer). When we assign an object this value, the object instantly takes the right form. In this case ‘x’ becomes an object storing our single floating point number.
7
Scientific Computing (w2) Variables, Objects and Assignment > x <- c(1.2, 1.3, 1.4, 2.0) [1] 1.2 1.3 1.4 2.0 in R, the things you manipulate and store are objects. There are different classes of object and R has methods for treating an object in a manner appropriate to its class. We use the c(…) function to combine several elements (in this case numbers) into one object, often called a vector (a 1D list) or more generally an array (which can have more than 1 dimension). The old ‘x’ has been replaced with a new one, which is now an array storing 5 values.
8
Scientific Computing (w2) Variables, Objects and Assignment > my.name <- “spider man” [1] “spider man” in R, the things you manipulate and store are objects. There are different classes of object and R has methods for treating an object in a manner appropriate to its class. “spider man” is a string of characters. There are 10 characters (including space) stored as a single string. The object ‘my.name’ is created as a string object and assigned this value.
9
Scientific Computing (w2) Variables, Objects and Assignment > my.name <- c(“spider man”, “cat woman”) [1] “spider man” “cat woman” in R, the things you manipulate and store are objects. There are different classes of object and R has methods for treating an object in a manner appropriate to its class. You can have an array of strings too…
10
Scientific Computing (w2) Variables, Objects and Assignment > x <- array(1:20, dim=c(4,5)) Here we define a 2 dimensional array, with size 4x5 (4 rows, 5 columns). There are 20 elements. This produces the numbers 1, 2, …, 20 (20 of them) Create an array. We need to define the values going in to it, and its dimensions.
11
Scientific Computing (w2) Variables, Objects and Assignment > x <- array(1:20, dim=c(4,5)) > x [,1] [,2] [,3] [,4] [,5] [1,] 1 5 9 13 17 [2,] 2 6 10 14 18 [3,] 3 7 11 15 19 [4,] 4 8 12 16 20 > x[2,3] Here is the 4x5 array. Notice the order the values are placed. What does this do?
12
Scientific Computing (w2) Variables, Objects and Assignment > 4 > 3 [1] TRUE Evaluate whether 4 is greater than 3. The answer is TRUE This is a logical object. Possible values are TRUE and FALSE.
13
Scientific Computing (w2) Variables, Objects and Assignment > 4 >= 3 [1] TRUE Evaluate whether 4 is greater than or equal to 3. The answer is still TRUE This is a logical object. Possible values are TRUE and FALSE.
14
Scientific Computing (w2) Variables, Objects and Assignment > 4 <= 3 [1] FALSE Evaluate whether 4 is less than or equal to 3. The answer is FALSE This is a logical object. Possible values are TRUE and FALSE.
15
Scientific Computing (w2) Variables, Objects and Assignment > x <- c(1.0, 2.3, 4.5, 10.0) > x > 4.0 [1] FALSE FALSE TRUE TRUE For each element in the array ‘x’ evaluate whether it is greater than 4 This is an array of logical values. The array has the same size/shape as ‘x’. This is an example of an ‘element-wise’ operation. The operation is simply repeated for each an every element of the arrays involved.
16
Scientific Computing (w2) Some simple data analysis
17
Scientific Computing (w2) Means, variance & all that > print(morley) Expt Run Speed 001 1 1 850 002 1 2 740 003 1 3 900 004 1 4 1070 005 1 5 930 006 1 6 850 The morley data have100 measurements in three columns: Exp = experiment no. (1-5) Run = run no (1-20) Speed = measured speed -299000 km/s R has several datasets built-in and ready to play with, to help you practice data analysis at the computer. Here we use 100 speed of light measurements.
18
Scientific Computing (w2) Means, variance & all that > print(morley$Speed) If we consider just the ‘Speed’ column of the morley array we have 100 numbers in the form of a 1d array (a “vector”) R has several datasets built-in and ready to play with, to help you practice data analysis at the computer. Here we use 100 speed of light measurements.
19
Scientific Computing (w2) Means, variance & all that > print(morley$Speed) > mean(morley$Speed) [1] 852.4 The mean(…) function will compute the mean value of whatever you feed it. In this case it uses the 100 values stored in morley$Speed R has several datasets built-in and ready to play with, to help you practice data analysis at the computer. Here we use 100 speed of light measurements. The output (or returned value) is just one number, the mean. As usual, R treats this as a 1 1 array.
20
Scientific Computing (w2) Means, variance & all that y <- c(3.4,4.5,2.3,-0.01) > mean(y) Try entering your own list of values. Remember to combine them into a single object – using c(…) – which can be processed by the mean(…) function R comes with a lot of functions for data analysis and manipulation. And you can add more by installing packages. There are 5000+ packages you can install to add new commands, some packages add dozens of new commands.
21
Scientific Computing (w2) Means, variance & all that > var(y) > length(y) > sqrt(var(y)) > sd(y) The sd(…) function returns the standard deviation (sqrt of variance). This is a ready-made function that does the same calculation as the previous line. Work from the inside out. The var(…) function returns the number of elements in ‘y’. This is then used as input to the sqrt(…) function. The length(…) function returns the number of elements in ‘y’. The var(…) function will compute the variance value of whatever you feed it. In this case it uses the values you assigned to ‘y’.
22
Scientific Computing (w2) Means, variance & all that > var(y) > length(y) > sqrt(var(y)) > sd(y) > sd(y)/sqrt(length(y)) This computes the “standard error” on the mean of y
23
Scientific Computing (w2) Means, variance & all that > mean(morley$Speed) [1] 852.4 > sd(morley$Speed) [1] 79.01055 > sd(morley$Speed) / sqrt(length(morley$Speed)) > [1] 7.901055 This computes the “standard error” on the mean of the speed
24
Scientific Computing (w2) Plot the speed of light data > plot(morley$Speed) > hist(morley$Speed) The hist(…) function will generate a histogram (and plot it). Try plotting the data
25
Scientific Computing (w2) Plot the speed of light data > plot(morley$Speed) > hist(morley$Speed) > hist(morley$Speed, col=“steelblue”, border=“white”, main=“my histogram”) > hist(morley$Speed, col="steelblue",border="white", breaks=seq(500,1200,by=100)) Define exactly where you want the breaks (between histogram bins) to go.
26
Scientific Computing (w2) Extracting subsets of an array
27
Scientific Computing (w2) Variables, Objects and Assignment > morley$Speed > 900 [1] FALSE FALSE FALSE TRUE...
28
Scientific Computing (w2) Variables, Objects and Assignment > morley$Speed > 900 [1] FALSE FALSE FALSE TRUE... > mask 900) > print(morley$Speed[mask]) store the result of the test in the (logical) array ‘mask’ only keeps elements of morley$Speed for which the corresponding element of mask=TRUE I.e. this is the subset of speeds > 900
29
Scientific Computing (w2) Variables, Objects and Assignment > morley$Speed > 900 [1] FALSE FALSE FALSE TRUE... > mask 900) > print(morley$Speed[mask]) > mean(mask) the logical value TRUE is treated as a numerical value 1, and FALSE is treated as 0. So mean(mask) gives the fraction of TRUE’s. I.e. the fraction of speeds > 900
30
Scientific Computing (w2) Variables, Objects and Assignment > morley$Speed > 900 [1] FALSE FALSE FALSE TRUE... > mask 900) > print(morley$Speed[mask]) > mean(mask) > morley[mask, ] morley is a 100x3 array (100 rows, 3 columns). Here we use mask to select the matching rows only. Leaving the column index empty means it will use all columns
31
Scientific Computing (w2) A simple physics simulation
32
Scientific Computing (w2) Brownian motion & random walks Einstein (1905) developed physical theory of Brownian motion
33
Scientific Computing (w2) A 1 -dimensional random walk -4-3-201234 start at “time” x=0 position y=0
34
Scientific Computing (w2) A 1 -dimensional random walk move ahead to x=1 randomly jump +1 or -1 in y now at y=1 -4-3-201234
35
Scientific Computing (w2) A 1 -dimensional random walk -4-3-201234 move ahead to x=2 randomly jump +1 or -1 in y now at y=0
36
Scientific Computing (w2) A 1 -dimensional random walk -4-3-201234 move ahead to x=3 randomly jump +1 or -1 in y now at y=-1
37
Scientific Computing (w2) A 1 -dimensional random walk -4-3-201234 what happens to y as we keep advancing x…? move ahead to x=4 randomly jump +1 or -1 in y now at y=-2
38
Scientific Computing (w2)
39
Writing you first script
40
Scientific Computing (w2) Help files, history, memory manager here graphics – Plots appear here The console – Type new commands here Editor - Write scripts here
41
Scientific Computing (w2) R Script Files Edit a script file and save (“ mysource.R ”) –to begin a new script: File New file R Script –to save the script: File Save / Save As (in working directory) –to open/edit an existing script: File Open Source the script file (run the script) > source(“mysource.R”) –Or click on Source button in editor window
42
Scientific Computing (w2) We are going to write a short script Click in the “editor” window (top left of RStudio) Start typing the commands on the next slide NOTE: Anything starting with a # symbol is a comment – the computer ignores it Comments are helpful to the reader/writer Remember to Save the script periodically (e.g. Save As…“random_walk.R”)
43
Scientific Computing (w2) # 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 when you have finished typing, save and source (run) the script.
44
Scientific Computing (w2) A (short) random walk
45
Scientific Computing (w2) How does the script work?
46
Scientific Computing (w2) 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") Try this at the console to see how sample(…) works > sample( c(-1, 1), size=10, replace=TRUE) then try it again (use the up arrow key)
47
Scientific Computing (w2) # 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 - a cumulative sum Try this at the console to see how cumsum(…) works > x <- 1:10 > sum(x) > cumsum(x)
48
Scientific Computing (w2) # 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”) now change number of steps to 10000 and Source again
49
Scientific Computing (w2) A (longer) random walk run (Source) again, and again, …
50
Scientific Computing (w2) # 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) Now that it works, we are going to add some more lines to the bottom of our script. Click in the “editor” window (top left) Start typing the following commands at the end your existing script
51
Scientific Computing (w2) # 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)
52
Scientific Computing (w2) A (longer) random walk
53
Scientific Computing (w2) 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
54
Scientific Computing (w2) # 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 Remember to save it periodically
55
Scientific Computing (w2) # 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")
56
Scientific Computing (w2)
57
# 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) Add more lines to the new script
58
Scientific Computing (w2) # 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)
59
Scientific Computing (w2)
60
Interlude: loops > 1:10 > for (i in 1:10) { print(i) } > data.array <- array(0, dim=100) > for (i in 2:length(data.array)) { data.array[i] <- data.array[i-1]+1 print(i) } > data.array try this at the console. what happens between the curly brackets is ‘looped’ you can use many lines inside a loop
61
Scientific Computing (w2) Now that your script works, we are going to make some changes to it. There is no need to replace the entire script. The new script reuses some the lines from our last script. Keep these lines, add to and edit the rest. Recycle code! Save this new one with a different file name. Remember: Anything starting with a # symbol is a comment – the computer ignores it
62
Scientific Computing (w2) 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
63
Scientific Computing (w2)
64
# 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
65
Scientific Computing (w2) What have we learnt? some different kinds of object and data in R how to select subsets of arrays, use logical operators how to compute mean, variance, std. deviation, std. error how to make and plot a histogram how to start, test, edit, load and save a simple script Now you know how to do this, try using these to improve your data analysis and presentation in lab work. perform repetitive/iterative tasks in maths/physics explore (map out) new mathematical functions
66
Scientific Computing (w2)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.