Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a graph using MatPlotLib with data from a scientific model?

To preface, I am completely new to Python and MatPlotLib, and I am working on a school project to calculate the trajectory of a satellite around the earth. I was wondering if it was possible to grab the data from every point the model calculates and put it inside the graph, plotting time on the x-axis and height on the y-axis. I tried to directly plot the two values, but it doesn't seem to appear on the graph. Is it possible to put every value from the calculations in the graph, or will I need to have a direct formula for the line to plot it in a graph.

The air resistance also changes for certain heights, so it makes it harder to make a direct formula. Here's the code, I am aware it is terribly formatted but it being my first project all I need from it is to get a t,h-graph. Thanks in advance for the help!

from typing import Any, Union

import matplotlib.pyplot as plt
import numpy as np

data = []
t = 0
dt = 10

A = 10
Cw = 2.7
h = 300000
h0 = h
G = 6.67384 * 10 ** -11
M = 5.972 * 10 ** 24
m = 209.4 * A ** (3 / 2)

r = 6371000 + h
v: Union[float, Any] = (G * M / r) ** .5

vx = v
vy = 0
Px = 0
Py = r

while 0 < h < h0 + 100000:
    # for _ in range(2592000):
    t = t + dt

    r = (Px ** 2 + Py ** 2) ** .5
    Fg = G * M * m / r ** 2
    Fgx = -Fg * Px / r
    Fgy = -Fg * Py / r

    h = r - 6371000
    if h > 600000:
        z = 1.607 * 10 ** -11 * 0.991169 ** (h / 1000)
    else:
        if h > 139000:
            z = 3.848 * 10 ** -8 * 0.978294 ** (h / 1000)
        else:
            z = 1.225 * 0.863697 ** (h / 1000)
    v = (vx ** 2 + vy ** 2) ** .5
    Fwl = 0.5 * z * Cw * A * v ** 2
    Fwlx = -Fwl * vx / v
    Fwly = -Fwl * vy / v

    ax = (Fgx + Fwlx) / m
    ay = (Fgy + Fwly) / m
    vx = vx + ax * dt
    vy = vy + ay * dt
    Px = Px + vx * dt
    Py = Py + vy * dt

data += [[h, t]]
print(h)
print(t)

fig, ax = plt.subplots()
ax.plot(t, h)

plt.show()
like image 715
TiemeVDS Avatar asked May 19 '26 06:05

TiemeVDS


1 Answers

As suggested by Michael Szczesny in the comment, you should indent data += [[h, t]] within the while loop.
Then I suggest you to plot with:

ax.plot(*zip(*data))

in order to separate h for t and get h(t) curve.
An other suggestion is to flip the order in which you store h and t in data:

data += [[h, t]]

in this way you will get a plot of the height with respect to time (and not the inverse).

Complete Code

from typing import Any, Union
import matplotlib.pyplot as plt


data = []
t = 0
dt = 10

A = 10
Cw = 2.7
h = 300000
h0 = h
G = 6.67384 * 10 ** -11
M = 5.972 * 10 ** 24
m = 209.4 * A ** (3 / 2)

r = 6371000 + h
v: Union[float, Any] = (G * M / r) ** .5

vx = v
vy = 0
Px = 0
Py = r

while 0 < h < h0 + 100000:
    # for _ in range(2592000):
    t = t + dt

    r = (Px ** 2 + Py ** 2) ** .5
    Fg = G * M * m / r ** 2
    Fgx = -Fg * Px / r
    Fgy = -Fg * Py / r

    h = r - 6371000
    if h > 600000:
        z = 1.607 * 10 ** -11 * 0.991169 ** (h / 1000)
    else:
        if h > 139000:
            z = 3.848 * 10 ** -8 * 0.978294 ** (h / 1000)
        else:
            z = 1.225 * 0.863697 ** (h / 1000)
    v = (vx ** 2 + vy ** 2) ** .5
    Fwl = 0.5 * z * Cw * A * v ** 2
    Fwlx = -Fwl * vx / v
    Fwly = -Fwl * vy / v

    ax = (Fgx + Fwlx) / m
    ay = (Fgy + Fwly) / m
    vx = vx + ax * dt
    vy = vy + ay * dt
    Px = Px + vx * dt
    Py = Py + vy * dt

    data += [[h, t]]


fig, ax = plt.subplots()
ax.plot(*zip(*data))

plt.show()

enter image description here

like image 51
Zephyr Avatar answered May 20 '26 20:05

Zephyr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!