Advertisement
Hustinyano

centerofmass.py

May 22nd, 2024
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import scipy.integrate as integrate
  4.  
  5. def CubicNatural(x, y):
  6.     m = x.size # m is the number of data points
  7.     n = m-1
  8.     a = np.zeros(m)
  9.     b = np.zeros(n)
  10.     c = np.zeros(m)
  11.     d = np.zeros(n)
  12.     for i in range(m):
  13.         a[i] = y[i]
  14.     h = np.zeros(n)
  15.     for i in range(n):
  16.         h[i] = x[i+1] - x[i]
  17.     u = np.zeros(n)
  18.     u[0] = 0
  19.     for i in range(1, n):
  20.         u[i] = 3*(a[i+1]-a[i])/h[i]-3*(a[i]-a[i-1])/h[i-1]
  21.     s = np.zeros(m)
  22.     z = np.zeros(m)
  23.     t = np.zeros(n)
  24.     s[0] = 1
  25.     z[0] = 0
  26.     t[0] = 0
  27.     for i in range(1, n):
  28.         s[i] = 2*(x[i+1]-x[i-1])-h[i-1]*t[i-1]
  29.         t[i] = h[i]/s[i]
  30.         z[i]=(u[i]-h[i-1]*z[i-1])/s[i]
  31.     s[m-1] = 1
  32.     z[m-1] = 0
  33.     c[m-1] = 0
  34.     for i in np.flip(np.arange(n)):
  35.         c[i] = z[i]-t[i]*c[i+1]
  36.         b[i] = (a[i+1]-a[i])/h[i]-h[i]*(c[i+1]+2*c[i])/3
  37.         d[i] = (c[i+1]-c[i])/(3*h[i])
  38.     return a, b, c, d
  39.  
  40. def CubicNaturalEval(w, x, coeff):
  41.     m = x.size
  42.     if w<x[0] or w>x[m-1]:
  43.         print('error: spline evaluated outside its domain')
  44.         return
  45.     n = m-1
  46.     p = 0
  47.     for i in range(n):
  48.         if w <= x[i+1]:
  49.             break
  50.         else:
  51.             p += 1
  52.     # p is the number of the subinterval w falls into, i.e., p=i means
  53.     # w falls into the ith subinterval $(x_i,x_{i+1}), and therefore
  54.     # the value of the spline at w is
  55.     # a_i+b_i*(w-x_i)+c_i*(w-x_i)^2+d_i*(w-x_i)^3.
  56.     a = coeff[0]
  57.     b = coeff[1]
  58.     c = coeff[2]
  59.     d = coeff[3]
  60.     return a[p]+b[p]*(w-x[p])+c[p]*(w-x[p])**2+d[p]*(w-x[p])**3
  61.  
  62. xaxis = np.linspace(0, 14.875, 100)
  63.  
  64. xi = np.array([0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, 13, 13.5, 14, 14.5, 14.875])  # Replace with your data points
  65. yi = np.array([0.44, 1.5, 2.875, 4.125, 5.5, 6.875, 8.25, 9.25, 9.375, 9.25, 8.75, 8, 7.375, 6.875, 6.375, 6.375, 6.625, 7.25, 8, 8.25, 7.5, 5.625, 5.25, 5, 5, 5.125, 5.625, 6.375, 7, 7.5, 8.25])  # Replace with your function values at x_data points
  66.  
  67. for i in CubicNatural(xi,yi):
  68.     print(i)
  69.  
  70. coeff = CubicNatural(xi, yi)
  71.  
  72. def f(x):
  73.     return CubicNaturalEval(x, xi, coeff)
  74.  
  75. def g(x):
  76.     return (f(x)**2)/2
  77.  
  78. def h(x):
  79.     return (x*f(x))
  80.  
  81. cuts = 30
  82.  
  83. #----Trapezoidal Rule Integration----
  84.  
  85. part = np.linspace(0,14.875,cuts)
  86. fpart = np.zeros(cuts)
  87. gpart = np.zeros(cuts)
  88. hpart = np.zeros(cuts)
  89.  
  90. for i in range(cuts):
  91.     fpart[i] = f(part[i])
  92.     gpart[i] = g(part[i])
  93.     hpart[i] = h(part[i])
  94.  
  95.  
  96. print("Sfdx = ", np.trapz(fpart))
  97. print("Sgdx = ", np.trapz(gpart))
  98. print("Shdx = ", np.trapz(hpart))
  99.  
  100. print("x = ", np.trapz(hpart)/np.trapz(fpart))
  101. print("y = ", np.trapz(gpart)/np.trapz(fpart))
  102.  
  103. #------------------------------------
  104.  
  105.  
  106. #Alternative version below
  107.  
  108. # Sf, err = integrate.quad(f, 0, 14.875)
  109. # Sg, err = integrate.quad(g, 0, 14.875)
  110. # Sh, err = integrate.quad(h, 0, 14.875)
  111.  
  112. # print("Sf(x)dx=", Sf)
  113. # print("Sg(x)dx=", Sg)
  114. # print("Sh(x)dx=", Sh)
  115.  
  116. # print("x = ", Sh/Sf)
  117. # print("y = ", Sg/Sf)
  118.  
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement