Python Forum
Simple fixed point iteration root finding in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple fixed point iteration root finding in python
#1
Hey all,

I recently have started a class that involves a bit of python programming and am having a bit of trouble on this question. The question asks to preform a simple fixed point iteration of the function below:

f(x) = sin(sqrt(x))-x, meaning g(x) = sin(sqrt(x))

The initial guess is x0 = 0.5, and the iterations are to continue until the absolute error is less than 0.01%. the absolute error is equal to ((new-old)/new)*100
I have the basis of the code, but I either receive a blank output, or "'xr' is not defined". I have inserted what i have so far:

import math

def f(x):
    return math.sin(math.sqrt(x))-x
def g(x):
    return math.sin(math.sqrt(x))

def Fixpt(xr, i, xrold, ea):
    
    xrold = 10 # i put this as a random number since it needs to be defined
    i = 0
    xr = 0.5
    
    ea = abs(((xr-xrold)/xr)*100)
    
    while (ea >= .01):
    
        xrold = xr
        xr = g(xrold)
        i = i + 1
    
    print ("The root is: %f"%xr)
Any help is appreciated, thanks all.
Reply
#2
I'll admit I'm not sure what you're doing mathematically but shouldn't line 14 be in the while loop if the condition depends on the value of ea changing?

I'm also assuming variable i is an iteration counter but you're not doing anything with it after you count the iterations.
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply
#3
(Jul-10-2020, 07:00 PM)Marbelous Wrote: I'll admit I'm not sure what you're doing mathematically but shouldn't line 14 be in the while loop if the condition depends on the value of ea changing?

I'm also assuming variable i is an iteration counter but you're not doing anything with it after you count the iterations.

Sorry for the lack of clarification,

Basically I am plugging the initial guess (x0 = 0.5)into g(x) to get a value xr. this value is then plugged into the ea formula to get a % error between the old and the new xr values. the new xr value is then plugged into g(x) again and the process repeats until the % error is below 0.01% as seen in the while statement. I changed some stuff around with your suggestions and i am now left with this:
import math

def f(x):
    return math.sin(math.sqrt(x))-x

def g(x):
    return math.sin(math.sqrt(x))

def Fixpt(xr, xrold, ea):
    
    xrold = 10
    xr = 0.5
    
    while (ea >= .01):
        
        ea = abs(((xr-xrold)/xr)*100)
    
        xrold = xr
        xr = g(xrold)
    
print("The root is: %6f"%xr)
But it is still not working.
Reply
#4
A few things:

1. Unless you've forgotten to post some of the code, Fixpt isn't called anywhere and neither is f.

2. Line 21: the variable xr is local to the Fixpt function. You need to learn about scope.

3. Why do you need to pass xr and xrold to the function? If you're hoping the changes to them will be reflected in the calling code, they won't - they're basically passed by value.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Table with fixed columns Mike2607 3 2,914 Nov-26-2019, 08:17 PM
Last Post: ichabod801
  Finding the end point of a path happylol123 1 1,941 Mar-31-2019, 09:55 AM
Last Post: Yoriz
  Python Finding Prime Factors foxman322 1 2,395 Jan-11-2019, 04:33 PM
Last Post: ichabod801
  Repeating elements when appending in iteration in Python 3.6 miguelsantana 2 3,525 Dec-17-2017, 01:22 AM
Last Post: Terafy
  split the line fixed length tokens bb8 5 5,784 Nov-25-2017, 06:18 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

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