Jul-10-2020, 05:29 PM
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:
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.