Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import scipy.integrate as integrate
- def PROBLEM1_EQ(x):
- i = ((3 * (x**3)) - (5 * (x**2)) - (4 * x) + 4)
- return i
- def PROBLEM1_DERIV_EQ(x):
- i = (9 * (x ** 2)) - (10 * x) - 4
- return i
- def PROBLEM5_EQ(x):
- i = ((math.exp(x)) - (100 * (x**2)))
- return i
- def PROBLEM5_DERIV_EQ(x):
- i = (-200 * x) + (math.exp(x))
- return i
- def PROBLEM7_EQ(x):
- i = (x * (math.cosh(100/x))) - x - 15
- return i
- def PROBLEM7_EQ_DER(x):
- i = math.cosh(100/x)-((100*math.sinh(100/x))/x)-1
- return i
- def PROBLEM7_Integ():
- i = integrate.quad(lambda x: math.sqrt(1+math.pow(math.sinh(x/335.7), 2)), 0, 100)
- print("Length:", i[0] * 2)
- def rate_of_convergence(x):
- L = len(x)
- y0 = math.log(abs(x[L-1] - x[L-2]))
- x0 = math.log(abs(x[L-2] - x[L-3]))
- y1 = x0
- x1 = math.log(abs(x[L-3] - x[L-4]))
- # x0 = round(x0, 1)
- # y0 = round(y0, 0)
- # x1 = round(x1, 0)
- # y1 = x0
- slope = ((y1 - y0) / (x1 - x0))
- print("\nRate of convergence: ", slope)
- def bisect(fcn,a,b,acc):
- fa = fcn(a)
- #fb = fcn(b)
- count = 1
- print(f'{count:<4} : {a:<20} {b:<25} ')
- while abs(b - a) > acc:
- m = (a + b) / 2
- fm = fcn(m)
- if fa * fm <= 0:
- b = m
- count = count + 1
- print(f'{count:<4} : {a:<20} {b:<25} ')
- else:
- a = m
- count = count + 1
- print(f'{count:<4} : {a:<20} {b:<25} ')
- count = count + 1
- return (a + b) / 2
- def secant(fcn, x0, x1, op1, tol):
- count = 2
- outputs.append(x0)
- outputs.append(x1)
- if op1 == 1:
- for x in range(4):
- x_new = x1 - ((fcn(x1) * (x0 - x1)) / (fcn(x0) - fcn(x1)))
- print("x" + str(count),":",x_new)
- outputs.append(x_new)
- count = count + 1
- x0 = x1
- x1 = x_new
- if op1 == 2:
- while(abs(x0 - x1) > tol):
- x_new = x1 - ((fcn(x1) * (x0 - x1)) / (fcn(x0) - fcn(x1)))
- print("x" + str(count),":",x_new)
- outputs.append(x_new)
- count = count + 1
- x0 = x1
- x1 = x_new
- def newton(fcn, df, x0, tol, option):
- c = 1
- if option == 0:
- for x in range(4):
- x1 = x0 - ((fcn(x0)) / (df(x0)))
- outputs_comparison.append(x1)
- print("Iteration", x+1,": ", x1)
- x0 = x1
- elif option == 1:
- while True:
- x1 = x0 - ((fcn(x0)) / (df(x0)))
- outputs_comparison.append(x1)
- print("Iteration", c,": ", x1)
- c += 1
- if abs(x0 - x1) <= tol:
- break
- x0 = x1
- outputs = []
- outputs_comparison = []
- print("\n---------")
- print("PROBLEM 1")
- print("---------\n")
- print("SECANT")
- print("------\n")
- print("x0 :", 0.6)
- print("x1 :", 0.7)
- secant(PROBLEM1_EQ, 0.4, 0.5, 1, 0)
- rate_of_convergence(outputs)
- print("\nNEWTON")
- print("------\n")
- newton(PROBLEM1_EQ, PROBLEM1_DERIV_EQ, 0.4, 10e-6, 0)
- rate_of_convergence(outputs_comparison)
- outputs = []
- outputs_comparison = []
- print("\n---------")
- print("PROBLEM 5")
- print("---------\n")
- print("SECANT")
- print("------\n")
- print("---")
- print("(i)")
- print("---")
- print("x0 :", 0)
- print("x1 :", 0.2)
- secant(PROBLEM5_EQ, 0, 0.2, 2, 10e-10)
- rate_of_convergence(outputs)
- outputs = []
- outputs_comparison = []
- print("----")
- print("(ii)")
- print("----")
- print("x0 :", -0.2)
- print("x1 :", 0)
- secant(PROBLEM5_EQ, -0.2, 0, 2, 10e-10)
- rate_of_convergence(outputs)
- outputs = []
- outputs_comparison = []
- print("-----")
- print("(iii)")
- print("-----")
- print("x0 :", 8)
- print("x1 :", 10)
- secant(PROBLEM5_EQ, 8, 10, 2, 10e-10)
- rate_of_convergence(outputs)
- print("\nNEWTON")
- print("------\n")
- print("---")
- print("(i)")
- print("---")
- newton(PROBLEM5_EQ, PROBLEM5_DERIV_EQ, -0.2, 10e-10, 0)
- print("----")
- print("(ii)")
- print("----")
- newton(PROBLEM5_EQ, PROBLEM5_DERIV_EQ, 0.3, 10e-10, 0)
- print("-----")
- print("(iii)")
- print("-----")
- newton(PROBLEM5_EQ, PROBLEM5_DERIV_EQ, 8, 10e-10, 0)
- outputs = []
- outputs_comparison = []
- print("\n---------")
- print("PROBLEM 7")
- print("---------\n")
- print("x0 :", 300)
- print("x1 :", 400)
- secant(PROBLEM7_EQ, 300, 400, 1, 10e-12)
- rate_of_convergence(outputs)
- newton(PROBLEM7_EQ, PROBLEM7_EQ_DER, 340, 10e-6, 0)
- rate_of_convergence(outputs_comparison)
- bisect(PROBLEM7_EQ, 300, 400, 10e-2)
- outputs = [335.546875, 335.7421875, 335.83984375, 335.791015625]
- rate_of_convergence(outputs)
- PROBLEM7_Integ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement