# R script for to make a plot of a function and demonstrate # calling procedures from separate files. # The code in this file calls the procedure that evaluates the function ... # ... then makes the plot. # http://www.math.nyu.edu/faculty/goodman/teaching/MathFin/index.html # Author: Jonathan Goodman # Parameters describing the function k = 30 # the frequency parameter in the function to be plotted source("PlotFunction.R") # Read in the function definition # Parameters describing the plot xMin = 1 # plot over the intervao [xMin,xMax] xMax = 4 nPts = 300 # the number of points to plot, uniformly spaced, including endpoints dx = (xMax - xMin)/(nPts-1) # The x-distance between plot points # Evaluate and record the function at the plot points xVals = numeric(nPts) fVals = numeric(nPts) for ( i in 1:nPts){ x = xMin + dx*(i-1) f = mod_wave( x, k) xVals[i] = x fVals[i] = f } # Make the plot, with all the extras that make it professional xLabel = sprintf("X, from %7.2f to %7.2f", xMin, xMax) # Create strings ... title = sprintf("A modulated wave") # ... to put parameters into ... subtitle = sprintf("k = %7.2f, with %6d points", k, nPts) # ... plot titles. # Make a popup plot on the screen plot( xVals, fVals, # the numbers xVals are the x values and fVals are the y values type = "n", # the curve is a "not" actually plotted, or plotted as invisible tck = 1, # the "tick length" is 1*the plot size, so the "ticks" turn into grid lines xlab = xLabel, # the string xLabel becomes xlab, which is writton below the x axis main = title, # written in bigger letters on top of the plot sub = subtitle) # the subtitle is below the x label lines( xVals, fVals, # This also plots xVals against fVals, but this time ... type = "l", # ... type "l" means a solid line lwd = 4) # line width = 4 means a wide line. lineX = numeric(2) # Draw a straight line over the tops of the waves lineX[1] = xMin # x coordinates of the endpoints of the line lineX[2] = xMax lineY = numeric(2) lineY[1] = xMin # y coordinates of the endpoints of the line lineY[2] = xMax lines( lineX, lineY, # Draw the line, with width 2, not so wide type = "l", lwd = 2) lineY[1] = -xMin # The same thing, a line under the bottoms of the waves lineY[2] = -xMax lines( lineX, lineY, type = "l", lwd = 2) # Put the same plot in a file mod_wave.pdf # The code is cut and pasted from above pdf("mod_wave.pdf") # Write the plot to a file called mod_wave.pdf plot( xVals, fVals, # the numbers xVals are the x values and fVals are the y values type = "n", # the curve is a "not" actually plotted, or plotted as invisible tck = 1, # the "tick length" is 1*the plot size, so the "ticks" turn into grid lines xlab = xLabel, # the string xLabel becomes xlab, which is writton below the x axis main = title, # written in bigger letters on top of the plot sub = subtitle) # the subtitle is below the x label lines( xVals, fVals, # This also plots xVals against fVals, but this time ... type = "l", # ... type "l" means a solid line lwd = 4) # line width = 4 means a wide line. lineX = numeric(2) # Draw a straight line over the tops of the waves lineX[1] = xMin # x coordinates of the endpoints of the line lineX[2] = xMax lineY = numeric(2) lineY[1] = xMin # y coordinates of the endpoints of the line lineY[2] = xMax lines( lineX, lineY, # Draw the line, with width 2, not so wide type = "l", lwd = 2) lineY[1] = -xMin # The same thing, a line under the bottoms of the waves lineY[2] = -xMax lines( lineX, lineY, type = "l", lwd = 2) dev.off() # close the file and go home