Moortiii

Matte-2 Oblig 1 (Linjedel)

Feb 15th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.29 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 newton_rhapson_f(x, n, tol):
  49.     for i in range(n):
  50.         if f(x) == 0:
  51.             return x
  52.         if df(x) == 0:
  53.             break
  54.  
  55.         last_estimate = x
  56.         x = x - (f(x) / df(x))
  57.         current_estimate = x
  58.         relative_error = calc_relative_error(current_estimate, last_estimate)
  59.  
  60.         if relative_error <= tol:
  61.             return current_estimate
  62.  
  63.     return -1
  64.  
  65.  
  66. def newton_rhapson_g(x, n, tol):
  67.     for i in range(n):
  68.         if g(x) == 0:
  69.             return x
  70.         if dg(x) == 0:
  71.             break
  72.  
  73.         last_estimate = x
  74.         x = x - (g(x) / dg(x))
  75.         current_estimate = x
  76.  
  77.         relative_error = calc_relative_error(current_estimate, last_estimate)
  78.  
  79.         if relative_error <= tol:
  80.             return current_estimate
  81.  
  82.     return -1
  83.  
  84.  
  85. def newton_rhapson_h(x, n, tol):
  86.     for i in range(n):
  87.         if h(x) == 0:
  88.             return x
  89.         if dh(x) == 0:
  90.             break
  91.  
  92.         last_estimate = x
  93.         x = x - (h(x) / dh(x))
  94.         current_estimate = x
  95.  
  96.         relative_error = calc_relative_error(current_estimate, last_estimate)
  97.  
  98.         if relative_error <= tol:
  99.             return current_estimate
  100.  
  101.     return -1
  102.  
  103.  
  104. def newton_rhapson_j(x, n, tol):
  105.     for i in range(n):
  106.         if j(x) == 0:
  107.             return x
  108.         if dj(x) == 0:
  109.             break
  110.  
  111.         last_estimate = x
  112.         x = (x - (j(x) / dj(x)))
  113.         current_estimate = x
  114.  
  115.         relative_error = calc_relative_error(current_estimate, last_estimate)
  116.  
  117.         if relative_error <= tol:
  118.             return current_estimate
  119.  
  120.     return -1
  121.  
  122.  
  123. def newton_rhapson_k(x, n, tol):
  124.     for i in range(n):
  125.         if k(x) == 0:
  126.             return x
  127.         if dk(x) == 0:
  128.             break
  129.  
  130.         last_estimate = x
  131.         x = (x - (k(x) / dk(x)))
  132.         current_estimate = x
  133.  
  134.         relative_error = calc_relative_error(current_estimate, last_estimate)
  135.  
  136.         if relative_error <= tol:
  137.             return current_estimate
  138.  
  139.     return -1
  140.  
  141.  
  142. # Shared variables
  143. max_iter = 100
  144.  
  145. # Function specific variables
  146. f_x_start = 2.5
  147. g_x_start = 3
  148. h_x_start = -2
  149. h_x2_start = 3
  150. j_x_start = 3
  151. k_x_start = -2
  152.  
  153. root_f = newton_rhapson_f(f_x_start, 100, 1.E-9)
  154. root_g = newton_rhapson_g(g_x_start, 100, 1.E-9)
  155. root_h = newton_rhapson_h(h_x_start, 100, 1.E-9)
  156. root_h2 = newton_rhapson_h(h_x2_start, 100, 1.E-9)
  157. root_j = newton_rhapson_j(j_x_start, 100, 1.E-9)
  158. root_k = newton_rhapson_k(k_x_start, 100, 1.E-9)
  159.  
  160. print(root_f)
  161. print(root_g)
  162. print(root_h)
  163. print(root_h2)
  164. print(root_j)
  165. print(root_k)
Advertisement
Add Comment
Please, Sign In to add comment