For a vector analysis exercise, I wrote a Python program to draw a parabola from an endpoint and a maximum height. The interesting part was finding a parameterization that behaved consistently for different coordinates.

Visualizing a Parabola with a Given Impact Point#

The inputs describe where the curve ends and how high it reaches. The corrected example below uses a parameter from zero to one, so the interval does not depend on the endpoint coordinates. It draws a geometric curve rather than simulating a flight.

Running program angle #1

Mathematical Background#

The curve starts at (0,0,0)(0,0,0), reaches height hmaxh_{max} halfway through its parameter interval, and ends at (xf,yf,0)(x_f,y_f,0). Let tt run from 0 to 1, independently of the coordinates:

x(t)=xft,y(t)=yft,z(t)=4hmaxt(1−t).x(t)=x_f t,\qquad y(t)=y_f t,\qquad z(t)=4h_{max}t(1-t).

At t=0t=0 and t=1t=1, the vertical coordinate is zero. At t=1/2t=1/2, it is hmaxh_{max}. For positive height and a nonzero horizontal displacement, this traces a parabola in a vertical plane. If both horizontal coordinates are zero, it degenerates into an up-and-down vertical path.

This is a geometric parameterization. The parameter is not a flight time in seconds, and the program does not infer a launch speed or include air resistance.

Project Overview#

The project takes a user-defined impact point (x,y)(x, y) and the maximum height (hmaxh_{max}), constructs a normalized parametric curve, and then visualizes the resulting graph in three-dimensional space.

Technologies#

  • Python
  • Libraries:
    • matplotlib
    • numpy

Dependencies#

To install the dependencies:

$ pip install -r requirements.txt

Execution#

To run the program:

$ python main.py

Users will need to input the following data:

  • xx coordinate of the impact point: any real number.
  • yy coordinate of the impact point: any real number.
  • Maximum height (hmaxh_{max}): a positive finite number. Coordinates must also be finite.

Source code#

import matplotlib.pyplot as plt
import numpy as np


def trajectory(xf, yf, hmax, samples=101):
    if not np.all(np.isfinite([xf, yf, hmax])) or hmax <= 0:
        raise ValueError("Coordinates must be finite and height must be positive.")
    if not isinstance(samples, int) or samples < 3:
        raise ValueError("Use at least three samples.")
    t = np.linspace(0.0, 1.0, samples)
    return xf * t, yf * t, hmax * (4 * t * (1 - t))


def main():
    xf = float(input("Coordenada x final: "))
    yf = float(input("Coordenada y final: "))
    hmax = float(input("Altura máxima (positiva): "))
    x, y, z = trajectory(xf, yf, hmax)

    fig = plt.figure()
    ax = fig.add_subplot(projection="3d")
    ax.plot3D(x, y, z, color="red", label="Trayectoria")
    ax.scatter([0, xf], [0, yf], [0, 0], color="black", label="Inicio y final")

    for values, set_limits in [(x, ax.set_xlim), (y, ax.set_ylim), (z, ax.set_zlim)]:
        low, high = float(values.min()), float(values.max())
        margin = max((high - low) * 0.1, 0.5)
        set_limits(low - margin, high + margin)

    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax.set_zlabel("z")
    ax.set_title("Trayectoria con parámetro normalizado")
    ax.legend()
    plt.show()


if __name__ == "__main__":
    main()

Credits#

This program was created for the Vector Analysis course at the Escuela Superior de Cómputo of the Instituto Politécnico Nacional, taught by Dr. Dárwin Gutiérrez Mejía.

May 2021


Note from 2023.#

This project was written in May 2021, however I never had the time to get to write a little about it. If you’re curious about the code and/or the original written report (which is basically the spanish version of what you just read) you can find it here:

The following screenshots show the original 2021 program. The corrected example above uses the same inputs with a normalized parameter:

Running the command: python main.py (after installing dependencies)

The output will be something like these options:

Running program angle #1

Running program angle #2