MATLAB: Is there a major difference in the accuracy of ode45 for polynomials of degrees 3 and 4

accuracykuttaMATLABode45orderpolynomialrunge

The following test indicates that ode accuracy order is only 3 (i.e., the Runge Kutta approximation is exact for polynomials only of degree 3). The documentation says the algorithm is "Runge-Kutta (4,5) formula, the Dormand-Prince pair". Shouldn't this have higher accuracy order? 
figure, hold on
options = odeset('MaxStep',1);
for p = 1:6
[t,y] = ode45(@(t,f) 1-(p+1)*t^p,[0,1],0,options);
y_ = t-t.^(p+1);
plot(t,[y,y_],'+-');
disp([num2str(p),' ',num2str(max(abs(y-y_)))])
end
output:
1 3.3307e-16
2 2.2204e-16
3 7.2164e-16
4 0.00038174
5 0.0014764
6 0.00083657

Best Answer

When considering whether a given ODE solver can exactly integrate an ODE of a particular order, the answer has to do with the number of slope evaluations in each step of the algorithm. Euler's method evaluates the slope only once at the beginning of each interval, so it can only exactly integrate constant functions. A 4th order Runge-Kutta solver uses 4 evaluations, so it can exactly integrate cubic functions. A 5th order Runge-Kutta solver actually uses 6 terms, so it can be used for quartic and quintic functions.
Since "ode45" uses both 4th and 5th order methods to obtain error estimates in each step, these methods will have a growing error discrepancy when you go from cubic to quartic functions and beyond, as you have noticed. This is because the 4th order method will not be able to integrate functions of order higher than a cubic very accurately, and it will do progressively worse as the order increases.
in order to obtain a higher accuracy for polynomials of order 4 and above, reduce the "RelTol" and "AbsTol" properties using the "odeset" function.