I'm trying to upgrade my class library to my current mathematical skill set and am having trouble with multi-term polynomials, especially those I where I don't know how many terms the user will be putting in. I know that the formula for the first derivative of a polynomial is ax^n = n * ax^n-1; and the second derivative is the derivative of the first derivative. So for example:
Find the derivative of 4x^2:
4x^2 = 2 * 4x^2 - 1 = 8x^2-1 = 8x
Find the second derivative of 4x^3:
4x^3 = 3 * 4x^3-1 = 12x^3-1 = 12x^2
12x^2 = 2 * 12x^2-1 = 24x^2-1 = 24x
Right now I just have an algorithm for the first derivative of a single term polynomial:
// Pesudocode (C++ Syntax):
int Base = 0;
int Exponent = 0;
string Variable = "";
cout << "Please input a polynomial to the first degree in the form (4 y 9): ";
cin >> Base >> Variable >> Exponent;
Base = Exponent * Base;
Exponent = Exponent > 1 ? Exponent - 1 : 0;
Variable = Exponent == 1 ? Variable : Variable + "^";
Variable = Exponent == 0 ? "" : Variable;
string exString = (Exponent == 1 || Exponent == 0) ? "" : (const char*)Exponent;
cout << "The first derivative of the polynomial you submited is: " << Base << Variable << exString << "\n";
Chose to not work on the second derivatives until I figure out the whole parameter problem. I'm sure I have to use arrays or lists to accomplish this, but I'm not sure how the algorithm would work for a polynomial with 3 or more terms:
3x^2 + 13x + 4
Also the derivative of a constant (basically any number without a variable and exponent of 2 or higher) is equal to 0 for those who don't know and the derivative of infinity is undefined.