pptx

advertisement
Scientific Computing (w1)
R Computing Workshops
An Introduction to Scientific Computing
workshop 1
Scientific Computing (w1)
Before we begin… (you only need to do this the
first time)
•
•
•
•
•
Log on
Click on Start
Click on Program Installer
Scroll down, select R for Windows 3.2.0
Click on Install
(may take a minute or two at busy times…)
• Select RStudio
• Click on Install
Scientific Computing (w1)
Getting started with R
Log on to a UoL computer.
Start  All Programs  RStudio [click]
Type commands at the ‘prompt’ in the R
‘console’.
Scientific Computing (w1)
Editor Write scripts here
The console –
Type new commands here
Help files, history,
memory manager here
graphics –
Plots appear here
Scientific Computing (w1)
R Programming Workshop 1
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…
Scientific Computing (w1)
The R Working Directory, and Quitting
> getwd()
get the working directory
[1] "Z:/My Documents/Rwork"
> setwd(“Z:/My Documents/Rwork")
set the working directory
> list.files()
list files in the working directory
> q()
quit R – don’t save workspace
NOTE: empty brackets
Scientific Computing (w1)
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/
Or http://stackoverflow.com/questions/tagged/r
Scientific Computing (w1)
Simple calculations
Scientific Computing (w1)
Making the computer do something
> 1
[1] 1
if you type something at the
console and hit return, R will
print its value onscreen
Scientific Computing (w1)
Making the computer do something
> 1
[1] 1
> 1+2
[1] 3
> 2*3
The * means ‘multiply’
Scientific Computing (w1)
Making the computer do something
> 1
[1] 1
> 1+2
[1] 3
> 2*3
[1] 6
> “dude!”
[1] “dude!”
The [1] is because R treats all
data as an array of values, even
if there’s just one number or
one string (of characters)
Scientific Computing (w1)
Making the computer do something
> pi
[1] 3.141593
Inspect the object
called ‘pi’
This is the answer
Scientific Computing (w1)
Making the computer do something
> pi
[1] 3.141593
> 3*pi
[1] 9.424778
Evaluate ‘3pi’ and then
inspect the result
This is the answer
Scientific Computing (w1)
Making the computer do something
> pi
[1] 3.141593
> 3*pi
[1] 9.424778
> sin(pi/2)
[1] 1
Always: function(…)
Evaluate sin(pi/2),
and inspect the result
This is the answer
Scientific Computing (w1)
Making the computer do something
> pi
[1] 3.141593
> 3*pi
[1] 9.424778
> sin(pi/2)
[1] 1
> 1:10
What does this mean?
Scientific Computing (w1)
Making the computer do something
> pi
[1] 3.141593
> 3*pi
[1] 9.424778
> sin(pi/2)
[1] 1
> 1:10
> plot(1:10)
Always: function(…)
Make a simple plot
Scientific Computing (w1)
Variables, Objects and Assignment
> x <- 3*pi
> x
[1] 9.424778
Scientific Computing (w1)
Variables, Objects and Assignment
> x <- 3*pi
> x
[1] 9.424778
Evaluate this bit
Assign the result to an object
called x. Notice we use two
symbols ‘<’ and ‘-’ together
(can also use ‘=’)
type the name of an object
to see its contents
Scientific Computing (w1)
Variables, Objects and Assignment
> x <- 3*pi
> x
[1] 9.424778
> x <- 1:5
> x
[1] 1 2 3 4 5
The object x is a list of numbers
(a vector or 1 dimensional array)
containing 5 values
1:5 means “1 to 5”
Scientific Computing (w1)
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.7568025 -0.9589243
computes sin(x) for
each value of x
0.1411200 -
Scientific Computing (w1)
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.7568025 -0.9589243
> y[2]
[1] 0.9092974
0.1411200
the second element
of the object y
Scientific Computing (w1)
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.7568025 -0.9589243
> y[3]
[1] 0.14112
0.1411200
the third element
of the object y
Scientific Computing (w1)
Variables, Objects and Assignment
> x <- 1:10
replace the values stored in x
> x
[1] 1 2 3 4 5 6 7 8 9 10
> y
[1] 0.8414710 0.9092974 0.1411200
-0.7568025 -0.9589243
what happened to y?
Scientific Computing (w1)
Variables, Objects and Assignment
> x <- 1:10
> x
[1] 1 2 3 4 5 6 7 8 9 10
> y
[1] 0.8414710 0.9092974
-0.7568025 -0.9589243
> y <- sin(x)
> y
0.1411200
what happens now?
Scientific Computing (w1)
Variables, Objects and Assignment
> x <- 1:10
> x
[1] 1 2 3 4 5 6 7 8 9 10
> y <- seq(2, 3, length=10)
> x + y
what happens here?
Scientific Computing (w1)
Variables, Objects and Assignment
> x <- 1:10
> x
[1] 1 2 3 4 5 6 7 8 9 10
> y <- seq(2, 3, length=10)
> x + y
> y <- seq(2, 3, length=7)
> x + y
what went wrong here?
Scientific Computing (w1)
Variables, Objects and Assignment
> x <- 1:10
> x
[1] 1 2 3 4 5 6 7 8 9 10
> y <- seq(2, 3, length=10)
> x + y
> y <- seq(2, 3, length=7)
> x + y
> x + 1
but this one works!
when the array sizes aren’t compatible (x has 10 elements, y has 7) they
cannot be combined… except if one array can be repeated (an integer
number of times) to match the size of the other (array of size 1 is scaled
into array of size 10 by repeating values)
Scientific Computing (w1)
Variables, Objects and Assignment
> x <- 1.2
[1] 1.2
“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.
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.
Scientific Computing (w1)
Variables, Objects and Assignment
> x <- c(1.2, 1.3, 1.4, 2.0)
[1] 1.2 1.3 1.4 2.0
We use the c(…) function to combine a several numbers into one object,
often called a vector (a 1D list of numbers) or 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 four values.
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.
Scientific Computing (w1)
Variables, Objects and Assignment
> 1
1
+
+
2.0
2.000000
=
3.000000
Scientific Computing (w1)
Variables, Objects and Assignment
> c(1,2,3) + c(2.0,2.5,3.0)
1
2
3
+
2.000000
2.500000
3.000000
=
3.000000
4.500000
6.000000
Scientific Computing (w1)
> x
1
2
3
4
5
6
7
8
9
10
+
+
y
2.000000
2.166667
2.333333
2.500000
2.666667
2.833333
3.000000
when the array sizes
aren’t compatible (x
has 10 elements, y has
7) they cannot be
combined…
=
?
Scientific Computing (w1)
> x
1
2
3
4
5
6
7
8
9
10
+ 1
+
1
=
?
when the array sizes
aren’t compatible (x
has 10 elements, y has
7) they cannot be
combined… except if
one array can be
repeated (an integer
number of times) to
match the size of the
other (array of size 1
is scaled into array of
size 10 by repeating
values)
Scientific Computing (w1)
> x
+ 1
1
2
3
4
5
6
7
8
9
10
1
1
1
1
1
1
1
1
1
1
+
=
2
3
4
5
6
7
8
9
10
11
when the array sizes
aren’t compatible (x
has 10 elements, y has
7) they cannot be
combined… except if
one array can be
repeated (an integer
number of times) to
match the size of the
other (array of size 1
is scaled into array of
size 10 by repeating
values)
Scientific Computing (w1)
Making simple plots
Scientific Computing (w1)
Plotting Graphs
> x <- 1:100
> y <- sin(x)*exp(-x/100)
Notice how we write the function…
y  sin( x)e
 x / 100
Scientific Computing (w1)
Plotting Graphs
> x <- 1:100
> y <- sin(x)*exp(-x/100)
> plot(x, y)
make a plot, putting points at each pair of
coordinates (x[i], y[i]) where i=1,2,…,100
Scientific Computing (w1)
Plotting Graphs
> x <- 1:100
> y <- sin(x)*exp(-x/100)
> plot(x, y, type="l")
Change the plot for one using a line.
Note: this is the letter l – for line – not the number 1
Scientific Computing (w1)
Plotting Graphs, again… (1)
> x <- seq(-10, 10, by=0.1)
> y <- sin(x)*exp(-x/100)
> plot(x, y, type="l")
Use “up” arrow key to bring back previous lines
(no need to re-type)
Scientific Computing (w1)
Plotting Graphs, again… (2)
> x <- seq(-10, 10, by=0.1)
> y <- 2*x + 1
> plot(x, y, type="l")
Try a new function…
Scientific Computing (w1)
Plotting Graphs, again… (3)
> x <- seq(-10, 10, by=0.1)
> y <- 2*x^2 – x + 1
> plot(x, y, type="l")
Try a new function…
Scientific Computing (w1)
Plotting Graphs, again… (4)
> x <- seq(-10, 10, by=0.1)
> y <- (sin(x))^2 +
cos(x/2-0.1)
> plot(x, y, type="l")
If you hit return on an unfinished line, R will wait for
you to finish…
Scientific Computing (w1)
More maths functions
> y <- asin(x)
Warning message:
In asin(x) : NaNs produced
asin(x) means arcsin(x) or sin-1(x)
what’s the problem here?
Take another look at x
Scientific Computing (w1)
More maths functions
> x <- seq(-1, 1, length=100)
> y <- asin(x)
> plot(x, y, type=“l”)
asin(x) isn’t defined outside -1 < x < +1
Scientific Computing (w1)
More maths functions
try some other functions…
>
>
>
>
>
>
>
>
>
y
y
y
x
y
y
y
y
y
<<<<<<<<<-
acos(x); print(y)
tan(x)
two lines in one!
sinh(x)
seq(0.1, 10, length=100)
log(x)
log10(x)
note the default is
sqrt(x)
the natural logarithm
x^(-0.5)
(‘log’)
2.0^x
Scientific Computing (w1)
choose your points
>
>
>
>
x <- c(-0.9, -0.8, 0, 0.8, 0.9)
print(x)
y <- acos(x)
the c(…) function combines a
print(y)
list of values into a single object
– very useful!
at the console you don’t need
to explicitly include the
print(…) function. But in a
script you do (more later…)
Scientific Computing (w1)
Scientific Computing (w1)
Reading to and writing from files
Scientific Computing (w1)
Loading some data from a file
> getwd()
what is the current
working directory?
Scientific Computing (w1)
Loading some data from a file
> getwd()
> list.files()
what files are there?
Scientific Computing (w1)
Loading some data from a file
> getwd()
> list.files()
> mydata <-read.table(
"http://www.star.le.ac.uk/sav2/waves.dat",
header=TRUE)
We set header=TRUE inside
read.table because the file
has a ‘header’ line
use read.table to
load a simple text file
can load from your disc
or off the web – the file
name is in quotes (“”)
Scientific Computing (w1)
Loading some data from a file
> getwd()
> list.files()
> mydata <-read.table(
"http://www.star.le.ac.uk/sav2/waves.dat",
header=TRUE)
> plot(mydata$force, type=“l”)
the object mydata now contains the contents of the file.
This is a single column of data. The column is named
‘force’ in the file header. We use
“[object]$[column]”
Scientific Computing (w1)
Loading some data from a file
> length(mydata$force)
what does this do?
Scientific Computing (w1)
Loading some data from a file
> length(mydata$force)
[1] 320
> mydata$x <- 1:320
add a new column called ‘x’ to the object
‘mydata’ (contains values 1,2,…320)
Scientific Computing (w1)
Loading some data from a file
> length(mydata$force)
[1] 320
> mydata$x <- 1:320
> plot(mydata$x, mydata$force, type=“l”)
plot data from columns ‘x’ and ‘force’ of object ‘mydata’
Scientific Computing (w1)
Write some data to a file
> write.table(mydata, “mydata.txt”)
object containing data to be
saved (‘mydata’)
name of file (in working
directory) to be saved
Now look at the file, e.g. File | Open File
Scientific Computing (w1)
Write some data to a file
> write.table(mydata, “mydata.txt”,
row.names=FALSE)
switch off the naming of
each row (“1”, “2”, …)
Now look (again) at the file, e.g. File | Open File
Scientific Computing (w1)
Loading some data from a file
> mydata <- read.table(“mydata.txt”,
header=TRUE)
> x <- mydata$x
> y <- mydata$force
> plot(x, y, type=“l”)
this is the file we just wrote
(in the working directory)
Scientific Computing (w1)
Tweaking the plot
> mydata <- read.table(“mydata.txt”,
header=TRUE)
> x <- mydata$x
> y <- mydata$force
> plot(x, y, type=“l”)
> plot(x, y, type=“l”, bty=“n”)
this means ‘box type’. Try setting equal to
any of n, l, u, o for different boxes
Scientific Computing (w1)
Tweaking the plot
> mydata <- read.table(“mydata.txt”,
header=TRUE)
> x <- mydata$x
> y <- mydata$force
> plot(x, y, type=“l”)
> plot(x, y, type=“l”, bty=“n”)
> plot(x, y, type=“l”, bty=“n”, col=“blue”)
set colour of the points or line
to see a list of colours type colours()
Scientific Computing (w1)
Tweaking the plot
> mydata <- read.table(“mydata.txt”,
header=TRUE)
> x <- mydata$x
> y <- mydata$force
> plot(x, y, type=“l”)
> plot(x, y, type=“l”, bty=“n”)
> plot(x, y, type=“l”, bty=“n”, col=“blue”,
lwd=2)
change line width
Scientific Computing (w1)
Tweaking the plot
> mydata <- read.table(“mydata.txt”,
header=TRUE)
> x <- mydata$x
> y <- mydata$force
> plot(x, y, type=“l”)
> plot(x, y, type=“l”, bty=“n”)
> plot(x, y, type=“l”, bty=“n”, col=“blue”,
lwd=2, cex.axis=1.5)
there are many ‘cex’ (character expansion) arguments,
this one is for the axis numbering
Scientific Computing (w1)
Tweaking the plot
> mydata <- read.table(“mydata.txt”,
header=TRUE)
> x <- mydata$x
> y <- mydata$force
> plot(x, y, type=“l”)
> plot(x, y, type=“l”, bty=“n”)
> plot(x, y, type=“l”, bty=“n”, col=“blue”,
lwd=2, cex.axis=1.5, xlab=“time”,
ylab=“force”)
ylab and xlab arguments are used
to set the y and x axis labels
Scientific Computing (w1)
Tweaking the plot
> mydata <- read.table(“mydata.txt”,
header=TRUE)
> x <- mydata$x
> y <- mydata$force
> plot(x, y, type=“l”)
> plot(x, y, type=“l”, bty=“n”)
> plot(x, y, type=“l”, bty=“n”, col=“blue”,
lwd=2, cex.axis=1.5, xlab=“time”,
ylab=“force”)
> ?plot
there are a LOT of settings you can
> ?par
play with
Scientific Computing (w1)
More about plots
• New plots always begin with a ‘high level’ plot command,
usually plot(…)
• You can add to this using segments(…), abline(…),
points(…) and so on.
• Once you’re happy with a plot use the ‘Export’ menu (over the
plot window) to save a PDF, PS, PNG, JPG, or save into
clipboard.
• Use  and  buttons over plot window to move between
previous plots
Scientific Computing (w1)
Plot data from a lab experiment
write data into a file: File | New File | Text File
1.0
1.4
2.3
3.3
4.5
5.7
6.1
8.5
9.8
0.47
0.87
0.24
2.00
1.30
2.80
3.40
4.40
4.00
save data into a file: File | Save As… “lab_data.txt”
Scientific Computing (w1)
Doing some simple analysis
Scientific Computing (w1)
Load and plot the data
>
>
>
>
mydata <- read.table(“lab_data.txt”)
x <- mydata$V1
NOTE: no ‘header’ in this file
y <- mydata$V2
plot(x, y)
if the columns in a data table
don’t have names specified, they
default to V1,V2,V3, …
Scientific Computing (w1)
Load and plot the data
>
>
>
>
mydata <- read.table(“lab_data.txt”)
x <- mydata$V1
y <- mydata$V2
plot(x, y, xlim=c(0,10), ylim=c(0,6))
xlim is given two values which set the lower and upper
limits of the x axis. The two limits are combined into a single
object using c(lower, upper)
Scientific Computing (w1)
Load and plot the data
>
>
>
>
>
>
mydata <- read.table(“lab_data.txt”)
x <- mydata$V1
let’s add some error bars
y <- mydata$V2
plot(x, y, xlim=c(0,10), ylim=c(0,6)))
y.error <- 1.0
segments(x, y-y.error, x, y+y.error)
draws line segments between coordinates
(x, y-y.error) and (x, y+y.error)
Scientific Computing (w1)
Load and plot the data
>
>
>
>
>
>
>
mydata <- read.table(“lab_data.txt”)
x <- mydata$V1
y <- mydata$V2
plot(x, y, xlim=c(0,10), ylim=c(0,6)))
y.error <- 1.0
segments(x, y-y.error, x, y+y.error)
result <- lm(y ~ x)
lm(…) is a powerful function for fitting linear functions to data
Scientific Computing (w1)
Fitting data with a linear model
the formula (and data) are given to the
lm(…) function which does the hard work
for you…
> result <- lm(y ~ x)
the output of
lm(…) is saved to
an object called
‘result’
this is a ‘formula’ – a special kind of
object in R. It says to ‘fit y as a linear
function of x’). Note the special tilde
‘~’ symbol for formulae
Scientific Computing (w1)
Fitting data with a linear model
> result <- lm(y ~ x)
> print(result)
display the main
contents of the ‘result’
object: the best fitting
intercept and slope
Scientific Computing (w1)
Fitting data with a linear model
> result <- lm(y ~ x)
> print(result)
> summary(result)
display a more
informative summary
of the ‘result’ object:
the best fitting
intercept and slope,
with additional error
analysis
Scientific Computing (w1)
Fitting data with a linear model
>
>
>
>
result <- lm(y ~ x)
print(result)
summary(result)
abline(a=-0.07, b=0.47)
abline(…) adds a
straight line with
intercept a and slope b
Scientific Computing (w1)
Fitting data with a linear model
>
>
>
>
>
result <- lm(y ~ x)
print(result)
summary(result)
abline(a=-0.07, b=0.47)
abline(result)
or just get the slope
and intercept out of
the object ‘result’
Scientific Computing (w1)
What have we learnt?
• start R / Rstudio (and you can install it at home)
• use R as a simple calculator
• compute values for elementary functions
• evaluate y = f(x) at multiple x values
• plot (x, y) coordinates as points, or join as a curve
• load a data table from a file or from the web
• save a data table to a file
• make a plot (with error bars) of some data
• perform and plot a simple linear fit to the data
Now you know how to do this, try using these to improve
your data analysis and presentation in lab work.
Scientific Computing (w1)
Download