print('\n Find stable zero of f(x) = 1- (1+d)x^2 -e^{-x}\n') from numpy import exp dt = 1; T = 20; N = int(T/dt); m = N//20 for d in range(9): x = 1/(1+d) print(' Iteration x = x +dt*f(x) for dt=1, d =',d) for i in range(N): x = x + dt * (1 -(1+d)*x*x -exp(-x)) # x = x +dt*f(x) if(i%m == 0): print('i,t,x ',i,i*dt,x) print("And now Newton's method") for d in range(9): x = 1/(1+d) print(" Iteration x = x -f(x)/f'(x) for d =", d) for i in range(7): f = 1-(1+d)*x*x-exp(-x) # f(x) f1 = -2*(1+d)*x+exp(-x) # f'(x) x = x - f/f1 # x = x - f(x)/f'(x) print('n,x ',i+1,x)