File size: 5,374 Bytes
148c2e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
407e2da
 
 
 
 
 
 
148c2e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a475f7a
148c2e2
3a7dbcd
148c2e2
 
 
 
 
 
 
 
2a56e07
a62ff13
2a56e07
148c2e2
 
4ce10b2
148c2e2
 
313732c
9988893
313732c
9988893
313732c
9988893
313732c
9988893
313732c
abb107c
313732c
148c2e2
313732c
148c2e2
313732c
148c2e2
313732c
148c2e2
313732c
9988893
313732c
9988893
313732c
9988893
313732c
9988893
313732c
148c2e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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 ConstantSpeedRhino(RhinoDisplacement):
    def __init__(self, speed):
        self.speed = speed 

    def displacement(self, time):
        return self.speed * time 

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=(-60, 10), ha='center')
        ax.annotate('Constant speed Rhino = 1200 m', (self.meeting_time, disp_const[-1]),
            textcoords="offset points", xytext=(-80, -20), 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 = 1/8 # "Acceleration of the first rhino (m/s^2)"
    speed = 15  # "Speed of the second rhino (m/s)"
    distance = 1600 # "Initial distance between the rhinos (m)"

    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'AR: s_1(t) = \frac{1}{2}at^2') # accelerating rhino
        st.write("")
        st.latex(r'CSR: s_2(t) = vt') # constant speed rhino
        st.write("")
        st.latex(r'TDCBBR: 1600 meters') # total distance covered by both rhinos 
        st.write("")
        st.latex(r'Utilize: ax^2 + bx + c = 0')
        st.write("")
        st.latex(r'a = \frac{1}{2}\frac{1}{8}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: t = \frac{-15+-\sqrt(625)}{\frac{1}{8}} = \frac{-15 +- 25}{\frac{1}{8}}')
        st.write("")
        st.latex(r'Psol: t = \frac{10}{\frac{1}{8}} = 80 seconds') # positive solution
        st.write("")
        st.latex(r's_1(t) = s_1(80) = \frac{1}{2}\frac{1}{8}80^2 = \frac{1}{16}6400 = 400 meters') # accelerating rhino AR s_1(t)
        st.write("")
        st.latex(r's_2(t) = s_2(80) = 15 * 80 = 1200 meters') # Constant speed rhino s_2(t)
        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()