A quick exercise in implementing an algorithm calculates the coefficients of the first derivative of a continuous function with one variable.

Implementation is done in the code below. The function takes a list of coefficients. These must be specified in the order of variable degree. For the function below the list of coefficients would thus be [1,3,4,2]:

# function for calculating first derivative def firstDerivative(listOfCoefficients): # derived coeffiecients will be appended to below list derived_coefficients = [] # derivative coefficients for i in range(0,len(listOfCoefficients)): derived_coefficients.append(listOfCoefficients[i]*i) # return derived coefficients return derived_coefficients

The function is tested below:

firstDerivative([1,3,4,2])

[0, 3, 8, 6]

The same could have been achieved using list comprehension in Python:

coefficients = [1,3,4,2] [coefficients[i]*i for i in range(0,len(coefficients)) ]

[0, 3, 8, 6]

## Leave a Reply