Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I cant use any user defined functions, it has to be *exactly* like the constrain

ID: 3668737 • Letter: I

Question

I cant use any user defined functions, it has to be *exactly* like the constraints. Sorry I didn't specify that before

Write a Python program that uses a while loop to compute and print the cosine of an angle using the first 20 terms of the following series

cosx = 1 - ((x^2/2!)) + ((x^4/4!)) - ((x^6/6!))....

where x is in radians. Your program must input a value for x in degrees, convert it to radians and print the computed cosine of the given angle. You may use math.cos to check your answer, but do not use it in your solution!

Hint: Notice how the numerator and denominator change from iteration to iteration and apply a "rule" that will update the numerator and denominator at each iteration. Note that the numerator of each term is the numerator of the previous term multiplied by x 2 and also note the denominator of each term is the denominator of the previous term multiplied by the next two integers in the series. You should NOT re-compute the complete factorial at each step!

Constraints:

You must use while loops for all repetition in your solution to this problem, do not use for loops.

• Do not import or use any module functions to solve this problem

• Do not use the Python exponentiation operator (see Hint below)

• Do not construct or call any user-defined functions in your solution

Has to be in python

Note that the numerator of each term is the numerator of the previous term multiplied by x 2 and also note the denominator of each term is the denominator of the previous term multiplied by the next two integers in the series. You should NOT re-compute the complete factorial at each step!

Here is what I had, but it does not work, and I dont know how to fix it:

xdeg=float(input("Enter a value for x: "))
rad = 3.14 * (xdeg/180)
counter = 20
total = 1
num = rad
den = 2
while counter>0:
num = num * -1
total = total + (num/den)
num = num * (rad * rad)
den = den * (den+1) * (den +2)
counter = counter - 1
  
print (total)

Explanation / Answer

This is Working

-> Have not used any of the python library

-> hace written our own power function

def pow(base, exponent):
    if exponent == 0:
        return 1
    elif exponent < 0:
        return 1 / pow(base, -exponent)
    elif exponent % 2 == 0:
        half_pow = pow(base, exponent // 2)
        return half_pow * half_pow
    else:
        return base * pow(base, exponent - 1)
i=0
n=20  
x=input("Enter a value for x: ")
sum = 1
x = x * 3.14159 / 180 ;
t = 1  
for i in range(1,n+1):
      t = t * pow(-1,2 * i - 1) *x * x / (2 * i * (2 * i - 1))  
      sum = sum + t
print sum;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote