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

Written Homework 6

Problem 1

In homework 4, you used several forward Euler and RK2 (as well as RK4, but we won’t worry about that method here) to solve a system of differential equations with various time steps ∆t. You calculated the maximum error of each of these solutions and ended up with a table like this:

t

FE

RK2

25

0.065689380191595

0.001930489228223

26

0.031504547513018

0.000479445374726

27

0.015430315984651

0.000119406417934

28

0.007637638607823

0.000029790283695

29

0.003799621345140

0.000007439627252

210

0.001895033259672

0.000001858896444

We know that forward Euler is a first order method, and so these errors should be proportional to ∆t. Similarly, since RK2 is second order, the errors in the second column should be proportional to ∆t2.

(a) Use polyfit to find the best fit line for the forward Euler error.  (That is,  find a formula FE error = mt + b that best approximates the forward Euler errors. Calculate the RMS error for this best fit line. Repeat this process with a best fit quadratic and a best fit cubic. Present the resulting RMS errors in the following table:

order

RMS

1

 

2

 

3

 

In addition, plot each of the best fit curves alongside the forward Euler errors. (Put all three curves, as well as the data points, on a single graph.)

Explain how you can tell from the RMS errors and/or the plots that forward Euler is first order. (Your explanation should consist of a few sentences, but doesn’t need any formulas or derivations.)

(b) Now make new variables x = ln(∆t) and y = ln(FE error). Find the best fit line y = mx + b. What is the slope of this line? How does this compare to the order of Forward Euler?

(c) Use polyfit to find the best fit line for the RK2 error. (That is, find a formula RK2 error = mt + b that best approximates the RK2 errors. Calculate the RMS error for this best fit line. Repeat this process with a best fit quadratic and a best fit cubic. Present the resulting RMS errors in the following table:

order

RMS

1

 

2

 

3

 

In addition, plot each of the best fit curves alongside the RK2 errors. (Put all three curves, as well as the data points, on a single graph.)

Explain how you can tell from the RMS errors and/or the plots that RK2 is second order. (Your explanation should consist of a few sentences, but doesn’t need any formulas or derivations.)

(d) Now make new variables x = ln(∆t) and y = ln(RK2 error). Find the best fit line y = mx + b. What is the slope of this line? How does this compare to the order of RK2?

Problem 2

The file data3.csv contains data that we used as an example of curve-fitting in class.

You can load this data with the code

data = readmatrix(’data3.csv’); x = data(1, :);

y  =  data(2, :);

The data was actually produced using the formula

y = e0.1x sin(4x) + ϵ

where ϵ is a small random number (different for each value of x). In this problem, you will explore what happens if we try to fit different polynomials to this data.

(a) Find the best fit polynomials of order 1, 2, 3, 4, . . . , 25 for this data. For each of these best fit polynomials, calculate the RMS error. Make a plot of RMS error versus order. (That is, have the order 1, . . . 25 on the x-axis and RMS error on the y-axis.

(b) Based on your plot from part (a), decide which order polynomial (between 1 and

25) will best describe the data. Plot your chosen best fit polynomial alongside the original data. Does this look like a good fit? Does it exhibit any polynomial wiggle? Do you think that you have overfit the data?

(c) Make a plot with the original data along with one more point at x = 15, y = e0.1·15 sin(4 15). On the same graph, plot the polynomial you found in part (b). (Use the polynomial you already calculated! Do not calculate a new best fit curve with the extra data point.) Did your best fit polynomial do a good job predicting the y-value of this new data point?

(d) Find the best fit polynomial of order 50 and plot it alongside the original data. What do you think went wrong?