Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 [split] f1(), f2() lambda functions addition baby_quant Programmer named Tim Posts: 6 Threads: 0 Joined: Sep 2018 Reputation: 0 Likes received: 0 #1 Sep-11-2018, 06:40 AM (This post was last modified: Sep-11-2018, 12:08 PM by buran. Edited 1 time in total.) Hi Guys, I am trying to solve f=x^2-2*x using Newton Raphson method, but somehow code is not providing the correct results. Can you please help me? ```def newton_raphson(f,guess,tolerance,max_iter,increment,epsilon): for iter in range(max_iter): func_value=f(guess) fprime=(f(guess+increment)-f(guess-increment))/(2.0*increment) if (abs(fprime)>epsilon): next_guess=guess-(float(func_value)/float(fprime)) if(abs(next_guess-guess)<=tolerance*abs(next_guess)) or fprime==0: break else: guess=next_guess else: break print("Root of the function %s is: %f" %(f,next_guess) )``` buran wrote Sep-11-2018, 12:08 PM:Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you. See BBcode help for more info. ichabod801 Bunny Rabbit Posts: 2,219 Threads: 49 Joined: Sep 2016 Reputation: 104 Likes received: 737 #2 Sep-11-2018, 12:53 PM Doesn't the Newton-Raphson method for finding the root of an equation use the derivative of the equation to find the root of the tangent to f at the guess? I'm not seeing where you are doing that in your function. Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures baby_quant Programmer named Tim Posts: 6 Threads: 0 Joined: Sep 2018 Reputation: 0 Likes received: 0 #3 Sep-11-2018, 01:12 PM Hi @ichabod801, Derivative is calculated in the line which defines fprime. Derivative here is being calculated using first principle of calculus. Thanks ichabod801 Bunny Rabbit Posts: 2,219 Threads: 49 Joined: Sep 2016 Reputation: 104 Likes received: 737 #4 Sep-11-2018, 04:03 PM Oh, I see, you're estimating the tangent line. What exactly is the problem you're getting? It's working for me. Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures baby_quant Programmer named Tim Posts: 6 Threads: 0 Joined: Sep 2018 Reputation: 0 Likes received: 0 #5 Sep-13-2018, 04:37 AM Ok. Is it giving solution for x**2-2*x as 2? You can pass this function as lambda function. ichabod801 Bunny Rabbit Posts: 2,219 Threads: 49 Joined: Sep 2016 Reputation: 104 Likes received: 737 #6 Sep-13-2018, 12:44 PM So what's the problem? 2 ** 2 - 2 * 2 = 4 - 4 = 0. That's a root of the function, right? Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures baby_quant Programmer named Tim Posts: 6 Threads: 0 Joined: Sep 2018 Reputation: 0 Likes received: 0 #7 Sep-13-2018, 12:47 PM Yes. But can we find the root 2 by passing the lambda function `lambda x: x**2-2*x` inside the newton raphson method? When I run the code on my machine, I am not getting 2. ichabod801 Bunny Rabbit Posts: 2,219 Threads: 49 Joined: Sep 2016 Reputation: 104 Likes received: 737 #8 Sep-13-2018, 12:52 PM I'm getting 2 when I run it on my machine. As I asked before, what exactly is the problem you are having? What output are you getting that is incorrect? What inputs are you using to get that incorrect answer? Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures baby_quant Programmer named Tim Posts: 6 Threads: 0 Joined: Sep 2018 Reputation: 0 Likes received: 0 #9 Sep-14-2018, 04:14 AM (This post was last modified: Sep-14-2018, 04:14 AM by baby_quant. Edited 1 time in total.) Hi Now when I am running the code I am getting the output correctly. Here is the code for future references: ```def newton_raphson(f,guess,tolerance,max_iter,increment,epsilon): for iter in range(max_iter): func_value=f(guess) fprime=(f(guess+increment)-f(guess-increment))/(2.0*increment) if (abs(fprime)>epsilon): next_guess=guess-(float(func_value)/float(fprime)) if(abs(next_guess-guess)<=tolerance*abs(next_guess)) or fprime==0: break else: guess=next_guess else: break print("Root of the function %s is: %f" %(f,next_guess) ) if __name__=="__main__": newton_raphson(lambda x:x**2-x*4,5,.000001,100,.001,.001) ```Thanks for your help ! « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post help using functions from other files lga13 3 72 Dec-12-2018, 12:46 AM Last Post: snippsat Homework on addition with working paperplanexx 1 254 Aug-19-2018, 12:40 PM Last Post: j.crater Nested Functions aditvaddi 1 415 Jun-26-2018, 03:04 AM Last Post: ichabod801 [split] [split] regular expression kerzol81 1 331 Jun-24-2018, 09:18 AM Last Post: volcano63 def functions and floats alwillia 3 386 May-26-2018, 07:31 PM Last Post: buran Issues with Lambda Function fad3r 5 361 May-22-2018, 04:13 PM Last Post: fad3r Understanding Functions kiki1113 2 370 May-09-2018, 11:38 PM Last Post: kiki1113 Pass 2 columns via loc to lambda in pandas fad3r 1 1,434 Feb-22-2018, 09:57 PM Last Post: glidecode f1(), f2() lambda functions addition Danielk121 5 896 Dec-02-2017, 09:27 AM Last Post: Danielk121 I am stuck on functions Hebruiser 11 1,548 Nov-11-2017, 08:46 PM Last Post: Hebruiser

Forum Jump:

Users browsing this thread: 1 Guest(s)