View Full Version : Python Assignment Help
Shield
10-09-2015, 05:37 PM
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:
http://i.imgur.com/lIe85DY.png
This part of my assignment is the basis of the rest of it and I cant get it right...
This is my code:
import math
DEG_PER_RAD = 180 / math.pi # Degrees per Radian
RAD_PER_DEG = math.pi / 180 # Radians Per Degree
print('\n Angle [deg] math.cosx Series Sum # of Terms Equal')
print('--------------------------------------------------------------------------------------------------')
count = 0
while 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:
http://i.imgur.com/3Xff35A.png
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:
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.
Incurable
10-09-2015, 05:52 PM
slacky;
Kratos
10-09-2015, 06:24 PM
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
Shield
10-09-2015, 06:45 PM
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:
XX = (random.random() + random.randint(-1, 0))
But I'm pretty sure there is a more efficient way of that...
Kratos
10-09-2015, 06:51 PM
Couldn't you just use
XX = (random.uniform(-1.57,1.57))
Shield
10-09-2015, 06:54 PM
Couldn't you just use 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?
Kratos
10-09-2015, 07:01 PM
No clue frankly. will look into it though.
Shield
10-09-2015, 07:11 PM
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!
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/math.html#angular-conversion
Edit2: This works for me. How did you come up with your term calculation?
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())
angle_rad = math.radians(angle_deg)
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)
math_cosx = math.cos(angle_rad)
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
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
Shield
10-10-2015, 03:06 AM
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/math.html#angular-conversion
Edit2: This works for me. How did you come up with your term calculation?
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())
angle_rad = math.radians(angle_deg)
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)
math_cosx = math.cos(angle_rad)
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
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!
Shield
10-11-2015, 11:06 PM
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:
http://i.imgur.com/qCLrz2r.png
http://i.imgur.com/jfpdRJp.png
This is what I've got for code:
print('\nTERMS OF SERUES USING HIGH PRECISION')
angle_rad = 40
COSX = 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 = 1
BIG10_SQ = (10**40)**2
bigXsq = (angle_rad * 10**40)**2
total = 10**40
term = 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 += 1
print(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!
Shield
10-13-2015, 02:01 AM
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.
You are trying to print integers as floats in that format string
%d instead of %f will solve it
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
Powered by vBulletin® Version 4.2.1 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.