2023-01-18 18:21:34 +08:00
|
|
|
import pandas as pd
|
2022-03-22 06:53:08 +08:00
|
|
|
import numpy as np
|
|
|
|
|
2022-04-12 04:18:42 +08:00
|
|
|
import gradio as gr
|
|
|
|
|
2022-03-22 06:53:08 +08:00
|
|
|
|
|
|
|
def plot(v, a):
|
|
|
|
g = 9.81
|
2022-03-25 14:03:47 +08:00
|
|
|
theta = a / 180 * 3.14
|
2022-03-22 06:53:08 +08:00
|
|
|
tmax = ((2 * v) * np.sin(theta)) / g
|
2023-01-18 18:21:34 +08:00
|
|
|
timemat = tmax * np.linspace(0, 1, 40)
|
2022-03-25 14:03:47 +08:00
|
|
|
|
|
|
|
x = (v * timemat) * np.cos(theta)
|
|
|
|
y = ((v * timemat) * np.sin(theta)) - ((0.5 * g) * (timemat**2))
|
2023-01-18 18:21:34 +08:00
|
|
|
df = pd.DataFrame({"x": x, "y": y})
|
|
|
|
return df
|
2022-03-22 06:53:08 +08:00
|
|
|
|
2022-03-25 14:03:47 +08:00
|
|
|
|
2022-03-23 07:54:35 +08:00
|
|
|
demo = gr.Blocks()
|
2022-03-22 06:53:08 +08:00
|
|
|
|
2022-03-23 07:54:35 +08:00
|
|
|
with demo:
|
2022-03-25 14:03:47 +08:00
|
|
|
gr.Markdown(
|
2022-11-30 03:26:17 +08:00
|
|
|
r"Let's do some kinematics! Choose the speed and angle to see the trajectory. Remember that the range $R = v_0^2 \cdot \frac{\sin(2\theta)}{g}$"
|
2022-03-25 14:03:47 +08:00
|
|
|
)
|
|
|
|
|
2022-03-22 06:53:08 +08:00
|
|
|
with gr.Row():
|
2022-05-16 14:55:35 +08:00
|
|
|
speed = gr.Slider(1, 30, 25, label="Speed")
|
|
|
|
angle = gr.Slider(0, 90, 45, label="Angle")
|
2023-01-18 18:21:34 +08:00
|
|
|
output = gr.LinePlot(
|
|
|
|
x="x",
|
|
|
|
y="y",
|
|
|
|
overlay_point=True,
|
|
|
|
tooltip=["x", "y"],
|
|
|
|
x_lim=[0, 100],
|
|
|
|
y_lim=[0, 60],
|
|
|
|
width=350,
|
|
|
|
height=300,
|
|
|
|
)
|
2022-07-19 23:16:56 +08:00
|
|
|
btn = gr.Button(value="Run")
|
2022-03-22 06:53:08 +08:00
|
|
|
btn.click(plot, [speed, angle], output)
|
|
|
|
|
2022-03-23 07:54:35 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
demo.launch()
|