473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

python program to solve calculus problem

bferguson94
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.








Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     highest= -9999999
  3.     x = 1.0
  4.     f=0
  5.     while x <= 3:
  6.         y = f 
  7.         if y > highest:
  8.             x = x + f
  9.     y=calc(f)
  10.     print y
  11.     raw.input("press enter to go on")
  12.  
  13. def calc(f):
  14.     f = (2(x**3))-(8(x**2))+ x + 16
  15.     return f
  16. main()
  17.  
Mar 5 '10 #1

✓ answered by Glenton

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:

Expand|Select|Wrap|Line Numbers
  1. step=0.00001
  2. x=-1
  3. xn=3
  4. highest=calc(x)
  5.  
  6. while x<=xn:
  7.     f=calc(x)
  8.     if f>highest:
  9.         highest=f
  10.     x+=step
  11. print highest
  12.  

5 4381
Glenton
391 Expert 256MB
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:
Expand|Select|Wrap|Line Numbers
  1. from numpy import *
  2.  
  3.  
  4. def calc(x):
  5.     """Returns f(x), for x a number or an array"""
  6.     return 2*x**3-8*x**2+x+16
  7.  
  8. def main():
  9.  
  10.     #create array of x from -1 to 3 with 100 elements
  11.     x=linspace(-1,3,1000) 
  12.     f=calc(x)
  13.     print max(f)
  14.  
  15. 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!)
Mar 8 '10 #2
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!
Mar 8 '10 #3
Glenton
391 Expert 256MB
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.
Mar 8 '10 #4
Glenton
391 Expert 256MB
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:

Expand|Select|Wrap|Line Numbers
  1. step=0.00001
  2. x=-1
  3. xn=3
  4. highest=calc(x)
  5.  
  6. while x<=xn:
  7.     f=calc(x)
  8.     if f>highest:
  9.         highest=f
  10.     x+=step
  11. print highest
  12.  
Mar 9 '10 #5
thanks! vary helpful!!
Mar 12 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any...
75
by: David MacQuigg | last post by:
Seems like we need a simple way to extend Python syntax that doesn't break existing syntax or clash with any other syntax in Python, is easy to type, easy to read, and is clearly distinct from the...
89
by: Radioactive Man | last post by:
In python 2.3 (IDLE 1.0.3) running under windows 95, I get the following types of errors whenever I do simple arithmetic: 1st example: >>> 12.10 + 8.30 20.399999999999999 >>> 1.1 - 0.2...
76
by: Nick Coghlan | last post by:
GvR has commented that he want to get rid of the lambda keyword for Python 3.0. Getting rid of lambda seems like a worthy goal, but I'd prefer to see it dropped in favour of a different syntax,...
68
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
53
by: Michael Tobis | last post by:
Someone asked me to write a brief essay regarding the value-add proposition for Python in the Fortran community. Slightly modified to remove a few climatology-related specifics, here it is. I...
852
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
29
by: jmDesktop | last post by:
For students 9th - 12th grade, with at least Algebra I. Do you think Python is a good first programming language for someone with zero programming experience? Using Linux and Python for first...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.