Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Coding Homework 6

Problem 1

Consider the data

x

0

1

2

3

4

5

6

7

8

9

y

0

2.9

4

4.5

4.9

5

4.8

4.6

3.9

3

(a) Find the best fit line for this data. Store the coefficients in a 1 2 row vector (in the same order as given by polyfit) named A1. Find the RMS error of this fit and store it in a variable named A2.

(b) Find the best fit quadratic for this data. Store the coefficients in a 1 3 row vector (in the same order as given by polyfit) named A3. Find the RMS error  of this fit and store it in a variable named A4.

(c) Find the best fit 9th degree polynomial for this data. Store the coefficients in a

1 10 row vector (in the same order as given by polyfit) named A5. Find the RMS error of this fit and store it in a variable named A6.

Problem 2

Himmelblau’s function is defined by

f (x, y) = (x2 + y 11)2 + (x + y2 7)2

This function has one local maximum and four identical local minima. It is often used as a test function for optimization algorithms because all five of these points can be found analytically (although their formulas are quite complicated).

(a) Use fminsearch to find one of the minima using an initial guess of x =   2 and  y = 3. Store your answer in a 2   1 column vector [x, y]T named A7. Find the f value of this minimum (i.e., plug the x and y you just found into f (x, y)) and store it in a variable named A8.


(b) Use fminsearch to find the maximum using an initial guess of x = y = 0. Store your answer in a 2 × 1 column vector named A9.

Problem 3

This problem uses the same data as the corresponding problem in the coding homework.

The file CO2_data.csv contains monthly averages of atmospheric CO2 (in parts per million) measured at the Mauna Loa observatory in Hawaii. (You can find this

data, along with much more information about the measurements, here.) You can load this data with the code

data = readmatrix(’CO2_data.csv’); t = data(1, :);

co2 = data(2, :);

(This code assumes that the file CO2_data.csv is in the same directory as your code. You do not need to upload the .csv file to gradescope, but you do need to include these lines of code in your script.)

This will create two variables: co2 contains atmospheric CO2 at each month and t contains time measured in years since the beginning of 1958. The first entry is from March, 1958 and the last entry is from September, 2022.

The CO2 concentration has risen steadily over the last 64 years, but also under- goes distinct seasonal oscillations:


(a) Find a best fit line y = mt + b for this data. Store the coefficients in a 1 2 row vector (in the same order as given by  polyfit) named A10.  Find the RMS error   of this fit and store it in a variable named A11.

(b) It looks like the data might be growing roughly exponentially. This means that we should try to find a best fit curve of the form

y = aert + b

Use fminsearch to find the values of a, r and b that minimize the RMS error between this curve and our data.  Use an initial guess of a = 30, r = 0.03 and   b = 300. Store your answers in a 1 3 row vector [a, r, b] named A12. Find the RMS error of this fit and store it in a variable named A13.

(c) You should find that the best fit exponential curve captures the overall trend of the data well, but that it does not do a good job of capturing seasonal oscillations.

To fix this, we can try to find a best fit curve of the form

f (t) = aert + b + A sin(B(t C))

Use fminsearch to find the values of a, r, b, A, B and C that minimize the RMS error between this curve and our data. Since this model is similar to the one from the last part, it seems likely that a, r and b will be similar to the best fit values we found previously. Because of this, we will use the exact values of a, b and r that you  calculated in A12 as initial guesses in this step. Use A = 5, B = 5 and C = 0.5 as initial guesses for the other variables. Store your answers in a 1 6 row vector [a, r, b, A, B, C] named A14. Find the RMS error of this fit and store it in a variable named A15.

Note: This last part is a fairly difficult optimization problem, so you will need to let fminsearch run for more steps than normal. You can do so with the code

options = optimset(’MaxFunEvals’, 100000); fminsearch(your_function, your_guess, options)

where your_function and your_guess are the function and guess that you would normally supply to fminsearch.