From 5e792d5e1ac0c1898468aa0d66c629153fd096c4 Mon Sep 17 00:00:00 2001
From: Thomas West <tw00956@surrey.ac.uk>
Date: Tue, 3 Sep 2024 15:24:29 +0000
Subject: [PATCH] Kepler function.

---
 Function/Kepler.m | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
 create mode 100644 Function/Kepler.m

diff --git a/Function/Kepler.m b/Function/Kepler.m
new file mode 100644
index 0000000..671a84f
--- /dev/null
+++ b/Function/Kepler.m
@@ -0,0 +1,35 @@
+% (W2.1)
+
+function [E, no_of_iterations] = Kepler(e, M, tol)
+
+% Initialize the estimate of E with M
+E = M;
+
+% Set a maximum number of iterations
+max_iterations = 100;
+for ii = 1:max_iterations
+
+    f = M - E + e * sin(E);           % evaluate function with respect to E
+    fp = -1 + e * cos(E);             % evaluate first derivative with respect E
+  
+    % Print output
+    % fprintf('E: %f, |f(E)|: %e\n', E, abs(f));
+
+    % Note: Un-note the above code to see all the convergencies, it is
+    % currently noted as it computes a lot of unnecessary data into the
+    % command window. The script still outputs the correct value of 'E’.
+
+
+    % check for convergence using IF statement
+    if(abs(f) < tol)
+        break;
+    else
+        % Newton's method
+        dE = -f/fp;                          % delta E
+        E = E + dE;                          % update estimate of E
+    end
+end
+
+no_of_iterations = ii;
+
+end
\ No newline at end of file
-- 
GitLab