Chapter 9 Multiple choice questions

Quiz Content

not completed
. Create the following variables and function:
a <<- 1:10
b <<- c(2.08, 2.75, 3.54, 4.78, 7.02, 3.65,
7.01, 9.59, 9.95, 8.43)
errorfit <- function(p){
y <- p[1]*a + p[2]
rms <- sqrt(sum((y-b)^2)/length(b))
return(rms)}
The variables are an increasing sequence, and the function fits a straight line (y = mx + c) to the data. Use the simplex algorithm to fit the function to the data using starting values of c(1,1). You might find it useful to plot the data and the fitted line to check it is working. The best fitting parameters are:

not completed
. What is the RMS error of the best fit from question 1?

not completed
. Now change the b variable to hold the following numbers:

b <<- c(9, 45, 8, 752,1003, 253, 589, 2301, 5440, 8277)

Also edit the first line of the errorfit function so that it returns an exponential function, as follows: y <- p[1]*a^p[2]

The best fitting exponent for the data is:

not completed
. Make a note of the RMS error returned by the fitting in question 3. Now fit the data again using the straight line model from question 1. How much worse is the RMS error with a straight line fit?

not completed
. The following values produce data that can be fit by a Gaussian:
a <<- 1:10
b <<- c(0.00, 0.01, 0.06, 0.15, 0.25, 0.26, 0.16, 0.06, 0.02, 0.01)
Use the following equation to fit these data:

y <- p[1]*exp(-((a-p[2])^2)/(2*p[3]^2))

Notice that the function requires three free parameters. You should plot the data and the fit to check the fit quality. The best fitting parameter for the width of the Gaussian is:

not completed
. If the output structure of the simplex algorithm is stored in a variable called sout, you can find the number of iterations the simplex went through by looking at sout$count. How many fewer iterations are required when starting the search from question 5 with the parameters c(1,5,1), compared with c(1,1,1)?

not completed
. The following data are approximately sinusoidal:
a <<- 1:10
b <<- c(1.85, 0.26, 0.82, 2.00, 0.59, 0.49, 2.08, 0.76, 0.32, 2.00) Use the following simplified sine wave equation to fit a sine function to the data:
y <- p[1]+sin(a*p[2]) You may need to experiment with different starting parameters to get a good fit – plot your data to check if the fit is reasonable. The best fitting parameters are:

not completed
. What is the increase in RMS error using the code in question 7 when a cosine function is used instead of a sine function?

not completed
. What type of model would you expect to best predict future observations?

not completed
. Which other function in the pracma package might be used for optimization?

Back to top