472,951 Members | 2,080 Online

# find the first derivative of the equation 10
so I've just begun learning python for a school course. I have to write a simple program that finds the first derivative of the equation
Y= Ax^4 + Bx^3 + Cx^2 + Dx + E
so far i can have the user input the variables, but one of the requirements is to have the program show the orignial formula with the inputed variables. because x is not defined, i keep coming up with an error message. The problem is that i need to have x undefined to show the user the original equation.
for example,
A = 2
B = 2
C = 2
D = 2
E = 2
i need the program to output

Y= 2x^4 + 2x^3 + 2x^2 + 2x + 2
How can I display this without defining x or having an error message?
Oct 7 '07 #1
11 3813 bartonc
6,596 Expert 4TB
Here's a neat trick (slightly advanced, though).
Expand|Select|Wrap|Line Numbers
1. Formula = "Y= Ax^4 + Bx^3 + Cx^2 + Dx + E"
2.
3. A = 2
4. B = 2
5. C = 2
6. D = 2
7. E = 2
8.
9. vars = ('A', 'B', 'C', 'D', 'E')
10.
11. result = Formula  # Copy isn't really needed, but is a good habit
12.
13. for var in vars:
14.     result = result.replace(var, str(eval(var)))  # eval() is the advanced trick #
15.
16.
17. print Formula
18. print result
Y= Ax^4 + Bx^3 + Cx^2 + Dx + E
Y= 2x^4 + 2x^3 + 2x^2 + 2x + 2
Oct 7 '07 #2
shanazzle
10 Here's a neat trick (slightly advanced, though).
Expand|Select|Wrap|Line Numbers
1. Formula = "Y= Ax^4 + Bx^3 + Cx^2 + Dx + E"
2.
3. A = 2
4. B = 2
5. C = 2
6. D = 2
7. E = 2
8.
9. vars = ('A', 'B', 'C', 'D', 'E')
10.
11. result = Formula  # Copy isn't really needed, but is a good habit
12.
13. for var in vars:
14.     result = result.replace(var, str(eval(var)))  # eval() is the advanced trick #
15.
16.
17. print Formula
18. print result
Y= Ax^4 + Bx^3 + Cx^2 + Dx + E
Y= 2x^4 + 2x^3 + 2x^2 + 2x + 2
Wow thanks, that really worked well. I understand most of this code, but i dont understand what this means.
Expand|Select|Wrap|Line Numbers
1. for var in vars:
2.     result = result.replace(var, str(eval(var)))
Oct 7 '07 #3
bartonc
6,596 Expert 4TB
Wow thanks, that really worked well. I understand most of this code, but i dont understand what this means.
Expand|Select|Wrap|Line Numbers
1. for var in vars:
2.     result = result.replace(var, str(eval(var)))
Hi shanazzle. I've added [code] tags to your post. Instructions for their use are on the right hand side of the page while you are posting.

Let me try to explain:
In Python, many objects are known as "iterable", meaning that you may get one element after another. To do that we use the in operator. The for operator signifies a loop. The vars variable that I defined is a tuple (an object that implements iteration very efficiently). So, for each item (or element) of vars, do the following:
Call the replace() method of the string object (result) with two arguments.
The first argument is simply the "A", etc pulled from the vars tuple by the in operator.
Next use the built-in function, eval() to ask the Python interpreter for the value of the variable A. The result of that is wrapped in the built-in str() function which converts objects to their string representation.

You may think if object oriented programming in very human language term this way:
The for/in combination says: "Hey, mister tuple, each time I ask, please give me your next available item until there are no more to give (the var variable)".
Inside the loop, rely on a string's knowledge of itself: "Mister string, replace the text matched by the first argument with the contents of the second argument".

I hope that I have not just totally confused you.
Welcome to the Python Forum on TSDN,
Barton
Oct 8 '07 #4
shanazzle
10 Alright, thanks that makes sense, but then how do i turn the variables back into floating numbers so that i can multiply them, for example, when the derivative formula is
Y= 4*Ax^3 + 3*Bx^2 + 2*Cx + D
A = 2
B = 2
C = 2
D = 2
the result would be
4*2x^3 instead of the desired 8x^3 ??
Oct 8 '07 #5
bartonc
6,596 Expert 4TB
Alright, thanks that makes sense, but then how do i turn the variables back into floating numbers so that i can multiply them, for example, when the derivative formula is
Y= 4*Ax^3 + 3*Bx^2 + 2*Cx + D
A = 2
B = 2
C = 2
D = 2
the result would be
4*2x^3 instead of the desired 8x^3 ??
That, my friend is a whole other ball of wax.
The task here is known as "parsing". You must be able to pick out (or parse) the various parts of the equation, finding which are numbers and which are operators and apply them accordingly.

