As I posted a bit about double integrals in Part 1 and Part 2, I thought I might go the other direction and post something about derivatives, or differentiation. Recall the integral example formula:
So, if you had a function, \(x^{2}\), the derivative would be, \(2x\). Or more formally, \(x^{n}\) becomes \(n \cdot x^{n-1}\), and \(\sin(x)\) becomes \(\cos(x)\). In other words, it’s all about slope. So, back down to Earth, and to put it into perspective, let’s say we have a circle formula, \(\pi r^{2}\). Then the derivative of that would be, \(2 \pi r\), or \(\pi d\). So, we find the circumference of a circle is the derivative of the circle’s area. But, I digress.
In this post, we will talk about a simple C++ program to calculate the derivative of a polynomial. A quick description of the functionality is where the input string is split into tokens and for each term, the derivative is calculated separately and then added to get the final result. This was pieced together from other snippets on the Internet to do similar functions. Some limitations are the formats of the inputs require the power symbol for each substring. And so, \(4x^{3}\), would be entered as ‘4x^3’, for example. If \(x\) is to be entered for a power of one, the input would require ‘1x^1’. If an input was not required, ‘0’ could be entered. Also, the inputs only accept integers. So, 4.9x^2 would be calculated as 4x^2. This is a future TODO.
If we had a polynomial formula, \(4x^2 + 11x^5 -4x^3 + 6x^3\), which would look like:
The if() statements are to handle zero inputs, yet provide tokens for calculation, and to make the resultant formula output look better. One area that needs improvement is the trailing ‘+’ if there are zero inputs at the end of the entries. I started to fix that with additional ‘if/then/else’ statements, but it became too cumbersome, so I dropped that idea, at least for now. It was only to make the resultant formula display prettier, and could be done away with entirely.
The two functions to calculate the derivatives are the business end of the program, and are presented as is:
Code
long long derivativeTerm(string pTerm, long long val){// Get coefficient string coeffStr =""; long unsigned int i; // type 'int i' clashes with 'pTerm.size()', but will compile.for (i =0; pTerm[i] !='x'; i++)coeffStr.push_back(pTerm[i]); long long coeff =atol(coeffStr.c_str());// Get Power (Skip 2 characters for x and ^) string powStr ="";for (i = i +2; i !=pTerm.size(); i++)powStr.push_back(pTerm[i]); long long power =atol(powStr.c_str());// For ax^n, we return anx^(n-1) return coeff * power *pow(val, power -1);}long long derivativeVal(string& poly, int val){ long long ans =0;// We use istringstream to get input in tokens istringstream is(poly); string pTerm;while (is >> pTerm) {// If the token is equal to '+' then// continue with the stringif (pTerm =="+") continue;// Otherwise find the derivative of that// particular termelse ans = (ans +derivativeTerm(pTerm, val)); } return ans;}
There is ample opportunity for improvement, especially for double entries. But for now, I have other projects to consider, so that may be for a later time.
So for now, have a great day and God Bless you and yours!