0

For my dynamics course I am tasked with writing a python code that will plot the trajectory of a position vector from when it starts on the ground to when it lands on the ground. I currently have my code create a linear space from the two zero values that I calculated by hand, but I want to code that in. Because I also need to create velocity vectors on the trajectory, I have the position vector broken into its x and y components. I have looked into xlim and this thread, but couldn't figure out how to implement them. I'm fairly new to python and coding in general, so I'm still trying to learn how things work.

import numpy as np
import matplotlib.pyplot as plt


#creates a function that returns the x component
def re10(x):
    r1 =  0.05*x
    return r1

#creates a function that returns the y component
def re20(x):
    r2 =  -4.91*(x**2) + 30*x + 100
    return r2


#Calculates the two zeroes of the trajectory
tmin = (-30 + np.sqrt(30**2 -4*-4.91*100))/(2*-4.91)
tmax = (-30 - np.sqrt(30**2 -4*-4.91*100))/(2*-4.91)


#Initializing time space
t = np.linspace(tmin, tmax, 100)


#Plot
plt.plot(re10(t), re20(t)) #(x, y)
jared
  • 4,165
  • 1
  • 8
  • 31
  • why you don't use [simpy](https://simpy.readthedocs.io/en/latest/) ? Python and Symbolic equations are really powerful when used together! – RegularNormalDayGuy Sep 27 '19 at 14:04
  • I have never heard of simpy. – jared Sep 27 '19 at 14:05
  • give me a couple secs :) – RegularNormalDayGuy Sep 27 '19 at 14:07
  • My mistake, you can use `numpy` to find zeroes of your equation, and you actually already have it based from your code, so just use np.roots to find your zeroes! – RegularNormalDayGuy Sep 27 '19 at 14:28
  • 2
    Some unasked-for coding advice: I strongly suggest you avoid [magic numbers](https://en.wikipedia.org/wiki/Magic_number_%28programming%29) in your code. For instance, put `g=9.81` somewhere at the top then use `0.5*g` instead of `4.91` whenever you need it. This will help you (and others) considerably when reading the code some time after writing it. – Leporello Sep 27 '19 at 15:01
  • The reason I use -4.91 is because the given position vector is: r(t) = (0.05*t)x_1 + (-4.91t^2 + 30t + 100)x_2 – jared Sep 27 '19 at 20:07

1 Answers1

1

You can easily find the zeroes of a funtion using numpy library.

First, install it. Open a cmd console and write pip install numpy. Then, write this code in your script:

import numpy
re20 = [-4.91, 30, 100]
zeroes = numpy.roots(coeff)
print(zeroes[0])
print(zeroes[1])

As you will see when running the script from console (or you IDE), numpy.roots(function) will return you the zeroes of your function, as an array. That is why you use the [] operator to access each one of them (take note that in programming, an array's first element will be at index 0).

To use it directly into your code, you can do:

tmin = zeroes[0]
tmax = zeroes[1]

Simpy is for symbolic operations, it is pretty powerful, but you don't need it for this, my mistake.

Hope you have fun with Python, it's a cool language !

RegularNormalDayGuy
  • 685
  • 1
  • 8
  • 25