I ask because I can't imagine an instructor throwing you into such a deep topic without much preparatory work.
Oct 8 '07 #6
bartonc
6,596 Expert 4TB
That, my friend is a whole other ball of wax.
The task here is known as "parsing". You must be able to pick out (or parse) the various parts of the equation, finding which are numbers and which are operators and apply them accordingly.

I ask because I can't imagine an instructor throwing you into such a deep topic without much preparatory work.
OK, I take most of that back. Using another trick (the built-in exec() function) it is possible to have the interpreter parse the formula for you:
Expand|Select|Wrap|Line Numbers
1. Formula = "Y= Ax^4 + Bx^3 + Cx^2 + Dx + E"
2.
3. A = 2
4. B = 2
5. C = 2
6. D = 2
7. E = 2
8.
9. vars = ('A', 'B', 'C', 'D', 'E')
10.
11. result = Formula  # Copy isn't really needed, but is a good habit
12. x = 2
13.
14. for var in vars:
15.     result = result.replace("%sx" %var, "%s*%s" %(str(eval(var)), str(x)))
16.
17. print Formula
18. print result
19. exec(result)
20. print Y
I've added a bunch of "string formating" to the original. Or, more simply:
Expand|Select|Wrap|Line Numbers
1. x = 2
2.
3. for var in vars:
4.     result = result.replace("x", "*%s" %str(x))
You'll need to be aware of operator precedence and group with parentheses accordingly.

Both forms return the same end result. Here's the latter versions output:
Y= Ax^4 + Bx^3 + Cx^2 + Dx + E
Y= A*2^4 + B*2^3 + C*2^2 + D*2 + E
3
Oct 8 '07 #7
bartonc
6,596 Expert 4TB
OK, I take most of that back. Using another trick (the built-in) exec() function it is possible to have the interpreter parse the formula for you:
Expand|Select|Wrap|Line Numbers
1. Formula = "Y= Ax^4 + Bx^3 + Cx^2 + Dx + E"
2.
3. A = 2
4. B = 2
5. C = 2
6. D = 2
7. E = 2
8.
9. vars = ('A', 'B', 'C', 'D', 'E')
10.
11. result = Formula  # Copy isn't really needed, but is a good habit
12. x = 2
13.
14. for var in vars:
15.     result = result.replace("%sx" %var, "%s*%s" %(str(eval(var)), str(x)))
16.
17. print Formula
18. print result
19. exec(result)
20. print Y
I've added a bunch of "string formating" to the original. Or, more simply:
Expand|Select|Wrap|Line Numbers
1. x = 2
2.
3. for var in vars:
4.     result = result.replace("x", "*%s" %str(x))
You'll need to be aware of operator precedence and group with parentheses accordingly.

