R_handout_1[1]

# R_handout_1[1] - A First Look at R Exploring Gaussian...

This preview shows pages 1–3. Sign up to view the full content.

A First Look at R : Exploring Gaussian Elimination Bruce Cohen and David Sklar January 24, 2008 v0.2 1 Introduction The purpose of this handout is to introduce you to the R -Project [2]. We assume you also have a handout on Gaussian elimination taken from Burden’s book on numerical analysis [1]. Mathematically, we will be heading towards this plan. We want to solve linear system that looks like this: E 1 : 9 x 1 + 3 x 2 + 4 x 3 = 7 E 2 : 4 x 1 + 3 x 2 + 4 x 3 = 8 E 3 : x 1 + x 2 + x 3 = 3 Using matrix notation, we get a coefficient matrix times an x -vector equal to some constant vector. 9 3 4 4 3 4 1 1 1 x 1 x 2 x 3 = 7 8 3 We concatenate the coefficient matrix and the constant matrix to build an augmented matrix, 9 3 4 7 4 3 4 8 1 1 1 3 and use elementary row operations to get a upper triangle matrix like this. 9 3 4 7 0 1 . 66667 2 . 22222 4 . 88889 0 0 -0 . 33333 0 . 26667 We then use backsubstitution to get values for x 1 , x 2 and x 3 . The plan for this handout is to walk you through a little bit of R , and then use R to solve the given linear system. 2 Starting with R 2.1 Using the command line R is an interpreted language which can be run from an R -command line. You can do very simple arithmetic (e.g. add two digits), > 4 + 5 [1] 9 and not so simple (e.g. approximate e π ), > exp(pi) [1] 23.14069 The [1] is part of the way R prints results and is used to help you navigate the output. R is designed to work with data and various data structures. Let’s store our answer into a variable named a . > a = exp(pi) We can see a by typing it > a [1] 23.14069 Note that R does not need “type” declarations. It can determine (at least a temporary) initial type for the data structure. We can make b a list of the 10 normally distributed random numbers. > b = rnorm(9) > b [1] 0.22503607 -0.20218255 0.04255864 [4] -0.31573391 0.55462685 1.90294722 [7] 0.18024143 -0.24645664 -1.55614012 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
By entering > b[7] [1] 0.1802414 we get the seventh element of the b “vector” which is also the first number printed on the third row of the R -output. Now you can see that the number in brackets (e.g. [7] ) at the beginning of each line is the index of the first entry on that line. Note we could avoid printing the navigation index by typing, > cat(b[7]) 0.1802414 The function cat() outputs an object. For people who have used a UNIX command line, cat() will look familiar.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern