Scientific Computing (w1) R Computing Workshops An Introduction to Scientific Computing workshop 1.

Slides:



Advertisements
Similar presentations
Introduction to R Brody Sandel. Topics Approaching your analysis Basic structure of R Basic programming Plotting Spatial data.
Advertisements

Working with Profiles in IX1D v 3 – A Tutorial © 2006 Interpex Limited All rights reserved Version 1.0.
MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
 Statistics package  Graphics package  Programming language  Can be used to share/reproduce analyses  Many new packages being created - can be downloaded.
Access - Project 1 l What Is a Database? –A Collection of Data –Organized in a manner to allow: »Access »Retrieval »Use of That Data.
Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming.
By Hrishikesh Gadre Session II Department of Mechanical Engineering Louisiana State University Engineering Equation Solver Tutorials.
XP New Perspectives on Microsoft Office Excel 2003, Second Edition- Tutorial 6 1 Microsoft Office Excel 2003 Tutorial 6 – Working With Multiple Worksheets.
Introduction to MATLAB Simon O’Keefe Non-Standard Computation Group
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
1 Introduction to MatLab MatLab stands for Matrix Laboratory. As the name suggests most of the programming operations have as input or output a matrix.
PowerPoint Add formulae. Course contents Overview: Typing math formulae Lesson1: Type a simple formula Lesson2: Type a complex formula.
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Mathcad Variable Names A string of characters (including numbers and some “special” characters (e.g. #, %, _, and a few more) Cannot start with a number.
Chapter 5 Review: Plotting Introduction to MATLAB 7 Engineering 161.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
Introduction to MATLAB
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
Ranjeet Department of Physics & Astrophysics University of Delhi Working with Origin.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
1 The EDIT Program The Edit program is a full screen text editor that allows you to: Create text files Create text files Edit an existing text files Edit.
Piotr Wolski Introduction to R. Topics What is R? Sample session How to install R? Minimum you have to know to work in R Data objects in R and how to.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
CMPS 1371 Introduction to Computing for Engineers MatLab.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Introduction to Programming with RAPTOR
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Scientific Computing (w2) An Introduction to Scientific Computing workshop 2.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Excel 2007 Part (3) Dr. Susan Al Naqshbandi
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
Today we will learn MATLAB Click Start  All programm  Class Software  Matlab This command window will be seen with a prompt sign >> Any command can.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
1 Lecture 5 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
Postgraduate Computing Lectures PAW 1 PAW: Physicist Analysis Workstation What is PAW? –A tool to display and manipulate data. Learning PAW –See ref. in.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Matlab for Engineers Matlab Environment Chapter 2.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
An Introduction to Programming in Matlab Emily Blumenthal
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
MATLAB DENC 2533 ECADD LAB 9.
MATH 493 Introduction to MATLAB
Code is on the Website Outline Comparison of Excel and R
INTRODUCTION TO MATLAB
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Mr Watson’s Introduction to Spreadsheets
By the end of the lesson, I want you to be able to say…
MatLab Program Used to Calculate Interactive
Presentation transcript:

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 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) Help files, history, memory manager here graphics – Plots appear here The console – Type new commands here Editor - Write scripts 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 - Or

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] Inspect the object called ‘pi’ This is the answer

Scientific Computing (w1) Making the computer do something > pi [1] > 3*pi [1] Evaluate ‘3  pi’ and then inspect the result This is the answer

Scientific Computing (w1) Making the computer do something > pi [1] > 3*pi [1] > sin(pi/2) [1] 1 Evaluate sin(pi/2), and inspect the result This is the answer Always: function(…)

Scientific Computing (w1) Making the computer do something > pi [1] > 3*pi [1] > sin(pi/2) [1] 1 > 1:10 What does this mean?

Scientific Computing (w1) Making the computer do something > pi [1] > 3*pi [1] > sin(pi/2) [1] 1 > 1:10 > plot(1:10) Make a simple plot Always: function(…)

Scientific Computing (w1) Variables, Objects and Assignment > x <- 3*pi > x [1]

Scientific Computing (w1) Variables, Objects and Assignment > x <- 3*pi > x [1] 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] > x <- 1:5 > x [1] 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] > x <- 1:5 > x [1] > y <- sin(x) > y [1] computes sin(x) for each value of x

Scientific Computing (w1) Variables, Objects and Assignment > x <- 3*pi > x [1] > x <- 1:5 > x [1] > y <- sin(x) > y [1] > y[2] [1] the second element of the object y

Scientific Computing (w1) Variables, Objects and Assignment > x <- 3*pi > x [1] > x <- 1:5 > x [1] > y <- sin(x) > y [1] > y[3] [1] the third element of the object y

Scientific Computing (w1) Variables, Objects and Assignment > x <- 1:10 > x [1] > y [1] what happened to y? replace the values stored in x

Scientific Computing (w1) Variables, Objects and Assignment > x <- 1:10 > x [1] > y [1] > y <- sin(x) > y what happens now?

