Skip to content

Instantly share code, notes, and snippets.

@therealFoxster
Created April 4, 2023 20:35
Show Gist options
  • Save therealFoxster/a0090ba90046e26633a2341d15bbb15c to your computer and use it in GitHub Desktop.
Save therealFoxster/a0090ba90046e26633a2341d15bbb15c to your computer and use it in GitHub Desktop.
Newton's Iterative Formula in SageMath
# Function variable x
x = var('x');
# Function f(x)
f(x) = (x-3)*ln(x)-1;
# Derivative f'(x)
df = diff(f,x);
# Newton's Iterative Formula
nif(x) = x-(f/df)(x);
x1 = 5; # Initial guess
i = 10; # Number of iterations
for _ in range(i):
x=N(nif(x1));
print(x, "<-- iteration", _+1)
x1=x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment