Results 1 to 13 of 13

Thread: Python Assignment Help

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

    Default 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 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:


    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. #2
    Join Date
    Aug 2014
    Location
    Australia
    Posts
    932
    Mentioned
    53 Post(s)
    Quoted
    495 Post(s)

    Default

    @slacky;



    New to scripting? Procedures & Functions for Beginners
    Do you use your computer at night? Just get f.lux

  3. #3
    Join Date
    May 2012
    Posts
    74
    Mentioned
    0 Post(s)
    Quoted
    38 Post(s)

    Default

    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. #4
    Join Date
    Sep 2015
    Posts
    65
    Mentioned
    7 Post(s)
    Quoted
    20 Post(s)

    Default

    Quote Originally Posted by Kratos View Post
    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. #5
    Join Date
    May 2012
    Posts
    74
    Mentioned
    0 Post(s)
    Quoted
    38 Post(s)

    Default

    Couldn't you just use
    Code:
    XX = (random.uniform(-1.57,1.57))

  6. #6
    Join Date
    Sep 2015
    Posts
    65
    Mentioned
    7 Post(s)
    Quoted
    20 Post(s)

    Default

    Quote Originally Posted by Kratos View Post
    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. #7
    Join Date
    May 2012
    Posts
    74
    Mentioned
    0 Post(s)
    Quoted
    38 Post(s)

    Default

    No clue frankly. will look into it though.

  8. #8
    Join Date
    Sep 2015
    Posts
    65
    Mentioned
    7 Post(s)
    Quoted
    20 Post(s)

    Default

    Quote Originally Posted by Kratos View Post
    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. #9
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,420
    Mentioned
    13 Post(s)
    Quoted
    241 Post(s)

    Default

    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())
        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
    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. #10
    Join Date
    Sep 2015
    Posts
    65
    Mentioned
    7 Post(s)
    Quoted
    20 Post(s)

    Default

    Quote Originally Posted by tls View Post
    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())
        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
    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. #11
    Join Date
    Sep 2015
    Posts
    65
    Mentioned
    7 Post(s)
    Quoted
    20 Post(s)

    Default

    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 = 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!
    Last edited by Shield; 10-13-2015 at 02:22 AM. Reason: Updated code

  12. #12
    Join Date
    Sep 2015
    Posts
    65
    Mentioned
    7 Post(s)
    Quoted
    20 Post(s)

    Default

    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. #13
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,420
    Mentioned
    13 Post(s)
    Quoted
    241 Post(s)

    Default

    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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •