summaryrefslogtreecommitdiff
path: root/Homework/math4610/src/approx_derivative.c
diff options
context:
space:
mode:
authorElizabeth Alexander Hunt <me@liz.coffee>2026-07-02 11:55:17 -0700
committerElizabeth Alexander Hunt <me@liz.coffee>2026-07-02 11:55:17 -0700
commit6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6 (patch)
treeed97e39ec77c5231ffd2c394493e68d00ddac5a4 /Homework/math4610/src/approx_derivative.c
downloadmisc-undergrad-main.tar.gz
misc-undergrad-main.zip
Diffstat (limited to 'Homework/math4610/src/approx_derivative.c')
-rw-r--r--Homework/math4610/src/approx_derivative.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/Homework/math4610/src/approx_derivative.c b/Homework/math4610/src/approx_derivative.c
new file mode 100644
index 0000000..63d0b05
--- /dev/null
+++ b/Homework/math4610/src/approx_derivative.c
@@ -0,0 +1,38 @@
+#include "lizfcm.h"
+#include <assert.h>
+
+double central_derivative_at(double (*f)(double), double a, double h) {
+ assert(h > 0);
+
+ double x2 = a + h;
+ double x1 = a - h;
+
+ double y2 = f(x2);
+ double y1 = f(x1);
+
+ return (y2 - y1) / (x2 - x1);
+}
+
+double forward_derivative_at(double (*f)(double), double a, double h) {
+ assert(h > 0);
+
+ double x2 = a + h;
+ double x1 = a;
+
+ double y2 = f(x2);
+ double y1 = f(x1);
+
+ return (y2 - y1) / (x2 - x1);
+}
+
+double backward_derivative_at(double (*f)(double), double a, double h) {
+ assert(h > 0);
+
+ double x2 = a;
+ double x1 = a - h;
+
+ double y2 = f(x2);
+ double y1 = f(x1);
+
+ return (y2 - y1) / (x2 - x1);
+}