From 0d4e770dc06763d225dce66f82bd49b052bead06 Mon Sep 17 00:00:00 2001 From: Christian Kolset Date: Fri, 9 May 2025 09:53:20 -0600 Subject: Added auto-generated (md converted) notebooks to modules. Added module 3 tutorials --- tutorials/module_3/ForwardFiniteDifference | 37 ++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tutorials/module_3/ForwardFiniteDifference (limited to 'tutorials/module_3/ForwardFiniteDifference') diff --git a/tutorials/module_3/ForwardFiniteDifference b/tutorials/module_3/ForwardFiniteDifference new file mode 100644 index 0000000..128bb56 --- /dev/null +++ b/tutorials/module_3/ForwardFiniteDifference @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Thu May 8 13:27:05 2025 + +@author: christian +""" +import numpy as np +import matplotlib.pyplot as plt + +# step size +h = 0.1 +# define grid +x = np.arange(0, 2*np.pi, h) +# compute function +y = np.cos(x) + +# compute vector of forward differences +forward_diff = np.diff(y)/h +# compute corresponding grid +x_diff = x[:-1:] +# compute exact solution +exact_solution = -np.sin(x_diff) + +# Plot solution +plt.figure(figsize = (12, 8)) +plt.plot(x_diff, forward_diff, '--', \ + label = 'Finite difference approximation') +plt.plot(x_diff, exact_solution, \ + label = 'Exact solution') +plt.legend() +plt.show() + +# Compute max error between +# numerical derivative and exact solution +max_error = max(abs(exact_solution - forward_diff)) +print(max_error) \ No newline at end of file -- cgit v1.2.3