Hello, time for an update I guess!
I hope I can get some last help from this topic, after that I'm hopefully done with the project.
I decided to do interpolation to find when y=0 the following way:
- When y<0, solve for one more iteration so we have 2 "grid points" below y=0.
- Take those 2 grid points and the 2 last grid points above y=0. Create an interpolation polynomial (of degree 3) between these points and interpolate ##y, \dot y, x, \dot x## as functions of ##t##.
- Solve using the bisection method for ##y_{interpolated}(t)=0##.
- Using the value obtained in (3), let's call it ##t_0##, plug it into all the interpolation polynomials (##y_{interpolated}(t_0), \dot y_{interpolated}(t_0), x_{interpolated}(t_0), \dot x_{interpolated}(t_0)##. ##\dot y## changes sign according to the assignment.
- The solutions obtained in (4) is set as being the last solution. All solutions below y=0 are scrapped, and Runge-Kutta solves for the next values using ##t_0## and the interpolated values as the last true solution
This does work, so I guess it's fine! However, I do have some new questions which also are (sigh) related to error calculation:
1. I am asked to have ##6-8## safe *digits*, aka. relative error which we have concluded in the past. With the interpolation around ##y=0## comes a new error, but it doesn't make sense to me how to calculate the relative error. When approximating the interpolation error, I compare my result with a fourth degree polynomial evalulated at the same point.
Here is an example for the two values of ##y_{interpolated}(t_0)## (see above for definition of ##t_0##)
Degree 3: -6.938893903907228e-18
Degree 4: 2.081668171172169e-17
So the absolute error in this case is ##\left| 6,938893903907228\cdot 10^{-18} - 2,081668171172169 \cdot 10^{-17} \right| \approx 2,8\cdot 10^{-17}##
But if I want to calculate the relative error, I take the absolute error divided by ##6,938893903907228\cdot 10^{-18}## which will turn out huge.
In some other cases, the value of the interpolation polynomial at degree 3 will turn out as ##0##, which is good because I want to find when ##y=0##. However, if I try to calculate the relative error in that case, I will get division by zero.
Since I use relative errors for everything else, I want to figure out how to calculate it for the interpolation polynomials.
2.
Bonus question The interpolation around ##y=0## includes a small difference between the ##t## values when a fine step size is being used. For example, to solve the assignment which asks for a speed, to get the wanted accuracy, I have to interpolate between values that differ quite little in magnitude: here is an example:
2.685156250000135e-01 2.685937500000135e-01 2.686718750000135e-01 2.687500000000135e
The method does provide the same result as with the old algorithm, but I'm still a little worried regarding the interpolation accuracy - let's say I use these datapoints to create a coefficient matrix for the interpolation polynomial. Will that matrix not have quite a bad condition number?
I guess if it works it works, but is there any way I can improve the condition number (it has only been briefly mentioned in my course)
I hope this is clear and you can follow.