Both forms return the same end result. Here's the latter versions output:
Y= Ax^4 + Bx^3 + Cx^2 + Dx + E
Y= A*2^4 + B*2^3 + C*2^2 + D*2 + E
3
Here's the corrected version and output:
Expand|Select|Wrap|Line Numbers
1. for var in vars:
2.     result = result.replace("x", "*%s" %str(x))
3.     result = result.replace("^", "**")
Y= ((Ax)^4) + ((Bx)^3) + ((Cx)^2) + (Dx) + E
Y= ((A*2)**4) + ((B*2)**3) + ((C*2)**2) + (D*2) + E
342
Oct 8 '07 #8
bartonc
6,596 Expert 4TB
Here's the corrected version and output:
Expand|Select|Wrap|Line Numbers
1. for var in vars:
2.     result = result.replace("x", "*%s" %str(x))
3.     result = result.replace("^", "**")
Y= ((Ax)^4) + ((Bx)^3) + ((Cx)^2) + (Dx) + E
Y= ((A*2)**4) + ((B*2)**3) + ((C*2)**2) + (D*2) + E
342
Here is the simplest yet:
Expand|Select|Wrap|Line Numbers
1. Formula = "Y= ((Ax)^4) + ((Bx)^3) + ((Cx)^2) + (Dx) + E"
2.
3. A = 2
4. B = 2
5. C = 2
6. D = 2
7. E = 2
8.
9. x = 2
10.
11. result = Formula  # Copy isn't really needed, but is a good habit
12.
13. result = result.replace("x", "*x")
14. result = result.replace("^", "**")
15.
16.
17. print Formula
18. print result
19. exec(result)
20. print Y
21.
22.
23. Y= ((Ax)^4) + ((Bx)^3) + ((Cx)^2) + (Dx) + E
24. Y= ((A*x)**4) + ((B*x)**3) + ((C*x)**2) + (D*x) + E
25. 342
26.
Oct 8 '07 #9
shanazzle
10 Here is the simplest yet:
Expand|Select|Wrap|Line Numbers
1. Formula = "Y= ((Ax)^4) + ((Bx)^3) + ((Cx)^2) + (Dx) + E"
2.
3. A = 2
4. B = 2
5. C = 2
6. D = 2
7. E = 2
8.
9. x = 2
10.
11. result = Formula  # Copy isn't really needed, but is a good habit
12.
13. result = result.replace("x", "*x")
14. result = result.replace("^", "**")
15.
16.
17. print Formula
18. print result
19. exec(result)
20. print Y
21.
22.
23. Y= ((Ax)^4) + ((Bx)^3) + ((Cx)^2) + (Dx) + E
24. Y= ((A*x)**4) + ((B*x)**3) + ((C*x)**2) + (D*x) + E
25. 342
26.

That makes sense, but see thats just defining x as a number. what I need to do is multiply the inputted variable by a number, ex 4*Ax^3, and say you input A = 4, what it needs to do is recognize 4*4x^3 equals 16x^3
Oct 8 '07 #10
bartonc
6,596 Expert 4TB
That makes sense, but see thats just defining x as a number. what I need to do is multiply the inputted variable by a number, ex 4*Ax^3, and say you input A = 4, what it needs to do is recognize 4*4x^3 equals 16x^3
Go ahead and post some of your work here. You should have enough to have made an honest attempt at it by now.

Thanks.
Oct 9 '07 #11
shanazzle
10 4 by: Sven Dzepina | last post by: Hello people =) Has somebody a nice script, which can solve equations ? It would be super, if someone has an idea where I can get such a script / code in php. Thanks. Gretting! 1 by: Russell Blau | last post by: This is not really a Python question, but it does relate to a Python program I'm working on, so this seems like as good a place as any to ask for suggestions ... I'm working on a GUI application... 20 by: Brian Kazian | last post by: Here's my problem, and hopefully someone can help me figure out if there is a good way to do this. I am writing a program that allows the user to enter an equation in a text field using... 9 by: Stud Muffin | last post by: Hey Basically, I'm trying to take objects created in microsoft word using equation editor (for creating clean looking math/physics equations) and putting them into some sort of webpage format.... 5 by: w33nie | last post by: My table is pretty well complete, but I would prefer it if the value for Points could be turned into a mathematical equation, and this equation would use the data from the other fields in the table... 6 by: Trev17 | last post by: Hello, I am new to C++ and i have tried for several hours to make a program my teacher has given me as a lab. Here is the Lab question: the roots of the quadratic equation ax^2 + bx + c = 0, a... 3 by: jlynx23 | last post by: gud am...i am currently working with a project of mine which is about derivative...and i am having some problem with the printing of the derivative... here is the block of code i used: /* print... 1 by: madman228 | last post by: Hi guys I have run in to a littl bit of trouble. I am writing a class called polynomial in which i need a derivative method I have everything, just dont know how to start the derivative method. Any... 10 by: Constantine AI | last post by: Hi i am having a little problem with an equation function that was created from all your help previously. The function works fine itself but with a small glitch within it. Here is the function... 0 by: lllomh | last post by: Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{ 2 by: DJRhino | last post by: Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing... 2 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central... 2 by: giovanniandrean | last post by: The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions... 4 by: NeoPa | last post by: Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :... 3 by: NeoPa | last post by: Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all... 1 by: Teri B | last post by: Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and... 0 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on... 0 by: isladogs | last post by: The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...