CoolFace
Apppublic

abidlabs/projectile-motion-range

sourceHugging Facemitupdated 4y agoView on Hugging Face
0likes
app.py58 linesDownload Raw Back to root
1import gradio as gr2import numpy as np3import pandas as pd4 5def plot(v, a):6    g = 9.817    theta = a / 180 * 3.148    tmax = ((2 * v) * np.sin(theta)) / g9    timemat = tmax * np.linspace(0, 1, 40)[:, None]10 11    x = (v * timemat) * np.cos(theta)12    y = ((v * timemat) * np.sin(theta)) - ((0.5 * g) * (timemat**2))13 14    trajectory = pd.DataFrame({'x': x.flatten(), 'y': y.flatten()})15    return trajectory16 17 18with gr.Blocks() as demo:19    gr.Markdown(r"""20    # Range of a Projectile21    22    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:23    24    """)25 26    with gr.Row():27        speed = gr.Slider(1, 30, 25, label="Speed")28        angle = gr.Slider(0, 90, 45, label="Angle")29    30    output = gr.ScatterPlot(None, 'x', 'y')31    speed.change(plot, [speed, angle], output)32    angle.change(plot, [speed, angle], output)33    demo.load(plot, [speed, angle], output)    34    35    gr.Markdown(r"""36    37    The horizontal displacement of a projectile at any time can be calculated using the following kinematic equation:    38    39    $x = x_0 + v_0 \cos \theta \cdot t$40    41    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.42    43    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:44    45    $y = y_0 + v_0 \sin \theta \cdot t - \frac{1}{2}gt^2$46    47    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.48    49    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:50    51    $0 = y_0 + v_0 \sin \theta \cdot t - \frac{1}{2}gt^2$52    53    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:54    55    $R = v_0^2 \cdot \frac{\sin(2\theta)}{g}$    56    """)57    58demo.launch()