Lab - Session - 1.ipynb - Colaboratory
Lab - Session - 1.ipynb - Colaboratory
Lab - Session - 1.ipynb - Colaboratory
2) f (x) = xcos(x) − 2x
2
+ 3x − 1
3) f (x) = 2xcos(2x) − (x + 1)
2
import numpy as np
from matplotlib import pyplot as plt
def f(x):
return (np.cos(x)-1.3*x)
x = np.linspace(-10,10 , 1000)
plt.plot(x,f(x), color='red')
plt.hlines(y=0,xmin=-10,xmax=10,color='blue')
plt.show()
import numpy as np
from matplotlib import pyplot as plt
def f(x):
return (x*np.cos(x)-2*(x**2)+3*x-1)
x = np.linspace(-10,10 , 1000)
plt.plot(x,f(x), color='red')
plt.hlines(y=0,xmin=-10,xmax=10,color='blue')
plt.show()
import numpy as np
from matplotlib import pyplot as plt
def f(x):
return (2*x*np.cos(2*x)-(x+1)**2)
x = np.linspace(-10,10 , 1000)
plt.plot(x,f(x), color='red')
plt.hlines(y=0,xmin=-10,xmax=10,color='blue')
plt.show()
import numpy as np
from matplotlib import pyplot as plt
def f(x):
return (np.cos(x) - 1.3 * x)
plt.show()
Lab Task 2: Complete the missing code of bisection method accordding to the explained algorithm and find root of given problems by bisection
method according to the instructions given in table.
1) f 1(x) = cos(x) − 1.3x
2) f 2(x) = xcos(x) − 2x
2
+ 3x − 1
3) f 3(x) = 2xcos(2x) − (x + 1)
2
import numpy as np
from tabulate import tabulate
def func(x):
return (2*x*np.cos(2*x)-(x+1)**2)
print(tabulate(data,headers=['#','x1','f(x1)','x2','f(x2)','xr','f(xr)',"error"],tablefmt="github"))
print('\nRoot of given function is x=%.9f in n=%d number of iterations with a tolerence=%.4f' %(xr,iter,tol))
return
bisection(func,-5,-1)
Lab Task 3: Find root of given problems by Newton Raphson method according to the instructions given in table.
1) f 1(x) = cos(x) − 1.3x
2) f 2(x) = xcos(x) − 2x
2
+ 3x − 1
3) f 3(x) = 2xcos(2x) − (x + 1)
2
import numpy as np
from tabulate import tabulate
## module Newton_Raphson
''' newton_raphson(func, dfunc, x0, tol=1e-4, max_iter=1000)
Finds a root of f(x) = 0 by newton_raphson.
'''
def func(x):
return (2*x*np.cos(2*x)-(x+1)**2)
def dfunc(x):
return ( 2*np.cos(2*x)- 4*x*np.sin(2*x) - 2*(x+1))
def newton_raphson(func, dfunc, x0, tol=0.001, max_iter=1000):
xr = x0
data=[]
iter = 0
error = tol + 1
for i in range(max_iter):
iter+=1
fx = func(xr)
dx = dfunc(xr)
if abs(dx) < tol:
raise Exception("Derivative is close to zero!")
xrold=xr
xr = xr - fx/dx
error=abs(xr-xrold)
data.append([iter,xr,func(xr),error])
if error < tol:
print(tabulate(data,headers=['Iteration','xr','f(xr)',"error"],tablefmt="github"))
print('\nRoot of given function is x=%.9f in n=%d number of iterations with a tolerence=%.4f' %(xr,iter,tol))
return
Lab Task 4: Find root of given problems by using fsolve command of scipy.optimize
1) f 1(x) = cos(x) − 1.3x
2) f 2(x) = xcos(x) − 2x
2
+ 3x − 1
3) f 3(x) = 2xcos(2x) − (x + 1)
2
import scipy.optimize
dir(scipy.optimize)
'minimize_scalar',
'minpack',
'minpack2',
'moduleTNC',
'newton',
'newton_krylov',
'nnls',
'nonlin',
'optimize',
'quadratic_assignment',
'ridder',
'root',
'root_scalar',
'rosen',
'rosen_der',
'rosen_hess',
'rosen_hess_prod',
'shgo',
'show_options',
'slsqp',
'test',
'tnc',
'toms748',
'zeros']
[0.62418458]
[0.29753023]
[-2.19130801]
Lab Task 5: Write program of Secant and False Position method by altering above codes.
import numpy as np
from tabulate import tabulate
def func(x):
return (2*x*np.cos(2*x)-(x+1)**2)
def secant(func, x0, x1, tol=0.001, max_iter=1000):
if func(x1) * func(x0) >= 0:
return "Error: Choose different interval, function should have different signs at the interval endpoints."
iter= 0
error=tol+1
xr=x1
xrold=x0
data=[]
while iter < max_iter and error > tol:
data.append([iter,xr,func(xr),xrold,func(xrold),error])
xrold=xr
xr=xr-(func(xr)*(xr-x0))/(func(xr)-func(x0))
x0=xrold
iter+=1
error=abs(func(xr)-func(xrold))
print(tabulate(data,headers=['Iteration','xr','f(xr)', 'xrold' ,'f(xrold)',"error"],tablefmt="github"))
print('\nRoot of given function is x=%.9f in n=%d number of iterations with a tolerence=%.5f' %(xr,iter,tol))
secant(func,-3,-2)
import numpy as np
p py p
from tabulate import tabulate
def func(x):
return (2*x*np.cos(2*x)-(x+1)**2)
def false_position(func, x0, x1, tol=0.00001, max_iter=1000):
if func(x1) * func(x0) >= 0:
return "Error: Choose different interval, function should have different signs at the interval endpoints."
iter= 0
error=tol+1
if x0<0:
a=x0
b=x1
else: