import numpy as np import matplotlib.pyplot as plt start = 1 # intitial value iterations = 10 # number of iterations print(start) graphmin = 0 graphmax = 4 def f(x): # model return x - (np.tan(x/4)-1)*(4*np.cos(x/4)**2) xv = np.linspace(0.0,10.0,2*iterations+1) # create array for points xvalue (initial values do not matter) yv = np.linspace(0.0,10.0,2*iterations+1) # create array for points yvalue (initial values do not matter) def plot_graphical(x0,n): x = x0 xv[0] = x0 yv[0] = 0 for i in range(0,n): #iterate xv[2*i+1] = x # first point is (x,f(x)) x = f(x) yv[2*i+1] = x xv[2*i+2] = x # second point is (f(x),f(x)) yv[2*i+2] = x print(x) # print sequence (optional) plt.plot(xv,yv) # connect up all these points blue plt.xlabel('x') plt.ylabel('f(x)') plot_graphical(start,iterations) xcon = np.arange(graphmin,graphmax, 0.01) plt.plot(xcon,xcon, 'g') # y = x plotted green ycon = f(xcon) plt.plot(xcon,ycon, 'r') # function plotted red plt.show()