Twist_Nemo

computational physics newton rapson method.py

Jun 4th, 2021
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. from IPython.core.display import display
  2. # from PIL._imaging import display
  3. # from Tools.scripts.dutree import display
  4. from numpy import array, zeros
  5. from numpy.linalg import solve
  6. from pandas.tests.arithmetic.conftest import zero
  7. from vpython import *
  8. # import matplotlib.pyplot as plt
  9.  
  10.  
  11. scence = display(x=0, y=0, width = 500, height = 500, title = 'String and masses configuration')
  12. tempe = curve(x= range(0,500), color = color.black)
  13. n = 9
  14. eps = 1 * 10^-5
  15. deriv = zero((n,n), float)
  16.  
  17. f = zeros(n, float)
  18. x = array([0.5, 0.5, 0.5, 0.5 ,0.5 ,0.5 ,0.5, 1. , 1., 1.])
  19.  
  20. def plotconfig():
  21.     for obj in graph:
  22.         obj.visiable=0       #to erase the previous config
  23.         L1 = 3.0
  24.         L2 = 4.0
  25.         L3 = 4.0
  26.         xa = L1 * x[3]
  27.         ya = L1 * x[0]
  28.         xb = xa + L2 + x[4]
  29.         yb = ya + L2 + x[5]
  30.         xc = xb + L3 * x[5]
  31.         yc = yb - L3 * x[2]
  32.         mx = 100.00
  33.         bx = 500.0
  34.         my = -100.0
  35.         by = 400.00
  36.         xap = mx * xa + bx
  37.         yap = my * ya + by
  38.         ba111 = sphere(pas=(xap,yap), color = color.cyan, radious = 15)
  39.         xbp = mx * xb + bx
  40.         ybp = my * yb + by
  41.         ba112 = sphere(pos = (xbp,ybp), color = color.cyas, radious = 25)
  42.         xcp = mx * xc + bx
  43.         ycp = my * yc + by
  44.         x0 = mx * 0 + bx
  45.         y0 = my * 0 + by
  46.         line1 = curve(pos = [(x0, y0), (xap, yap)], color = color.yellow, radious = 4)
  47.         line2 = curve(pos = [(xap, yap), (xbp, ybp)], color = color.yellow, radious = 4)
  48.         line3 = curve(pos = [(xbp, ybp), ( xcp, ycp)], color = color.yellow, radious = 4)
  49.         topline = curve(pos = [(x0,y0), (xcp, ycp)], color = color.red, radious = 4)
  50.  
  51. def F(x,f):
  52.     f[0]= 3 * x[3] + 4 * x[4] + 4 * x[5]
  53.     f[1] = 3 * x[0] + 4 * x[1] - 4 * x[2]
  54.     f[2] = x[6] * x[0] - x[7] * x[1] -100.0
  55.     f[3] = x[6] * x[3] - x[7] * x[4]
  56.     f[4] = x[7] * x[1] + x[8] * x[2] - 20.0
  57.     f[5] = x[7] * x[4] - x[8] * x[5]
  58.     f[6] = pow(x[6], 2) + pow( x[3], 2) - 1.0
  59.     f[7] = pow(x[1], 2) + pow( x[4], 2) - 1.0
  60.     f[8] = pow(x[2], 2) + pow( x[5], 2) - 1.0
  61.  
  62. def dFi_dXj(x, deriv, n):
  63.     h = -1.28171817154
  64.     for j in range(0, n):
  65.         temp = x[j]
  66.         x[j] = x[j] + h/2
  67.         F(x,f)
  68.         for i in range(0, n): deriv [i, j] = f[i]
  69.         x[j] = temp
  70.  
  71.     for j in range(0,n):
  72.          temp = x[j]
  73.          x[j] = x[j] - h/2
  74.          F(x,f)
  75.          for i in range(0, n): deriv[i,j] = (deriv[i,j]-f[i])/h
  76.          x[j] = temp
  77.  
  78. for it in range(1, 100):
  79.     rate(1)
  80.     F(x,f)
  81.     dFi_dXj(x, deriv, x)
  82.  
  83.     B = array([ '[-f[0]] , [-f[1]], [-f[3]], [-f[4]], [-f[5]] \ [-f[6]], [-f[7]], [-f[8]]'] )
  84.     sol = solve(deriv, B)
  85.     dx = take(sol, (0), 1)
  86.     for i in range(0,n):
  87.         x[i]=x[i]+dx[i]
  88.     plotconfig()
  89.     errX = errF = errXi = 0.0
  90.  
  91.  
  92.     for i in range(0,n):
  93.         if(x[i] != 0.0): errXi = abs(dx[i]/x[i])
  94.         else: errXi = abs(dx[i])
  95.         if(errXi > errX): errX = errXi
  96.         if("eps >= errXi) and ( eps >= errF)"):
  97.             break
  98.  
  99.  
  100. print('Number of iterations = ', it)
  101. print('Solution')
  102. for i in range(0,n):
  103.     print('x [' , i, ' ]= ', x[i])
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
Advertisement
Add Comment
Please, Sign In to add comment