Advertisement
ivodevweb

Untitled

Jan 19th, 2023
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | None | 0 0
  1. import streamlit as st
  2. import matplotlib.pyplot as plt
  3. import pandas as pd
  4. import math
  5.  
  6. st.set_option('deprecation.showPyplotGlobalUse', False)
  7. st.header("EfolioB de Fisica")
  8. st.title("Simulador Grafico de um Pendulo ")
  9.  
  10. st.sidebar.title("Valores a Simular:")
  11. M = st.sidebar.slider("Massa da esfera (g)", 1.0, 10.0, 2.6)
  12. L = st.sidebar.slider("Comprimento do fio (m)", 0.5, 3.0, 1.0)
  13. R = st.sidebar.slider("Raio da esfera (cm)", 1.0, 20.0, 30.0)
  14.  
  15. def run_simulation():
  16.     # constants
  17.     ra = R/100
  18.     cd = 0.1
  19.     rho = 1.28
  20.     g = 9.81
  21.     m = M/1000
  22.     A = math.pi * ra**2
  23.     b = 1/2 * rho * cd * A
  24.  
  25.     theta0 = 0.05
  26.     w0 = 0.0
  27.     t0 = 0.0
  28.     h = 0.1
  29.     t = [t0]
  30.     theta = [theta0]
  31.     w = [w0]
  32.     k1x_ = 0
  33.     k1v_ = 0
  34.     k2x_ = 0
  35.     k2v_ = 0
  36.     k1x = [k1x_] * len(t)
  37.     k1v = [k1v_] * len(t)
  38.     k2x = [k2x_] * len(t)
  39.     k2v = [k2v_] * len(t)
  40.  
  41.     while t[-1] < 100.0:
  42.         k1x_ = w[-1]
  43.         k1v_ = -math.copysign(1, w[-1]) * (((b*L)/m) * w[-1]**2) - (g / L) * theta[-1]
  44.         k2x_ = w[-1] + k1v_ * h
  45.         k2v_ = -math.copysign(1, w[-1] + k1v_ * h) * (((b*L)/m )* (w[-1] + k1v_ * h)**2) - (g / L) * (theta[-1] + k1x_ * h)
  46.        
  47.         theta.append(theta[-1] + ((k1x_ + k2x_) / 2.0)*h)
  48.         w.append(w[-1] + ((k1v_ + k2v_) / 2.0)*h)
  49.         t.append(t[-1] + h)
  50.         #append the new values to the k1x, k1v, k2x, k2v arrays
  51.         k1x.append(k1x_)
  52.         k1v.append(k1v_)
  53.         k2x.append(k2x_)
  54.         k2v.append(k2v_)
  55.      
  56.         fig, ax = plt.subplots()
  57.         ax.plot(t, theta, 'r')
  58.         ax.plot(t, w, 'b')
  59.         ax.legend(['theta (rad)', 'w (rad/s)'])
  60.  
  61.     ax.set_xlabel('time (s)')
  62.     ax.set_ylabel('Theta (rad), w (rad/s)')
  63.    
  64.     # Create a dataframe from the lists t, theta, w, k1x, k1v, k2x, k2v
  65.     df = pd.DataFrame({'t (s)': t, 'theta (rad)': theta, 'w (rad/s)': w, 'k1x': k1x, 'k1v': k1v, 'k2x': k2x, 'k2v': k2v})
  66.  
  67.     # Show the plot
  68.     st.pyplot(fig)
  69.    
  70.     # Show the first few rows of the dataframe
  71.     st.title("Tabela ")
  72.     st.write(df)
  73.  
  74. if st.button("Simular Grafico"):
  75.     run_simulation()
  76.    
  77.     st.text(" Realizado por: Ivo Baptista  ")
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement