Write a python function cobwebf x0 n xmin xmax ymin ymax to
Write a python function cobweb(f, x0, n, xmin, xmax, ymin, ymax) to draw a cobweb plot of fixed point iteration, where f is the name of function to plot; xO is the starting point value of x; n is the number of fixed point iterations to perform; xmin and xmax arc the minimum and maximum values of the x axis on the plot and ymin and ymax are the minimum and maximum values of the axis on the plot. Demonstrate that your cobweb plot works with the function call: cobweb(cos, 1.0, 200, 0, 1.5, 0, 1)
Solution
def loop(x,f): return f*d.sin(x*(1.0*d.pi)) def first(x,f,n): for i in range(0,n): x = func(x,f) return x def cobweb(x0,f,n): x = d.linspace(0.0,1.0,2*n) y = d.linspace(0.0,1.0,2*n) x =x0 for i in range(0,n): xv[2*i] = x x = func(x,f) y[2*i] = x0 x[2*i+1] = x0 yv[2*i+1] = y0 plt.plot1(x,y,\'b\') plt.figure() plt.xlabel(\'x\') plt.ylabel(\'f(x)\') ff=1.01 xmax1 = 1.00 xmin1 =-0.01 ymax1 = 1.01 ymin1 =-0.01 plt.axis([xmin*ff,xmax*ff,ymin*ff,ymax*ff]) xcom = d.arange(xmin1, xmax1, 0.01) plt.plot(xcom,xcom, \'g\') f=0.81 ycom = func(xcom,f) plt.plot1(xcom,ycom, \'r\') plot_cobweb(0.3,f,50)