from math import exp
#f(x) = exp(-x) - x#
#using simple fixed point iteration, Xi+1 = exp(-Xi)
x = 0##initial guess
y=[]
e=[]
y.append(0)
e.append(0)
for i in range(1,12):
if i == 1 :
y.append(x)
else:
y.append(exp(-y[(i-1)]))
e.append((y[(i)] - y[(i-1)]) * 100 / y[(i)])
print "x = :"
for x in y[1:]:
print x
print "\ne = :"
for e in e[1:]:
print e
from math import exp
%matplotlib inline
from matplotlib.pyplot import plot,title,xlabel,ylabel,show
#y1 = x
#y2 = exp(-x)
x=[]
y1=[]
y2=[]
for i in range(0,6):
if i == 0:
x.append(0)
else:
x.append(x[(i-1)] + 0.2)
y1.append(x[(i)])
y2.append(exp(-x[(i)]))
print "x = ",x
print "y1 = ",y1
print "y2 = ",y2
plot(x,y1)
plot(x,y2)#
title("f(x) vs x")
xlabel("x")
y=("f(x)")
# from the graph, we get
x7 = 5.7#
print "answer using two curve graphical method = ",x7
from math import exp
#f(x) = exp(-x)-x
#f'(x) = -exp(-x)-1
x=[]
et=[]
for i in range(0,5):
if i == 0:
x.append(0)
else:
x.append(x[(i-1)] - (exp(-x[(i-1)])-x[(i-1)])/(-exp(-x[(i-1)])-1))
et.append((x[(i)] - x[(i-1)]) * 100 / x[(i)])
x[(i-1)] = x[(i)]
print "x =",x
print "et =",et
from math import exp
#f(x) = exp(-x) - x
#f'(x) = -exp(-x) - 1
#f"(x) = exp(-x)
xr = 0.56714329#
#E(ti+1) = -f"(x)* E(ti) / 2 * f'(x)
Et0 = 0.56714329#
Et1 = -exp(-xr)* ((Et0)**2) / (2 * (-exp(-xr) - 1))#
print "Et1 = ",Et1,"which is close to the true error of 0.06714329"
Et1true = 0.06714329#
Et2 = -exp(-xr)* ((Et1true)**2) / (2 * (-exp(-xr) - 1))#
print "Et2 = ",Et2,"which is close to the true error of 0.0008323"
Et2true = 0.0008323#
Et3 = -exp(-xr)* ((Et2true)**2) / (2 * (-exp(-xr) - 1))#
print "Et3 = ",Et3,"which is close to the true error of 0.0008323"
Et4 = -exp(-xr)* ((Et3)**2) / (2 * (-exp(-xr) - 1))#
print "Et4 = ",Et4,"which is close to the true error of 0.0008323"
print "Thus it illustratres that the error of newton raphson method for this case is proportional(by a factor of 0.18095) to the square of the error of the previous iteration"
z = 0.5#
#f(x) = x**10 - 1
#f'(x) = 10*x**9
y=[]
for i in range(0,40):
if i==0:
y.append(z)
else:
y.append(y[(i-1)] - (y[(i-1)]**10 - 1)/(10*y[(i-1)]**9))
print "y ="
for yy in y:
print yy
print "Thus, after the first poor prediction, the technique is converging on to the true root of 1 but at a very slow rate"
from math import exp
#f(x) = exp(-x)-x
x=[]
er=[]
for i in range(0,5):
if i==0:
x.append(0)
else:
if i==1:
x.append(1)
else:
x.append(x[(i-1)] - (exp(-x[(i-1)])-x[(i-1)])*(x[(i-2)] - x[(i-1)])/((exp(-x[(i-2)])-x[(i-2)])-(exp(-x[(i-1)])-x[(i-1)])))
er.append((0.56714329 - x[(i)]) * 100 / 0.56714329)
print "x ="
for xx in x:
print xx
print "\net ="
for xx in er:
print xx
from math import log
#f(x) = log(x)
print "secant method"
x=[]
for i in range(0,4):
if i==0:
x.append(0.5)
else:
if i==1:
x.append(5)
else:
x.append(x[(i-1)] - log(x[(i-1)]) * (x[(i-2)] - x[(i-1)])/(log(x[(i-2)]) - log(x[(i-1)])))
print "x ="
for xx in x:
print x
print "thus, secant method is divergent"
print "Now, False position method"
xl = 0.5#
xu = 5#
for i in range(0,3):
m = log(xl)#
n = log(xu)#
xr = xu - n*(xl - xu)/(m - n)#
print "xr = ",xr
w = log(xr)#
if m*w < 0:
xu = xr#
else:
xl = xr#
print "thus, false position method is convergent"
from math import exp
Del = 0.01#
z = 0.56714329
x1 = 1#
#f(x) = exp(-x) - x
x=[]
print "x1 = ",x1
for i in range(0,4):
if i == 0:
x.append(1)
else :
w = x[(i-1)]
m = exp(-x[(i-1)]) - x[(i-1)]
x[(i-1)] = x[(i-1)]*(1+Del)#
n = exp(-x[(i-1)]) - x[(i-1)]#
x.append(w - (x[(i-1)]- w) * m/(n-m))
em = (z - x[(i)])*100/z#
print "x = ",x[(i)]
print "error = ",em,"%"
from __future__ import division
#f(x) = x**3 - 5*x**2 + 7*x -3
#f'(x) = 3*x**2 - 10*x + 7
print "standard Newton Raphson method"
x=[]
et=[]
for i in range(0,7):
if i == 0:
x.append(0)
else:
x.append(x[(i-1)] - ((x[(i-1)])**3 - 5*(x[(i-1)])**2 + 7*x[(i-1)] -3)/(3*(x[(i-1)])**2 - 10*(x[(i-1)]) + 7))
et.append((1 - x[(i)]) * 100 / 1)
print "x = ",x[i]
print "error = ",et[(i-1)],"%"
x[(i-1)] = x[(i)]
print "Modified Newton Raphson method"
#f"(x) = 6*x - 10
x=[]
et=[]
for i in range(0,4):
if i == 0:
x.append(0)
else:
x.append(x[(i-1) ]- ((x[(i-1)])**3 - 5*(x[(i-1)])**2 + 7*x[(i-1)] -3)*((3*(x[(i-1)])**2 - 10*(x[(i-1)]) + 7))/((3*(x[(i-1)])**2 - 10*(x[(i-1)]) + 7)**2 - ((x[(i-1)])**3 - 5*(x[(i-1)])**2 + 7*x[(i-1)] -3) * (6*x[(i-1)] - 10)))
et.append((1 - x[(i)]) * 100 / 1)
print "x = ",x[i]
print "error = ",et[i-1],'%'
x[(i-1) ]= x[(i)]
from __future__ import division
from math import sqrt
#u(x,y) = x**2 + x*y - 10
#v(x,y) = y + 3*x*y**2 -57
x=[]
y=[]
for i in range(0,4):
if i == 0:
x.append(1.5)
y.append(3.5)
else:
x.append(sqrt(10 - (x[(i-1)])*(y[(i-1)])))
y.append(sqrt((57 - y[(i-1)])/(3*x[(i)])))
print "x =",x[(i)]
print "y =",y[i]
print "Thus the approaching to the true value 0f x = 2 and y = 3"
from __future__ import division
from numpy import mat
from numpy.linalg import det
def u(x,y):
z=x**2+x*y-10
return z
def v(x,y):
z=y+3*x*y**2-57
return z
x=1.5#
y=3.5#
e=[100,100]#
while e[0]>0.0001 and e[1]>0.0001:
J=mat([[2*x+y, x],[3*y**2, 1+6*x*y]])
deter=det(J)#
u1=u(x,y)#
v1=v(x,y)#
x=x-((u1*J[1,1]-v1*J[0,1])/deter)#
y=y-((v1*J[0,0]-u1*J[1,0])/deter)#
e[(0)]=abs(2-x)#
e[(1)]=abs(3-y)#
bracket=[x ,y]#
print "bracket:",bracket