-
Notifications
You must be signed in to change notification settings - Fork 1
/
ict01FirstDerivatives.py
69 lines (53 loc) · 1.48 KB
/
ict01FirstDerivatives.py
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
66
67
68
69
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 26 21:30:23 2021
@author: uqcleon4
"""
import numpy as np
import matplotlib.pyplot as plt
# Define functions and their derivatives
def f1(x):
return x*x*x
def f2(x):
return 3*x*x - 2*x
def f3(x):
return np.sin(x)
def f1Prime(x):
return 3*x*x
def f2Prime(x):
return 6*x - 2
def f3Prime(x):
return np.cos(x)
# Create arrays for x and f(x)
h = 0.1
x = np.arange(-5,6,h,dtype=float)
f = f1(x)
# Calculate the forward, backward and central differences
forwardD = np.empty_like(f)
forwardD[:-1] = (f[1:]-f[:-1])/h
backwardD = np.empty_like(f)
backwardD[1:] = (f[1:]-f[:-1])/h
centralD = np.empty_like(f)
centralD[1:-1] = (f[2:]-f[:-2])/(2*h)
# Calculate the error associated with each difference approach
fd = f1Prime(x)
errors = np.zeros((3,x.size))
errors[0,:-1] = (fd[:-1]-forwardD[:-1])
errors[1,1:] = (fd[1:]-backwardD[1:])
errors[2,1:-1] = (fd[1:-1]-centralD[1:-1])
# Plot the function and each derivative approximation
fig,(ax1,ax2) = plt.subplots(2,1,sharey=False)
ax1.plot(x, f,
x[:-1], forwardD[:-1], '-.',
x[1:], backwardD[1:], ':',
x[1:-1], centralD[1:-1], '--')
ax1.set(xlabel="x", ylabel="f(x) and f'(x)")
ax1.legend(["f", "forwardD","backwardD", "centralD"])
ax1.grid()
ax2.plot(x[:-1], errors[0,:-1], '-.',
x[1:], errors[1,1:], ':',
x[1:-1], errors[2,1:-1], '--')
ax2.set(xlabel="x", ylabel="Error in f'(x)")
ax2.legend(["forwardD","backwardD", "centralD"])
ax2.grid()
plt.show()