Weierstrass Function in Python-List index out of range error -
i trying code , plot weierstrass function in python.
http://mathworld.wolfram.com/weierstrassfunction.html
f(x)=∑sin(πk^(a)x)/(πk^(a)) k=1 ∞
i'm little stuck because maintain running "list index out of range error" @ line yval = wer(a, (xl[i]), e)
i'm not sure i'm going wrong, or if thought process incorrect. help appreciated! thanks!
here code:
import math,pylab x0 = float(input('enter origin of interval: ')) #start of interval xf = float(input('enter endpoint of interval: ')) #end of interval = float(input('enter value a: ')) #x = float(input('enter value x: ')) e = float(input('enter desired error tolerance: ')) n = int(input('enter number of iterations: ')) def wer(a, x, e): # givin number, e error tolerance sum1 = 0 sum2 = 0 k = 1 while(true): #sine of pi times k times x on pi times k sum1 = math.sin((math.pi)*pow(k,a)*(x))/((math.pi)*(pow(k,a))) sum2 = sum1 + math.sin((math.pi)*pow((k+1),a)*(x))/((math.pi)*pow((k+1),a)) if (abs(sum2-sum1) < e): break else: k+=1 homecoming sum1 def append(x0, xf, n): xl = [] #list containing x values yl = [] #corresponding y values dx = (xf-x0)/n #length of each subinterval in range (0, (n+1)): xval = x0 + (i * dx) yval = wer(a, (xl[i]), e) #error here xl.append(xval) yl.append(yval) #print i,':',xl homecoming xl, yl append(x0, xf, n) pylab.plot(xl,yl)
at start of each iteration xl
doesn't have i-th element yet, since add together @ end of loop; should either perform xl.append
after calculating xval
, or utilize xval
instead of xl[i]
in yval
formula.
python function matplotlib indexoutofboundsexception
No comments:
Post a Comment