TroglodyteDerivations's picture
Create app.py
148c2e2 verified
raw
history blame
No virus
4.93 kB
import streamlit as st
import numpy as np
from abc import ABC, abstractmethod
import matplotlib.pyplot as plt
class RhinoDisplacement(ABC):
@abstractmethod
def displacement(self, time):
pass
class AcceleratingRhino(RhinoDisplacement):
def __init__(self, acceleration):
self.acceleration = acceleration
def displacement(self, time):
return 0.5 * self.acceleration * time**2
class RhinoMeetingTime(ABC):
@abstractmethod
def calculate_meeting_time(self, acceleration, speed, distance):
pass
class CalculateMeetingTime(RhinoMeetingTime):
def calculate_meeting_time(self, acceleration, speed, distance):
a = 0.5 * acceleration
b = speed
c = -distance
discriminant = b**2 - 4 * a * c
if discriminant < 0:
return None
time = (-b + np.sqrt(discriminant)) / (2 * a)
return time
class RhinoDisplacementPlot(ABC):
@abstractmethod
def plot_displacement(self):
pass
class DisplacementVisualization(RhinoDisplacementPlot):
def __init__(self, accelerating_rhino, constant_speed_rhino, meeting_time):
self.accelerating_rhino = accelerating_rhino
self.constant_speed_rhino = constant_speed_rhino
self.meeting_time = meeting_time
def plot_displacement(self):
fig, ax = plt.subplots()
times = np.linspace(0, self.meeting_time, 100)
disp_acc = [self.accelerating_rhino.displacement(t) for t in times]
disp_const = [self.constant_speed_rhino.displacement(t) for t in times]
ax.plot(times, disp_acc, label='Accelerating Rhino')
ax.plot(times, disp_const, label='Constant Speed Rhino')
ax.annotate('Accelerating rhino = 400 m', (self.meeting_time, disp_acc[-1]),
textcoords="offset points", xytext=(0, 10), ha='center')
ax.annotate('Constant speed Rhino = 1200 m', (self.meeting_time, disp_const[-1]),
textcoords="offset points", xytext=(0, 10), ha='center')
ax.set_xlabel('Time (s)')
ax.set_ylabel('Displacement (m)')
ax.legend()
return fig
def main():
st.title("Rhino Meeting Displacement Calculator")
st.image("rhinos_0.png", caption='Rhinos Charging Towards One Another 0')
acceleration = st.number_input("Acceleration of the first rhino (m/s^2)", value=1/8)
speed = st.number_input("Speed of the second rhino (m/s)", value=15)
distance = st.number_input("Initial distance between the rhinos (m)", value=1600)
if st.button("Generate Rhinos Charging Towards One Another Motion Graph Problem"):
st.write("Two rhinos initially 1600 m apart, begin running directly toward one another at the same time. One rhino uniformly accelerates from rest at 1/8 m.s^2, while the other rhino runs with a constant speed of 15 m/s. What is the net displacement of each rhino when they meet?")
if st.button("Formulations Required For Solving Problem"):
st.latex(r'Accelerating rhino: s_1(t) = \frac{1}{2}at^2')
st.latex(r'Constant speed rhino: s_2(t) = vt')
st.latex(r'Total Distance Covered by both rhinos: 1600 meters')
st.latex(r'Utilize form ax^2 + bx + c = 0')
st.latex(r'a = \frac{1}{2}\frac{1}{16}t^2')
st.latex(r'b = 15 m/s')
st.latex(r'\frac{1}{16}t^2 + 15t = 1600')
st.latex(r'c= -1600')
st.latex(r'\Delta = b^2 - 4ac = 15^2 - 4\frac{1}{16}1600 = 225 + 400 = 625')
st.latex(r'Solve for t: t = \frac{-15+-\sqrt(625)}{\frac{1}{8}} = \frac{-15 +- 25}{\frac{1}{8}}')
st.latex(r'Positive solution: t = \frac{10}{\frac{1}{8}} = 80 seconds')
st.latex(r'Accelerating rhino s_1(t) = s_1(80) = \frac{1}{2}\frac{1}{8}80^2 = \frac{1}{16}6400 = 400 meters')
st.latex(r'Constant speed rhino s_2(t) = s_2(80) = 15 * 80 = 1200 meters')
if st.button("Calculate"):
calc_meeting_time = CalculateMeetingTime()
time = calc_meeting_time.calculate_meeting_time(acceleration, speed, distance)
if time is not None:
accelerating_rhino = AcceleratingRhino(acceleration)
constant_speed_rhino = ConstantSpeedRhino(speed)
disp_acc = accelerating_rhino.displacement(time)
disp_const = constant_speed_rhino.displacement(time)
st.write(f"Time to meet: {time:.2f} seconds")
st.write(f"Displacement of accelerating rhino: {disp_acc:.2f} meters")
st.write(f"Displacement of constant speed rhino: {disp_const:.2f} meters")
visualization = DisplacementVisualization(accelerating_rhino, constant_speed_rhino, time)
fig = visualization.plot_displacement()
st.pyplot(fig)
else:
st.write("The rhinos will never meet under these conditions.")
st.image("rhinos_1.png", caption='Rhinos Charging Towards One Another 1')
if __name__ == "__main__":
main()