2

I've defined the following function which takes x and n as arguments, using Taylor series summation to approximate arctan. I also embedded a conditional statement inside so the function works for all x. import math

import math

for x in range(1,10,1):
   def arctan(x, n):
       arctang=0
       inv_x=1/x
       for i in range(n):
        sign=(-1)**i
        arctang = arctang + ((inv_x**(2.0*i+1))/(2.0*i+1))*sign

       if x>0:
        arc_tan=(math.pi/2)-arctang

       else:
           arc_tan=-(math.pi/2)-arctang

       return arc_tan


   print(arctan(x,100))

this code prints the iterations:

0.7878981009052581

1.1071487177940906

1.2490457723982544

1.3258176636680326

1.3734007669450157

1.4056476493802696

1.4288992721907325

1.446441332248135

1.460139105621001

however I want to be able to store the x values into an array, and these iterations into another so I can generate a graph out of the arrays, ie: something along the lines x=[1,2,3,4,5,6,7,8,9] y=[iterations as listed]

How should I go about doing this?

thanks!

3 Answers 3

3

Just initialize an empty array of xmap and ymap and then try to append in that. A sample code would look like:

xmap = []
xmap.append(str(x))

Similarly for y.

ymap = []
ymap.append(str(arctan(x,100)))

Don't forget to initialize the xmap and ymap outside of the loop. If you want me to modify the whole of your code then let me know.

Update Complete code in 1D array.

import math
xmap = []
ymap = []

for x in range(1,10,1):
   def arctan(x, n):
       arctang=0
       inv_x=1/x
       for i in range(n):
        sign=(-1)**i
        arctang = arctang + ((inv_x**(2.0*i+1))/(2.0*i+1))*sign

       if x>0:
        arc_tan=(math.pi/2)-arctang

       else:
           arc_tan=-(math.pi/2)-arctang

       return arc_tan
   xmap.append(str(x))
   ymap.append(str(arctan(x,100)))

x = xmap
y = ymap
print "x= ",x
print "y= ",y
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply! :) I've tried the following code:xmap = [] xmap.append(str(x)) ymap = [] ymap.append(str(arctan(x,100))) print(xmap, ymap), but it only returns a 1-d array....please could you modify my code for me if it's no trouble?
Yeah sure! I can modify the code. But can you please tell me what is your expected output. Because from your question what I can understand is that it is in 1D array.
thanks prashant i think that's done the trick! I just changed the data type to float as wanna output 1d arrays of x and y so can plot them against eachother.
0

Don't redefine the function in every iteration. This way you will be able to use list comprehension in order to create the "iterations" list.

import math

def arctan(x, n):
       arctang = 0
       inv_x = 1/x
       for i in range(n):
        sign=(-1) ** i
        arctang = arctang + ((inv_x ** (2.0 * i + 1)) / (2.0 * i+1)) * sign

       if x>0:
        arc_tan = (math.pi / 2) - arctang

       else:
           arc_tan = -(math.pi / 2) - arctang

       return arc_tan

iterations = [arctan(x, 100) for x in range(1, 10, 1)]

You can change the comprehension to also store the value of x, then each element of the iterations list will be a tuple:

iterations = [(x, arctan(x, 100)) for x in range(1, 10, 1)]

By the way, numpy already has an arctan function: numpy.arctan.

Comments

0

Why not vectorize arctan() itself?

def varctan(x, n):
    inv_x = 1/x

    signs = numpy.empty((len(x), n))
    signs[:, ::2] = 1
    signs[:, 1::2] = -1

    exprange = 2.0 * numpy.arange(n)[None, :] + 1
    signs *= (inv_x[:, None] ** exprange) / (exprange)
    arctang = numpy.sum(signs, axis=1)

    arctang *= -1
    arctang[x > 0] += numpy.pi / 2
    arctang[~x > 0] -= numpy.pi / 2

    return arctang

varctan(numpy.arange(1, 10), 100)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.