Python Forum
f1(), f2() lambda functions addition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
f1(), f2() lambda functions addition
#3
import random
def diff_param(f,h=0.001):
    return (lambda x: (f(x+h)-f(x))/h)
 
 
def NR(func, deriv, epsilon=10**(-8), n=100, x0=None):
    if x0 is None:
        x0 = random.uniform(-100.,100.)
    x=x0; y=func(x)
    for i in range(n):
        if abs(y)<epsilon:
            print (x,y,"convergence in",i, "iterations")
            return x
        elif abs(deriv(x))<epsilon:
            print ("zero derivative, x0=",x0," i=",i, " xi=", x)
            return None
        else:
            print(x,y)
            x = x- func(x)/deriv(x)
            y = func(x)
    print("no convergence, x0=",x0," i=",i, " xi=", x)
    return None

# a
def equal(f1, f2):
    root = NR(f1()-f2(),diff_param(f1()-f2(),h=0.001))
    return root

def test():
    f1, f2 = lambda x:4*x+1, lambda x: -x+6
    if equal(f1,f2) == None or abs(equal(f1, f2) - 1) > 10**-7:
        print("error in equal")

test()
NR() and diff_param() are given functions by the teacher. equal() is what I need to write, adding only two lines! (which I already added here)
In test() you can see f1,f2 , also given by the teacher, that the program needs to run with.
Hope this helps to understand
Reply


Messages In This Thread
RE: f1(), f2() lambda functions addition - by Danielk121 - Dec-01-2017, 09:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] f1(), f2() lambda functions addition baby_quant 8 3,999 Sep-14-2018, 04:14 AM
Last Post: baby_quant
  Homework on addition with working paperplanexx 1 2,529 Aug-19-2018, 12:40 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020