python program to solve calculus problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bferguson94
    New Member
    • Feb 2010
    • 11

    python program to solve calculus problem

    I was given the following assignment:

    f(x)=2x^3-8x^2+x+16

    "2x^3 was my attempt to say (2x)cubed and 8x^2 was my attempt to say (8x)squared", I apologize if that is confusing.
    1. create a python program that will fing f(x)

    2.find the axtreme in function 1 acoss the closed interval (1,-3)

    below was my solution, but doesnt seem to work.. could you please help me out.








    Code:
    def main():
        highest= -9999999
        x = 1.0
        f=0
        while x <= 3:
            y = f 
            if y > highest:
                x = x + f
        y=calc(f)
        print y
        raw.input("press enter to go on")
    
    def calc(f):
        f = (2(x**3))-(8(x**2))+ x + 16
        return f
    main()
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    Hi

    It's not clear to me why you would expect that code to work?! Can you tell us in words what your algorithm is?

    Currently it is (roughly) as follows:
    1. Set x=1, f=0, and highest=-999999
    2. While x is less than 3:
    a. set y=f
    b. if y>highest, then set x=x+f.

    I needn't go any further, cos I can't see it getting further:
    f will always be 0
    x will always be 1
    y will always be 0

    Other comments: numpy has a constant called inf (short for infinity), which would be used profitably. Highest=-inf would be a good start.

    An overly simplistic way of doing it would be roughly as follows:
    Code:
    from numpy import *
    
    
    def calc(x):
        """Returns f(x), for x a number or an array"""
        return 2*x**3-8*x**2+x+16
        
    def main():
        
        #create array of x from -1 to 3 with 100 elements
        x=linspace(-1,3,1000) 
        f=calc(x)
        print max(f)
    
    if __name__=="__main__":main()

    Hope that helps. If you want a more sophisticated mechanism, you'll have to tell us what algorithm you want to use!

    Good luck, and please post back if this is helpful (or if it isn't!)

    Comment

    • bferguson94
      New Member
      • Feb 2010
      • 11

      #3
      Sorry that my algorithm was unclear.. It came from notes that I had taken and from my inability to understand calculus. Below will be the set of instructions given to me by my professor.

      **************the instructions go as follows*************** ****


      In calculus, there are techniques to find relative extremes for a function. A relative extreme is the highest value or the lowest value of a function within a limited interval of inputs. Graphically, relative extrema are the highest and lowest y-coordinate values across a span of x-coordinate values.
      We can use a computer program to find very close approximations of the exact extrema values by actually running the function many, many times with many, many values of x. That is, we can run the function for x = 1 and see what value we get. Then run it again for x = 1.00001 and compare the result, noting the highest and lowest values obtained. Then run it again for x = 1.00002 and so on.

      f(x) = 2x3 - 8x2 + x + 16

      1. Create a Python statement that will produce f(x):

      2. Write a Python program that will find the extrema (both high and low) for the function in part 1 across the closed interval (-1, 3):

      I hope this helps, I wouldn't be asking you guys if I didn't need it. Thanks again!

      Comment

      • Glenton
        Recognized Expert Contributor
        • Nov 2008
        • 391

        #4
        That's more or less what my algorithm does, except it evaluates all the points simultaneously instead of going through them one at a time. And it doesn't do so many points. And it doesn't space them like that.

        Changing x=linspace(-1,3,400001) should do it.

        Comment

        • Glenton
          Recognized Expert Contributor
          • Nov 2008
          • 391

          #5
          Or use x=arange(-1,3,0.00001).

          Of course the other way to do this is to step through, one step at a time:

          Code:
          step=0.00001
          x=-1
          xn=3
          highest=calc(x)
          
          while x<=xn:
              f=calc(x)
              if f>highest:
                  highest=f
              x+=step
          print highest

          Comment

          • bferguson94
            New Member
            • Feb 2010
            • 11

            #6
            thanks! vary helpful!!

            Comment

            Working...