471,338 Members | 1,031 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

find the first derivative of the equation

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

This is a very advanced topic, so I must ask: Are you teaching your self?
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.

This is a very advanced topic, so I must ask: Are you teaching your self?
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
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
thanks for your help.
Oct 10 '07 #12

Post your reply

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

Similar topics

4 posts views Thread by Sven Dzepina | last post: by
1 post views Thread by Russell Blau | last post: by
20 posts views Thread by Brian Kazian | last post: by
9 posts views Thread by Stud Muffin | last post: by
6 posts views Thread by Trev17 | last post: by
3 posts views Thread by jlynx23 | last post: by

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.