1180:Lab1 1 The very Basics January 8th, 2012

advertisement
1180:Lab1
January 8th, 2012
Goals For This Week
Just a review
1
The very Basics
Create a new folder called ”math1180”. If you are on a campus computer right
click on the desktop and select open terminal from the menu. On Campus
Computers:
>mkdir math1180
>cd math1180
Then type
>gedit lab1.R &
>R
2
Plotting Data
When we create a vector or list of numbers, there are two commands that we
can use. We can use ’c’ when there is no clear pattern to the numbers (such
as when we record measured data). We use ’seq’ if there is a pattern. For
example, if we want evenly spaced numbers from 5 to 12 we use the commands
below. By default, the spacing will be 1. If we add in an extra argument then
we can manipulate spacing. In the case below, the spacing is .5 so there are
more numbers.
> p<-c(10,5,8,13,7,2,19,3)
> t1<-seq(5,12)
> t2<-seq(5,12,.5)
Once we have vectors, we can plot them using the appropriately named
’plot’ command. At the bare minimum, the plot command requires the x and
y coordinates of points you want to plot.
>plot(t1,p)
1
If you reverse the arguments then the x and y coordinates will be flipped.
>plot(p,t1)
If these two vectors have different lengths then you get an error.
>plot(t2,p)
Error in xy.coords(x, y, xlabel, ylabel, log) :
’x’ and ’y’ lengths differ
You can change the type of plot from points to lines like so.
> plot(t1,p,type=’l’)
And you can add labels.
> plot(t1,p,type=’l’,xlab=’This is the horizontal label’,ylab=’This is the vertical label’,m
You can add more points (or lines) to a preexisting plot using the ’points’
(or ’lines’) command.
> plot(t1,p,type=’l’,xlab=’This is the horizontal label’,ylab=’This is the vertical label’,m
> p2<-c(6,10,13,14,12,17,18,19)
> points(t1,p2)
You can add points of a different color like this.
> plot(t1,p,type=’l’,xlab=’This is the horizontal label’,ylab=’This is the vertical label’,m
> points(t1,p2,col=’red’)
3
Saving
R can export graphics in many different formats, I’m going to recommend using
jpegs.
> dev.copy(jpeg,’Lab1plot1.jpg’)
> dev.off()
This just saved the current graph in the working directory. To figure out where
that is type.
> getwd()
You can now paste this into the word processor of your choice. The math
departments computers have Open-Office Writer. Find it in the launch menu
under applications and then office.
Once you’ve opened office, you can insert your figure by choosing insert¿picture-¿from file.
2
4
Functions
The commands ’plot’, ’points’,’seq’ etc are also called ’built in functions’. They
are built in because you don’t have to define them yourself. We define calculation
many times with only small changes. Some functions are simple. For example
if we wanted to define f (x) = x2 ex
>f<-function(x){x^2*exp(x)}
First comes the name of the function, then the keyword ‘function’ along with
the names of the inputs, which in this case is just x. The calculations are then
enclosed in curly braces.
If we want to plot the function f , we must first choose a plot range.
> xvals<-seq(-2,2,.01)
We can then generate a plot with this command.
> plot(xvals,f(xvals))
The expression f (xvals) plugs each number in xvals into f , giving us a list of
y-coordinates.
We could make more complicated function that have to do several steps of
calculations. This function gives the solution to the quadratic equation x2 +
bx + c = 0. In this case it is better to type this into your gedit window.
quadform<-function(b,c){
dscr <- b^2-4*c
x1 <- -b/2+sqrt(dscr)/2
x2 <- -b/2-sqrt(dscr)/2
c(x1,x2)
}
You then load your function into R by running the source command, which
runs all the lines in the named file. (Remember everytime you edit your .R file
you have to rerun the source command). After the source command has been
run, R will now recognize the function quadform and we can use it to solve the
equation x2 + 2x − 3 = 0.
>source(’lab1.R’)
>quadform(2,-3)
[1] 1 -3
5
Functions with For Loops
For loops are necessary if we want to perform the same task multiple times. For
example, suppose we want to a function to calculate the factorial of N. (The
factorial of N is N ∗ (N − 1) ∗ (N − 2) ∗ · · · 2 ∗ 1). We call the output of our
factorial function ‘soln’ and initialize it to 1. Then we multiply by all numbers
from 1 to N in a for loop. After the for loop the solution is returned.
3
factorial<-function(N){
soln<-1
for(i in seq(1,N)){
soln=soln*i
}
soln
}
> source(’lab1,R’)
> factorial(3)
[1] 6
> factorial(4)
[1] 24
> factorial(10)
[1] 3628800
6
If statements
If statements allow us to write more complicated functions. Consider g(x) = |x|.
This means that g(x) = x if x > 0 and g(x) = −x otherwise. We can program
this with an if statement.
absvalue<-function(x){
if(x>0){x}
else{-x}
}
> absvalue(3)
[1] 3
> absvalue(-3)
[1] 3
This is just an example as R already has a built in function ‘abs’, which does
the same thing.
7
Assignment for this week
1. Using an if statement, define the piecewise function f (x) = sin(π/x) for
x > −1 and f (x) = x + 1 otherwise.
2. Plot the function f between -10 and 10. Use a fine spacing and make sure
that the points are connected by a red line. On the same plot, plot green
points that are spaced apart by .5. Save this plot.
3. Use my factorial program to find the factorial of 6.
4
4. Use the quadform program to solve x2 + 17x − 182.
5. Now try to solve x2 + x + 1. What happens? Why is this?
6. Modify the quadform program so that it can solve problems of the form
ax2 + bx + c = 0. (Hint:use the quadratic formula as you learnt it.)
5
Download