#Program to implement 2D Newton Raphson method to find intersecting points for
# i) x^2 + y^2 = 4
# ii) (x-1)^2 + y^2 = 4
from math import*
def f(x,y):
y=x**2+y**2-4
return y
def g(x,y):
y=(x-1)**2+y**2-4
return y
def fx(x,y):
y=2*x
return y
def gx(x,y):
y=2*(x-1)
return y
def fy(x,y):
y=2*y
return y
def gy(x,y):
y=2*y
return y
x=float(input("Enter the initial guess,x0="))
y=float(input("Enter the initial guess,y0="))
s=0
det_J_inv=fx(x,y)*gy(x,y)-fy(x,y)*gx(x,y)
if det_J_inv==0.0:
print("Enter new guess values.")
else:
while abs(f(x,y))>.000000001 and abs(g(x,y))>.000000001 and s<50:
h=(f(x,y)*gy(x,y)-fy(x,y)*g(x,y))/float(det_J_inv)
k=(-gx(x,y)*f(x,y)+g(x,y)*fx(x,y))/float(det_J_inv)
x=x-h
y=y-k
s=s+1
print("The root is located at:\nx=",x,"y=",y,"\nRequired No. of iterations:",s)
# i) x^2 + y^2 = 4
# ii) (x-1)^2 + y^2 = 4
from math import*
def f(x,y):
y=x**2+y**2-4
return y
def g(x,y):
y=(x-1)**2+y**2-4
return y
def fx(x,y):
y=2*x
return y
def gx(x,y):
y=2*(x-1)
return y
def fy(x,y):
y=2*y
return y
def gy(x,y):
y=2*y
return y
x=float(input("Enter the initial guess,x0="))
y=float(input("Enter the initial guess,y0="))
s=0
det_J_inv=fx(x,y)*gy(x,y)-fy(x,y)*gx(x,y)
if det_J_inv==0.0:
print("Enter new guess values.")
else:
while abs(f(x,y))>.000000001 and abs(g(x,y))>.000000001 and s<50:
h=(f(x,y)*gy(x,y)-fy(x,y)*g(x,y))/float(det_J_inv)
k=(-gx(x,y)*f(x,y)+g(x,y)*fx(x,y))/float(det_J_inv)
x=x-h
y=y-k
s=s+1
print("The root is located at:\nx=",x,"y=",y,"\nRequired No. of iterations:",s)
Comments
Post a Comment