-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDerivative.py
More file actions
65 lines (47 loc) · 2.13 KB
/
Derivative.py
File metadata and controls
65 lines (47 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Author: Jerome Richards
# Derivative
#
# Takes the derivative of a function of a set of points
import math
import numpy as np
# Description: Takes derivative of received function from starting point to end point
# Parameters: a function, FUNC, starting value start, ending value end, increment value DELTA, and the derivative values NUM_OF_dF (NUM_OF_dF determines the amount of times to take a derivative.)
# Returns: two arrays - x values and y values, with spacings of DELTA increments
def DerivativeOfFunction(FUNC, start, end, DELTA, NUM_OF_dF):
# Description: The derivative of the inquired function, F.
# Parameters: a function, f, a floating point value x, and the derivative n (n determines the amount of times to take a derivative.)
# Returns: a floating point value
def dF(f, x, n):
if n <= 0:
return f(x);
else:
h = 0.01;
return (dF(f, x + h, n - 1) - dF(f, x, n - 1)) / h;
###########################################################
# #
# Creates a y array with the original function's y values #
# #
###########################################################
x = [];
y = []; # y values for the original function
delta = DELTA; # incremental distance on x-axis
i = start;
while i < end:
x.append(i);
y.append(dF(FUNC, i, NUM_OF_dF));
i = i + delta;
return x, y;
# Description: The derivative of the input data
# Parameters: the horizontal data, x_data, and the vertical data, y_data
# Returns: two arrays - x values and y values, stored in x_data array and y data array respectively
def DerivativeOfData(x_data, y_data):
if len(x_data) != len(y_data):
print('Both arrays must be the same length');
return 0, 0;
x = [];
y = [];
for i in range(0, len(x_data) - 1):
m = (y_data[i + 1] - y_data[i]) / (x_data[i + 1] - x_data[i]);
y.append(m);
x.append(x_data[i]);
return x, y;