Jump to content

Derivative Program


CanadaAotS

Recommended Posts

hey all

 

well just to start I use visual c++ with programming

 

I was just fooling around with a program (I also just recently learned all about derivatives in calculus).

 

I decided to make myself a lil' derivative calculating program, but I only know how to program c++ where your using the stupid dos-like box (forget what its called).

 

Anyways, so I got it to work, but its really not very good, you have to input each part of the beginning function individually (say 2x^3 you'd have to specify the 2 and the 3). Also the terms had to be inputed individually, and displaying the derivative afterwards was hell lol.

 

So just so non-calculus knowing ppl can help to the derivative of any function is:

 

[math]

y = ax^n

[/math]

[math]

y' = (a*n)x^{n-1}

[/math]

 

Where y' is the derivative.

 

So all these limitations are bad, but to add say product, quotient and chain rules to with derivatives makes it even stupider lol

 

any help would be appreciated!

 

I have my source file attached...

derivative.txt

Link to comment
Share on other sites

What you ask cannot be done in a simple C++ program of a few hundreds of lines. A program for general differentiation of functions with chain rule, product and all that kind of fancy things requires you to parse the input and break this apart.

 

The best algorithm would be to convert your input to INFIX notation. E.g. an expression like sin(x)*cos(x*x) will become:

 

(* (sin x) (cos (* x x)))

 

Using this notation, all differentations can be formulated in a trivial way.

 

product rule:

D(* A B) --> (+ (* A D(B)) (* D(A) B))

 

Chain rule:

D(U A) ---> (* (D(U) A) D(A))

 

Here D is the derivative operator, U is any unitary operator (e.g. sin, cos, tan). A and B are general operands, * is the multiplication, + is addition.

 

As you see, you need to do a LOT of manipulation with your input.

 

A language, much better suited for this kind of manipulations is LISP.

 

There are computer algebra systems (also open source ones, such as PARI), which can do all these fancy things for you. Programming it yourself can be done but will probably take you months of effort.

Link to comment
Share on other sites

I would suggest making a new data type and allow users to enter strings like:

 

"4x^12 - 3x^3"; // etc

 

Then overload some operators and you will have a pretty useful tool. I am working on a javaScript implementation of this, a polynomial object type that allows factorisation, multiplication etc. When I finish I will post my source code if you are interested, I have already written the input parser.

Link to comment
Share on other sites

Wow thanks for the input (lol... comp sci jokes are sad :D) but yah, I havent fully gotten parseing strings and such... but I'll work on it. The thing is, when you get a user to enter say a string, isn't it broken up at every space? I know theres some kind of cin.getline or something like that in c++ that can handle spaces...

 

What would that be? (is it cin.getline() ?)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.