1170:Lab2 August 28th, 2012 Goals For This Week In this second week we will get some more practice plotting functions. Hopefully, you paid attention to what you were doing as we’re going to add in a few twists. • Plot data points and curves on the same plot • Make our lives easier (in the long run) by defining our own functions 1 Plotting data As scientists, we usually want to compare real-world data with a simplified model. As a quick example consider the mean monthly temperature in Salt Lake City. Month Temperature(F) January 37 February 43 March 53 April 61 May 71 June 82 July 91 August 89 September 78 October 64 November 49 December 38 First let’s plot these data. You can call the variables anything you want; I try to make my names as descriptive as possible. > t_data_months<-seq(.5,11.5) > temp_data_F<-c(37,43,53,61,71,82,91,89,78,64,49,38) > plot(t_data_months,temp_data_F,xlab=’months’,ylab=’temp(F)’,main=’Temp in SLC’) The t vector is defined that way because we want to put the data point at the middle of the month. 2 Plotting a function on top of data Next I’m going to come up with a simple function that goes through these points. Let’s assume that is has the following form. T = Tavg − Tchangecos(2π(t − t0 )/12) The temperature range is 37 to 91 degrees so I’m going to guess that Tavg = (91 + 37)/2 = 64 and Tchange = (91 − 37)/2 = 27. This just leaves t0 unknown. 1 Now it’s time to introduce a new and very important command in R. The function command allows us to define our own functions for later use. We’ll learn how to use this function by example. > f<-function(t){64-27*cos(2*pi*t/12)} We can try entering in numbers > f(3) or lists of numbers > f(t_data_months) Next I’m going to try to put this line on top of the plotted points from before. As I’m plotting a continuous function I want to create some more t values to make the curve smoother. > t_model<-seq(0,12,.01) > lines(t_model,f(t_model)) This doesn’t look quite right, so it would be nice if we could make it a little better. Recall that we left t0 unknown. We can try varying this parameter to make the curve match the points better. First I’ll guess that t0 = 1 and then redo the lines command. > lines(t_model,f(t_model-1)) Now you should see the points along with the two curves plotted on top of each other. Play around a little with t0 to get the best value. You can clear the picture and start over by re-entering the plot command. Once you have a good value for t0 , save the picture. The picture should have the original data points and a single smooth curve going through it that represents your best fit to the data. 3 Changing Units Note in my examples below I’m using t0 = 1, you should use whatever t0 you decided was best. Now that we have the function defined, let’s try some changes of units. Currently, your function takes an input in months and gives an output in fahrenheit. Let’s suppose that instead, someone wanted a plot for the first 12 days of the year. We can reuse the same t values (in fact, we have to), but now their unit is days. We have to make the conversion from days to months before inputting them to the function. Now I’m going to add in a factor (1/30) which represents the approximate conversion from months to days. > plot(t_model,f(t_model/30-1),xlab=’days’,ylab=’temp(F)’,main=’Temp in SLC’,type=’l’) Note how multiplying by a number smaller than 1 resulted in a horizontal shrink. Save Your Graph Now, let’s imagine that we want to display the temperature in celsius. > plot(t_model,(f(t_model-1)-32)*5/9,xlab=’months’,ylab=’temp(C)’,main=’Temp in SLC’,type=’l’) Save this plot. 2 4 Assignment for this week 1. A mean monthly temperature in SLC (in F) along with a smooth function that approximates the data. (Should already be done) 2. Plot your smooth function from the last part for the 1st twelve days of the year. No need to include the data. (Should already be done) 3. Plot the smooth function for the entire year with months on the horizontal axis and temperature in celsius on the vertical. (Should already be done) 4. Plot the smooth function for 12 years with years on the horizontal and temperature in Kelvin on the vertical axis. Note that we convert celsius to Kelvin by adding 273.15. 5. The following table shows the population of Utah. Plot it with the year on the horizontal axis and population on the vertical axis. Year 1850 1860 1870 1880 1890 1900 1910 1920 1930 1940 1950 1960 1970 1980 1990 2000 2010 Population 11,380 40,273 86,336 143,963 210,779 276,749 373,351 449,396 507,847 550,310 688,862 890,627 1,059,273 1,461,037 1,722,850 2,223,169 2,763,885 Try to find an exponential function of the form P = A ∗ exp(r ∗ (t − 1850)) that fits this data. Save a plot that includes the original data and your smooth function. Hint: Population= g(A, r, t) = Aert . You can encode this into R like this. > g<-function(A,r,t){A*exp(r*(t-1850))} 3