Last active
April 18, 2023 21:15
-
-
Save thotypous/a1adf9037483117fe3eae2b8c53314da to your computer and use it in GitHub Desktop.
Cálculo do comprimento ideal de uma rede de dormir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from sympy import sinh, acosh, sqrt, nsolve, Symbol, Float | |
def calcular_rede(altura_rede, altura_ganchos, distancia_ganchos): | |
H = distancia_ganchos | |
h = altura_ganchos - altura_rede | |
a = Symbol('a') | |
L = Symbol('L') | |
# https://en.wikipedia.org/wiki/Catenary#Determining_parameters | |
eqs = ( | |
2*a*sinh(H/(2*a)) - L, | |
2*a*acosh((h+a)/a) - H, | |
) | |
a, L = nsolve(eqs, (a, L), (1, 1)) | |
hmin = altura_ganchos - sqrt(L**2/4 - distancia_ganchos**2/4) | |
return a, L, hmin | |
def plotar_rede(altura_rede, altura_ganchos, distancia_ganchos, a, hmin): | |
import matplotlib.pyplot as plt | |
import numpy as np | |
x = np.linspace(-distancia_ganchos/2, distancia_ganchos/2, 1024) | |
y = altura_rede - a + a*np.cosh(x/a) | |
plt.plot(x, y, 'g') | |
plt.plot([-distancia_ganchos/2, 0, distancia_ganchos/2], | |
[altura_ganchos, hmin, altura_ganchos], 'r') | |
plt.xlim([-distancia_ganchos/2, distancia_ganchos/2]) | |
plt.ylim([0, altura_ganchos]) | |
plt.tight_layout() | |
plt.show() | |
if __name__ == '__main__': | |
import argparse | |
import json | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--altura_rede', type=Float, default=Float('0.60')) | |
parser.add_argument('--plot', action=argparse.BooleanOptionalAction) | |
parser.add_argument('altura_ganchos', type=Float) | |
parser.add_argument('distancia_ganchos', type=Float) | |
args = parser.parse_args() | |
a, L, hmin = calcular_rede( | |
args.altura_rede, | |
args.altura_ganchos, | |
args.distancia_ganchos) | |
print(json.dumps({ | |
"comprimento": float(L), | |
"altura_minima": float(hmin) | |
}, indent=4)) | |
if args.plot: | |
plotar_rede(float(args.altura_rede), | |
float(args.altura_ganchos), | |
float(args.distancia_ganchos), | |
float(a), | |
float(hmin)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment