MATLAB Find line best fit with polyfit function from the problem
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 \( \Delta t \). You calculated the maximum error of each of these solutions and ended up with a table like this: We know that forward Euler is a first order method, and so these errors should be proportional to \( \Delta t \). Similarly, since RK2 is second order, the errors in the second column should be proportional to \( \Delta t^{2} \). (a) Use polyfit to find the best fit line for the forward Euler error. (That is, find a formula \( \mathrm{FE} \) error \( =m \Delta t+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: 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 (\Delta t) \) and \( y=\ln (\mathrm{FE} \) error). Find the best fit line \( y=m x+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 \( =m \Delta t+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: 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 (\Delta t) \) and \( y=\ln (\mathrm{RK} 2 \) error \( ) \). Find the best fit line \( y=m x+b \). What is the slope of this line? How does this compare to the order of \( \mathrm{RK} 2 ? \)