Finding the derivative of a function
807588Jul 4 2009 — edited Jul 4 2009Hey all, recently I decided to try to write a program to find the derivative of a function. No real point in it other than to just do it and learn.
Basicly I'm going to assume the user will type in the form:
5x^3+7x-x .......no spaces, etc
So for the first part I want to just return its derivative,
15x^2+7
Which really isnt the hardest thing....its just when the user starts to input more involved functions. Which is were my dilema is. Should I incorporate the actual derivative formula:
[http://mathworld.wolfram.com/images/equations/Derivative/NumberedEquation6.gif|http://mathworld.wolfram.com/images/equations/Derivative/NumberedEquation6.gif]in my program? That would essentially be the quickest way to find any derivative, but I'm not so sure how to approach that with the variables and all.
My other option was to just set up various methods/classes for the different derivative rules(product, quotient, etc) and ask the user which one they want to use. Which I figured would be tedious beyond belief, and kind of inconvinient for the user if they dont know which one to use(I want to assume the user has no idea about the basics of Calc and is simply messing around).
I guess what my main question is is once I accept the user's function(ie the one above), how would I incorporate that into the derivative formula with all the variables using Java, then output the derivative as listed above by putting variables back in after already taking them out from the input? I want to assume I dont know the answer to the derivative, and NEED to use the formula to get it.
My second issue would be if the user types:
(((x^3)/(3x*3))/((22x^2)-(2)))
how do I set my program to scan for the "(" 's and stuff, and remember where they were to be able so parse the x's and numbers out. I can to it for accepting the date 12/12/2012 into 12122012 then dividing that to int x=12, y=12, z=2012, but thats all set and wont ever change with more or less "useless" chars, or bigger ints. I would also assume that my program needs to be set up that when it scans the String and comes across "*", "/", etc should set them to the side to be able use them later for the math.
So its so many questions, I figured I'd ahve the best luck coming here than anywhere else.
Edited by: JimmyV88 on Jul 4, 2009 2:55 PM