1. SRL Junior Member
Join Date
Sep 2015
Posts
65
Mentioned
7 Post(s)
Quoted
20 Post(s)

## Python Assignment Help

Hey guys,

I am currently in a python computer programming course in which they simply go over the basics of python. This is the only programming course I have ever actually done and I am having troubles with it.
My troubles are not from the programming itself. The problem is the math that the prof is trying to get us to do with python.

A part of my current assignment is this:

This part of my assignment is the basis of the rest of it and I cant get it right...

This is my code:

python Code:
import mathDEG_PER_RAD = 180 / math.pi # Degrees per RadianRAD_PER_DEG = math.pi / 180 # Radians Per Degreeprint('\n      Angle [deg]            math.cosx         Series Sum      # of Terms  Equal')print('--------------------------------------------------------------------------------------------------')count = 0while count <= 6 :    count1 = 0    total = 0.0    term = 1    XX = (random.random())    XX_IN_DEG = (XX * DEG_PER_RAD)        while abs(term) > 1e-17:        count1 += 1        total += term        term = -term * (XX) / float(count1)        COSX = math.cos(XX)        EQ = abs(COSX - term) < 10e-14    print('%20.14f %20.14f %20.14f %5.0f       %5r' % (XX_IN_DEG, COSX, total, count1, EQ))    count += 1

The output is supposed to look as such:

All I need is to have the EQUALS column to be true for each number. The numbers do not need to be the same.

And if you are curious, here is the output I get:

Code:
      Angle [deg]            math.cosx         Series Sum      # of Terms  Equal
--------------------------------------------------------------------------------------------------
13.97040071176855     0.97042057486337     0.78362123793878    13       False
54.86275386652605     0.57553698318527     0.38383761567340    19       False
38.94104733351908     0.77779306913299     0.50679459773859    17       False
11.79910716583398     0.97887057528352     0.81388738612065    13       False
8.41996244681518     0.98922137607035     0.86333190483336    12       False
11.76549148742114     0.97899037655946     0.81436503743057    13       False
5.44590997307657     0.99548623788377     0.90932832191067    11       Fals
Any help on this matter would be much appreciated.
Last edited by Shield; 10-09-2015 at 05:41 PM.

2. @slacky;

3. SRL Junior Member
Join Date
May 2012
Posts
74
Mentioned
0 Post(s)
Quoted
38 Post(s)
Just read through it quickly, but it looks like you have forgotten to specify that the angles fed into the comparison should be between 90 and -90. Don't know if that will make a difference though

4. SRL Junior Member
Join Date
Sep 2015
Posts
65
Mentioned
7 Post(s)
Quoted
20 Post(s)
Originally Posted by Kratos
Just read through it quickly, but it looks like you have forgotten to specify that the angles fed into the comparison should be between 90 and -90. Don't know if that will make a difference though
Yea.. not too sure what the most efficient way of doing that was but I was going to use:

python Code:
XX = (random.random() + random.randint(-1, 0))

But I'm pretty sure there is a more efficient way of that...

5. SRL Junior Member
Join Date
May 2012
Posts
74
Mentioned
0 Post(s)
Quoted
38 Post(s)
Couldn't you just use
Code:
XX = (random.uniform(-1.57,1.57))

6. SRL Junior Member
Join Date
Sep 2015
Posts
65
Mentioned
7 Post(s)
Quoted
20 Post(s)
Originally Posted by Kratos
Couldn't you just use
python Code:
XX = (random.uniform(-1.57,1.57))
That should work! Thanks for that.

But now my main problem is still the returning false in the COS calculations... Any clue on how to fix that?

7. SRL Junior Member
Join Date
May 2012
Posts
74
Mentioned
0 Post(s)
Quoted
38 Post(s)
No clue frankly. will look into it though.

8. SRL Junior Member
Join Date
Sep 2015
Posts
65
Mentioned
7 Post(s)
Quoted
20 Post(s)
Originally Posted by Kratos
No clue frankly. will look into it though.
Thanks! For the past 2.5 hours I've been struggling with this. I have even been plugging the code into pythontutor.com to see where I am going wrong. I have found out that the first number in the series of sums is not 1 with the code I have written above. I have tried making the first number in the series equal to one but then I get a result that is totally outrageous. My frustration is going through the roof!

9. Why are you comparing the term to the COSX? You should compare it to total, right?
Edit: Also you should use math functions for converting radians<->degrees https://docs.python.org/2/library/ma...lar-conversion

Edit2: This works for me. How did you come up with your term calculation?
Code:
import math
import random

print('\n%-19s%-17s%-18s%-12s%-5s\n%s' %
('Angle [deg]', 'math.cos(x)', 'Series Sum', '# of Terms', 'Equal', '-' * 71))

loop_count = 0
while loop_count <= 6:
term_count = 0
sum_cosx = 0.0
term = 1.0
angle_deg = (random.randrange(-90, 90) + random.random())

while abs(term) > 1e-17:
term_count += 1
sum_cosx += term
term = math.pow(-1, term_count) * math.pow(angle_rad, 2 * term_count) / \
math.factorial(2 * term_count)

