So here's my problem: I have a for loop with a variable k running from 1 to 31. Inside the for loop there is a while loop that seemingly only runs for the very first k and no others.
from numpy import exp
a = 0.0
N = 1
x = 0.1
def f(t):
return exp(-t**2)
def int_trap(x,N):
h = (x-a)/N
s = 0.5*f(a) + 0.5*f(x)
for i in range(1,N):
s += f(a + i*h)
return h*s
new_value = 1.0
old_value = 0.0
for k in range(1,11):
x = k/10
while abs(new_value - old_value) > 10**-6:
old_value = new_value
N = N*2
new_value = int_trap(x,N)
print(N,'\t',x,'\t',abs(new_value - old_value))
print(x)
The print(x) at the end is there to confirm that that the code is running through the k's.
And here's the output:
2 0.1 0.900373598036
4 0.1 3.09486672713e-05
8 0.1 7.73536466929e-06
16 0.1 1.93372859864e-06
32 0.1 4.83425115119e-07
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
whileloop checksabs(new_value - old_value)against10**-6- the last time it prints you had4.83425115119e-07which is smaller than the10**-6so it won't run again.x = k/10printsuggests python3.