Sometimes when I did convergence test for my numerical solver, I can't get the exact order of convergence even I am sure that my code is correct. I would like to ask if anyone else has experienced this and knows what the cause is?
I put a very simple example here about the Euler Method. I want to approximate $x$ with $$ x'=\sqrt{x}+x $$ And I use the Euler Method such that $$ x(t_{0}+\Delta t) = x(t_{0})+x'(t_{0})\Delta t $$
I also attach my python code here:
delta_t_list = [0.001, 0.005, 0.01, 0.02]
error_list = []
for delta_t in delta_t_list:
x_list = [1]
max_iter_times = int(3/delta_t)
t = np.linspace(0, 3, max_iter_times+1)
x= (2*np.e**(t/2)-1)**2
for i in range(max_iter_times):
x0 = x_list[-1]
x1 = x0+(np.sqrt(x0)+x0)*delta_t
x_list.append(x1)
error = np.linalg.norm(x-x_list, ord=2)
error_list.append(error)
plt.plot(np.log(delta_t_list), np.log(error_list))
linregress(np.log(delta_t_list), np.log(error_list))

The norm you compute is just the Euclidean norm of the vector. As the sample number increases with decreasing step size, the norm increases with the square root of the length. You have to change to
You might also need to account and correct for the possibility that the step size of the
tarray is notdelta_tdue to floating point errors in computing3/delta_t. The safe way would be to set eitheror to hit the end point correctly