Python Forum
f1(), f2() lambda functions addition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
f1(), f2() lambda functions addition
#1


I got this home assignment where i need to use the Newton-Raphson method to find a root of a given function.
this time I needed to search for a common meeting point of two given functions - f1() and f2(), both are lambda functions.
So i thought about doing this:
[inline]def equal(f1, f2):
root = NR(f1()-f2(),diff_param(f1()-f2(),h=0.001))
return root[/inline]

But got error message because functions are
Error:
unsupported operand type(s) for -:

How could I apply my idea? or should i go for another way?

Thanks
Reply
#2
Can you provide full code which reproduces the problem? Currently, you've provided the function definition but not the call. Since Python is dynamically typed, we can't know anything about f1 and f2 other than you're trying to call them and use the minus operator with their return values.

It might help to provide a full homework description, if you don't understand what I'm saying.
Reply
#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
#4
I get a different error message than you
Error:
Traceback (most recent call last): File "testit.py", line 34, in <module> test() File "testit.py", line 31, in test if equal(f1,f2) == None or abs(equal(f1, f2) - 1) > 10**-7: File "testit.py", line 26, in equal root = NR(f1()-f2(),diff_param(f1()-f2(),h=0.001)) TypeError: <lambda>() takes exactly 1 argument (0 given)
Reply
#5
Yes because when you write f1() like you did it expects an argument, integer in this case (my h.w).
Then it calculate also but I dont wish for that
Reply
#6
Ok i got this.
All I needed to do was to define new function , f3, like this:
f3 = lambda x: f1(x) - f2(x)
and then calling NR() with f3
Thanks for the help any ways
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] f1(), f2() lambda functions addition baby_quant 8 3,841 Sep-14-2018, 04:14 AM
Last Post: baby_quant
  Homework on addition with working paperplanexx 1 2,477 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