473,387 Members | 3,821 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,387 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 3837
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

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

Similar topics

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
w33nie
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.