eq = abs(math_cosx - sum_cosx) < 1e-13
print('%-19.14f%-17.14f%-18.14f%-12d%-5r' %
(angle_deg, math_cosx, sum_cosx, term_count, eq))
loop_count += 1
Code:
Angle [deg]        math.cos(x)      Series Sum        # of Terms  Equal
-----------------------------------------------------------------------
85.56252753508009  0.07737110201137 0.07737110201137  11          True
23.54675389385704  0.91673438611638 0.91673438611638  8           True
7.10812104174803   0.99231440877814 0.99231440877814  6           True
44.49776024463146  0.71327784792729 0.71327784792729  9           True
-69.96679664107417 0.34256464519610 0.34256464519610  11          True
-52.42231609149914 0.60983652931192 0.60983652931192  10          True
-76.65633541282435 0.23079132098999 0.23079132098999  11          True
Last edited by tls; 10-09-2015 at 11:42 PM.

10. SRL Junior Member
Join Date
Sep 2015
Posts
65
Mentioned
7 Post(s)
Quoted
20 Post(s)
Originally Posted by tls
Why are you comparing the term to the COSX? You should compare it to total, right?
Edit: Also you should use math functions for converting radians<->degrees https://docs.python.org/2/library/ma...lar-conversion

Edit2: This works for me. How did you come up with your term calculation?
Code:
import math
import random

print('\n%-19s%-17s%-18s%-12s%-5s\n%s' %
('Angle [deg]', 'math.cos(x)', 'Series Sum', '# of Terms', 'Equal', '-' * 71))

loop_count = 0
while loop_count <= 6:
term_count = 0
sum_cosx = 0.0
term = 1.0
angle_deg = (random.randrange(-90, 90) + random.random())

while abs(term) > 1e-17:
term_count += 1
sum_cosx += term
term = math.pow(-1, term_count) * math.pow(angle_rad, 2 * term_count) / \
math.factorial(2 * term_count)

eq = abs(math_cosx - sum_cosx) < 1e-13
print('%-19.14f%-17.14f%-18.14f%-12d%-5r' %
(angle_deg, math_cosx, sum_cosx, term_count, eq))
loop_count += 1
Code:
Angle [deg]        math.cos(x)      Series Sum        # of Terms  Equal
-----------------------------------------------------------------------
85.56252753508009  0.07737110201137 0.07737110201137  11          True
23.54675389385704  0.91673438611638 0.91673438611638  8           True
7.10812104174803   0.99231440877814 0.99231440877814  6           True
44.49776024463146  0.71327784792729 0.71327784792729  9           True
-69.96679664107417 0.34256464519610 0.34256464519610  11          True
-52.42231609149914 0.60983652931192 0.60983652931192  10          True
-76.65633541282435 0.23079132098999 0.23079132098999  11          True
Dude! Thanks so much! That works so much better and makes more sense! I really appreciate it!

11. SRL Junior Member
Join Date
Sep 2015
Posts
65
Mentioned
7 Post(s)
Quoted
20 Post(s)
Alright... I am really bad with the complex mathematical side of programming so this stupid assignment is getting me really frustrated. Luckily this is the only assignment in this course that involved this much math. I've been working on this part for the past day or two and cannot seem to get it done properly. I have edited this code close to 100 different equations and none of them have worked. In this equation I was able to get the total correct but not the term.

Here is my problem:

This is what I've got for code:

python Code:
print('\nTERMS OF SERUES USING HIGH PRECISION')angle_rad = 40COSX = math.cos(angle_rad)print('\nAngle: 40 radians')print('cos of Angle:', COSX)print('Big power of 10: 1e+40 of type', type(1**40))print('\n%')      count = 1BIG10_SQ = (10**40)**2bigXsq = (angle_rad * 10**40)**2total = 10**40term = 10**40    while abs(term) > 3:    term = -term * bigXsq // (2 * count) // (2 * count - 1) // BIG10_SQ    total += term    print('\n%7.0f  Term: %60.0f' % ((count), term))    print('         Total:%60.0f' % (total))    count += 1print(COSX)

I am not worried about the format of the output, I am mainly doing my best to get the main equation correct to get the proper output in the picture with the given equation.

Any help would be greatly appreciated!
Last edited by Shield; 10-13-2015 at 02:22 AM. Reason: Updated code

12. SRL Junior Member
Join Date
Sep 2015
Posts
65
Mentioned
7 Post(s)
Quoted
20 Post(s)
I had posted the wrong code in the post above. I now updated it.
But once again all help is greatly appreciated.

So far, I get the correct output, the only problem is with my first term and total. I cant get them to be the same as the assignment sheet.
Last edited by Shield; 10-13-2015 at 02:08 AM.

13. You are trying to print integers as floats in that format string
%d instead of %f will solve it
Code:
while abs(term) > 3:
print('\n%7d  Term: %60d' % (count, term))
print('         Total:%60d' % (total))
term = -term * bigXsq // (2 * count) // (2 * count - 1) // BIG10_SQ
total += term
count += 1