Moortiii

Linjeoblig 1 - Matte 2

Feb 16th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1. import math
  2.  
  3.  
  4. def f(x):
  5.     return x**3 - 3
  6.  
  7.  
  8. def df(x):
  9.     return 3 * x**2
  10.  
  11.  
  12. def g(x):
  13.     return 5*x + math.log(x, math.e) - 10000
  14.  
  15.  
  16. def dg(x):
  17.     return 5 + 1/x
  18.  
  19.  
  20. def h(x):
  21.     return 2 - x**2 - math.sin(x)
  22.  
  23.  
  24. def dh(x):
  25.     return -2*x - math.cos(x)
  26.  
  27.  
  28. def j(x):
  29.     return x**2 - 10
  30.  
  31.  
  32. def dj(x):
  33.     return 2*x
  34.  
  35.  
  36. def k(x):
  37.     return math.log((x**2 + 1), math.e) - math.e**(0.4*x)*math.cos(math.pi * x)
  38.  
  39.  
  40. def dk(x):
  41.     return math.pi * math.e**((2 * x) / 5) * math.sin(math.pi * x) - (((2 * math.e**((2 * x) / 5)) * math.cos(math.pi * x)) / 5) + ((2 * x) / (x**2 + 1))
  42.  
  43.  
  44. def calc_relative_error(current_error, last_error):
  45.     return abs((current_error - last_error) / current_error)
  46.  
  47.  
  48. def functions(function_name, x):
  49.     if function_name == "f":
  50.         return f(x)
  51.     if function_name == "df":
  52.         return df(x)
  53.     if function_name == "g":
  54.         return g(x)
  55.     if function_name == "dg":
  56.         return dg(x)
  57.     if function_name == "h":
  58.         return h(x)
  59.     if function_name == "dh":
  60.         return dh(x)
  61.     if function_name == "j":
  62.         return j(x)
  63.     if function_name == "dj":
  64.         return dj(x)
  65.     if function_name == "k":
  66.         return k(x)
  67.     if function_name == "dk":
  68.         return dk(x)
  69.  
  70.  
  71. def newton_rhapson(x, n, tol, function_name, derivative):
  72.     for i in range(n):
  73.         if functions(function_name, x) == 0:
  74.             return x
  75.         if functions(derivative, x) == 0:
  76.             break
  77.  
  78.         last_estimate = x
  79.         x = x - (functions(function_name, x) / functions(derivative, x))
  80.         current_estimate = x
  81.         relative_error = calc_relative_error(current_estimate, last_estimate)
  82.  
  83.         if relative_error <= tol:
  84.             return current_estimate
  85.     return -1
  86.  
  87.  
  88. # Function specific variables
  89. f_start = 2.5
  90. g_start = 3
  91. h_start = -2
  92. h2_start = 3
  93. j_start = 3
  94. k_start = -2
  95.  
  96. root_f = newton_rhapson(f_start, 10, 1.E-1, "f", "df")
  97. root_g = newton_rhapson(g_start, 100, 1.E-9, "g", "dg")
  98. root_h = newton_rhapson(h_start, 100, 1.E-9, "h", "dh")
  99. root_h2 = newton_rhapson(h2_start, 100, 1.E-9, "h", "dh")
  100. root_j = newton_rhapson(j_start, 100, 1.E-9, "j", "dj")
  101. root_k = newton_rhapson(k_start, 100, 1.E-9, "k", "dk")
  102.  
  103. print(root_f)
  104. print(root_g)
  105. print(root_h)
  106. print(root_h2)
  107. print(root_j)
  108. print(root_k)
Advertisement
Add Comment
Please, Sign In to add comment