Update 79,81,83,85,87,89,91,93,95,97,99,101,103,105 with: st.write(β### Formulations β) | st.write(ββ)
313732c
verified
import streamlit as st | |
import numpy as np | |
from abc import ABC, abstractmethod | |
import matplotlib.pyplot as plt | |
class RhinoDisplacement(ABC): | |
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 ConstantSpeedRhino(RhinoDisplacement): | |
def __init__(self, speed): | |
self.speed = speed | |
def displacement(self, time): | |
return self.speed * time | |
class RhinoMeetingTime(ABC): | |
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): | |
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.write("### Formulations") | |
st.latex(r'Accelerating_rhino: s_1(t) = \frac{1}{2}at^2') | |
st.write("") | |
st.latex(r'Constant_speed_rhino: s_2(t) = vt') | |
st.write("") | |
st.latex(r'Total_Distance_Covered_by_both_rhinos: 1600 meters') | |
st.write("") | |
st.latex(r'Utilize_form: ax^2 + bx + c = 0') | |
st.write("") | |
st.latex(r'a = \frac{1}{2}\frac{1}{16}t^2') | |
st.write("") | |
st.latex(r'b = 15 m/s') | |
st.write("") | |
st.latex(r'\frac{1}{16}t^2 + 15t = 1600') | |
st.write("") | |
st.latex(r'c= -1600') | |
st.write("") | |
st.latex(r'\Delta = b^2 - 4ac = 15^2 - 4\frac{1}{16}1600 = 225 + 400 = 625') | |
st.write("") | |
st.latex(r'Solve_for_t: t = \frac{-15+-\sqrt(625)}{\frac{1}{8}} = \frac{-15 +- 25}{\frac{1}{8}}') | |
st.write("") | |
st.latex(r'Positive_solution: t = \frac{10}{\frac{1}{8}} = 80 seconds') | |
st.write("") | |
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.write("") | |
st.latex(r'Constant_speed_rhino s_2(t) = s_2(80) = 15 * 80 = 1200 meters') | |
st.write("") | |
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() |