So I am trying to calculate an error by using two step sizes, one twice the size of the other. I set up two temperature arrays which are being calculated using two different ste
First, looping over the same temperatures which are spaced by dt
but then doubling dt
makes no sense. It makes more sense to compare to the analytical solution as attempted in your previous post.
To print the value of the result at a certain temperature you may - under the condition that you know the value is actually inside the array - index the array by the condition.
E.g.
t = np.array([1,55,77])
x = np.array([1,2,3])
print x[t == 55]
In this case:
import numpy as np
r = 0.024 #cooling rate per minute
te_init = 90.0
te_surr = 17
def f(te):
return -r*(te - te_surr) # the derivative
dt_values = [0.05, 0.025, 0.01, 0.005]
for dt in dt_values:
t = np.arange(0,100,dt) #time
te = np.zeros(len(t)) #temperature array
te[0] = te_init
for i in range(1,len(t)): #eulers method for computing temperature
p1=f(te[i-1])
p2 =f(te[i-1]+dt*p1)
te[i] = te[i-1] + (p1 + p2)*(dt/2)
te_ana = te_surr - (te_surr - te_init)*np.exp(-r*t)
print dt,
print te[t==10] - te_ana[t==10]