from __future__ import print_function from math import sqrt # Oppgave a def secant(f, x0, x1, N): xpp = x0 xp = x1 for i in range(N): x = xp - ((xp-xpp)/(f(xp)-f(xpp)))*f(xp) xpp = xp xp = x return x # Oppgave b #def f(x): # return x*x-2 def test_secant(): x0 = 3.0 x1 = 2.0 f = lambda x: x*x - 2 N = 10 x = secant(f, x0, x1, N) success = abs(x - sqrt(2)) <= 1E-8 msg = "Did not find the root sqrt(2) for the function x*x-2" assert success, msg test_secant()