Spaces:
Runtime error
Runtime error
import gradio as gr | |
import numpy as np | |
import pandas as pd | |
def plot(v, a): | |
g = 9.81 | |
theta = a / 180 * 3.14 | |
tmax = ((2 * v) * np.sin(theta)) / g | |
timemat = tmax * np.linspace(0, 1, 40)[:, None] | |
x = (v * timemat) * np.cos(theta) | |
y = ((v * timemat) * np.sin(theta)) - ((0.5 * g) * (timemat**2)) | |
trajectory = pd.DataFrame({'x': x.flatten(), 'y': y.flatten()}) | |
return trajectory | |
with gr.Blocks() as demo: | |
gr.Markdown(r""" | |
# Range of a Projectile | |
The range of a projectile is the horizontal distance it travels during its motion. You can see how the range changes as you adjust the initial speed and angle of the projectile: | |
""") | |
with gr.Row(): | |
speed = gr.Slider(1, 30, 25, label="Speed") | |
angle = gr.Slider(0, 90, 45, label="Angle") | |
output = gr.ScatterPlot(None, 'x', 'y') | |
speed.change(plot, [speed, angle], output) | |
angle.change(plot, [speed, angle], output) | |
demo.load(plot, [speed, angle], output) | |
gr.Markdown(r""" | |
The horizontal displacement of a projectile at any time can be calculated using the following kinematic equation: | |
$x = x_0 + v_0 \cos \theta \cdot t$ | |
where $x$ is the horizontal displacement, $x_0$ is the initial horizontal position, $v_0$ is the initial velocity, $\theta$ is the angle at which the projectile is launched, and $t$ is the time. | |
To find the range of the projectile, we need to find the time at which it hits the ground (i.e., when its vertical displacement becomes zero). The vertical displacement of a projectile at any time can be calculated using the following kinematic equation: | |
$y = y_0 + v_0 \sin \theta \cdot t - \frac{1}{2}gt^2$ | |
where $y$ is the vertical displacement, $y_0$ is the initial vertical position, $v_0$ is the initial velocity, $\theta$ is the angle at which the projectile is launched, $t$ is the time, $g$ is the acceleration due to gravity, and $t$ is the time. | |
To find the time at which the projectile hits the ground, we can set the vertical displacement to zero and solve for $t$. This gives us the following equation: | |
$0 = y_0 + v_0 \sin \theta \cdot t - \frac{1}{2}gt^2$ | |
Solving for $t$, we get two possible solutions, but since the projectile will hit the ground at a later time, we need to take the positive value of $t$. Substituting this value into the equation for horizontal displacement and using a trigonometric identity, we get the following equation for the range of the projectile: | |
$R = v_0^2 \cdot \frac{\sin(2\theta)}{g}$ | |
""") | |
demo.launch() |