Scientific Computing (w1) Variables, Objects and Assignment > x <- 1:10 > x [1] > y <- seq(2, 3, length=10) > x + y what happens here?

Scientific Computing (w1) Variables, Objects and Assignment > x <- 1:10 > x [1] > 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] > 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 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.

Scientific Computing (w1) Variables, Objects and Assignment > x <- c(1.2, 1.3, 1.4, 2.0) [1] 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 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.

Scientific Computing (w1) = Variables, Objects and Assignment >

Scientific Computing (w1) = Variables, Objects and Assignment > c(1,2,3) + c(2.0,2.5,3.0)

Scientific Computing (w1) = ? when the array sizes aren’t compatible (x has 10 elements, y has 7) they cannot be combined… > x + y

Scientific Computing (w1) = > x + 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 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…

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) > x <- seq(-1, 1, length=100) > y <- asin(x) > plot(x, y, type=“l”) asin(x) isn’t defined outside -1 < x < +1 More maths functions

Scientific Computing (w1) > y <- acos(x); print(y) > y <- tan(x) > y <- sinh(x) > x <- seq(0.1, 10, length=100) > y <- log(x) > y <- log10(x) > y <- sqrt(x) > y <- x^(-0.5) > y <- 2.0^x try some other functions… note the default is the natural logarithm (‘ log ’) More maths functions two lines in one!

Scientific Computing (w1) > x <- c(-0.9, -0.8, 0, 0.8, 0.9) > print(x) > y <- acos(x) > print(y) choose your points the c(…) function combines a 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)

Reading to and writing from files

Scientific Computing (w1) > getwd() Loading some data from a file what is the current working directory?

Scientific Computing (w1) > getwd() > list.files() Loading some data from a file what files are there?

Scientific Computing (w1) > getwd() > list.files() > mydata <-read.table( " header=TRUE) Loading some data from a file use read.table to load a simple text file can load from your disc or off the web – the file name is in quotes (“”) We set header=TRUE inside read.table because the file has a ‘header’ line

Scientific Computing (w1) > getwd() > list.files() > mydata <-read.table( " header=TRUE) > plot(mydata$force, type=“l”) Loading some data from a file 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) > length(mydata$force) Loading some data from a file what does this do?

Scientific Computing (w1) > length(mydata$force) [1] 320 > mydata$x <- 1:320 Loading some data from a file add a new column called ‘x’ to the object ‘mydata’ (contains values 1,2,…320)

Scientific Computing (w1) > length(mydata$force) [1] 320 > mydata$x <- 1:320 > plot(mydata$x, mydata$force, type=“l”) Loading some data from a file plot data from columns ‘x’ and ‘force’ of object ‘mydata’

Scientific Computing (w1) > write.table(mydata, “mydata.txt”) Write some data to a file 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.table(mydata, “mydata.txt”, row.names=FALSE) Write some data to a file switch off the naming of each row (“1”, “2”, …) Now look (again) at the file, e.g. File | Open File

Scientific Computing (w1) > mydata <- read.table(“mydata.txt”, header=TRUE) > x <- mydata$x > y <- mydata$force > plot(x, y, type=“l”) Loading some data from a file this is the file we just wrote (in the working directory)

Scientific Computing (w1) > 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”) Tweaking the plot this means ‘box type’. Try setting equal to any of n, l, u, o for different boxes

Scientific Computing (w1) > 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() Tweaking the plot

Scientific Computing (w1) > 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 Tweaking the plot

Scientific Computing (w1) > 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 Tweaking the plot

Scientific Computing (w1) > 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 Tweaking the plot

Scientific Computing (w1) > 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 > ?par there are a LOT of settings you can play with Tweaking the plot

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 save data into a file: File | Save As… “lab_data.txt”

Scientific Computing (w1) Doing some simple analysis

Scientific Computing (w1) > mydata <- read.table(“lab_data.txt”) > x <- mydata$V1 > y <- mydata$V2 > plot(x, y) NOTE: no ‘header’ in this file Load and plot the data if the columns in a data table don’t have names specified, they default to V1, V2, V3, …

Scientific Computing (w1) > mydata <- read.table(“lab_data.txt”) > x <- mydata$V1 > y <- mydata$V2 > plot(x, y, xlim=c(0,10), ylim=c(0,6)) Load and plot the data 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) > 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) Load and plot the data let’s add some error bars draws line segments between coordinates (x, y-y.error) and (x, y+y.error)

Scientific Computing (w1) > 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) Load and plot the data lm(…) is a powerful function for fitting linear functions to data

Scientific Computing (w1) > result <- lm(y ~ x) Fitting data with a linear model 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 the formula (and data) are given to the lm(…) function which does the hard work for you… the output of lm(…) is saved to an object called ‘result’

Scientific Computing (w1) > result <- lm(y ~ x) > print(result) display the main contents of the ‘result’ object: the best fitting intercept and slope Fitting data with a linear model

Scientific Computing (w1) > 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 Fitting data with a linear model

Scientific Computing (w1) > 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 Fitting data with a linear model

Scientific Computing (w1) > 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’ Fitting data with a linear model

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)