user466534
user466534

Reputation:

calculate derivative of function

suppose that there is given following matlab code

function dp = derp(p)
n = length(p) - 1;
p = p(:)';
dp = p(1:n).*(n:-1:1);
k = find(dp ~= 0);
if ~isempty(k)
dp = dp(k(1):end);
else
dp = 0;
end

as i know from the book,from where i have taken this code,it is explained just that it calculates derivative of function,of polynomials,but in case of i would like to write it in c++,how to rewrite it?please explain me ideas of this code,i just need to fully understand even in matlab what does it do,for a few moment let's forget c++ and pay attention to main ideas of code,please guys help me,i know actualy ,mathematicaly how derivative is calculated,i need understanding in code

Upvotes: 0

Views: 531

Answers (2)

aganders3
aganders3

Reputation: 5945

This function takes the coefficients of a polynomial as its input p. Given some polynomial function:

zx^n + yx^(n-1) + ... + cx^2 + bx + a
p = [z y ... c b a]

Given these coefficients, and the corresponding exponents [0 1 2 3 ...], we have an algorithm for taking a derivative of a polynomial (which you know). For each term in the polynomial:

  1. Multiply the coefficient of each term by the exponent of the that term
  2. Subtract one from the exponent

So...that's what your code does! I'll go through it (almost) line-by-line:

function dp = derp(p)
    n = length(p) - 1;

n is the length (number of terms) of the derivative it will just be one less than the input polynomial because the constant term drops out (corresponding exponent is zero). This is also the order of the input polynomial (highest exponent value).

    p = p(:)';

This just transposes the input vector. I'm...not sure why this is done in your code, it seems unnecessary.

    dp = p(1:n).*(n:-1:1);

Here each coefficient p(1:n) is multiplied by the exponent of its term (n:-1:1).

    k = find(dp ~= 0);

This searches for any indices where the coefficient is not zero and stores those indices in k.

    if ~isempty(k)
        dp = dp(k(1):end);
    else
        dp = 0;
    end

This if-statement sets dp to the coefficients of the derivative starting with the first non-zero coefficient. If all the calculated coefficients are zero (input function was a constant), dp is just set to zero.

Hopefully this helps!

Upvotes: 2

Willem Hengeveld
Willem Hengeveld

Reputation: 2776

The polynomial is represented as a list of coeficients.

make sure p is a row vector.

p = p(:)';

multiply each element by it's index

dp = p(1:n).*(n:-1:1);

remove any zero items from the front of the list

k = find(dp ~= 0);
if ~isempty(k)
   dp = dp(k(1):end);
else
   dp = 0;
end

in C you would write it probably like this:

void derivative(const double *poly, int polysize, double *dervpoly)
{
    for (int i=0 ; i<polysize-1 ; i++)
        dervpoly[i] = poly[i]*(polysize-i-1);
}

Where the caller is responsible for allocating an array of size polysize-1 for dervpoly.

The polynomial is:

poly[0]*x^(polysize-1) + poly[1]*x^(polysize-2) + .. + poly[polysize-2]*x + poly[polysize-1]

Note that it may be more convenient to store the coefficients in reverse order.

Upvotes: 2

Related Questions