DEV Community

koki
koki

Posted on • Originally published at kokiair.com

Lets simply plot some functions in python using numpy.

I am trying to build up my skills to specialize control engineering as an aerospace bachelor student. As odd as it sounds, I don´t have any programming experience yet, so I decided to build up thermal regulation control system in python as my first engineering project.
So if you´re in the same kind of situation where you don´t have any experience of programming but trying to learn, maybe my progress could be at least one of bunch of learning trajectories of python or Matlab that you can take a look at and get some insight from.
My english is not good and I´m trying to figure out literally everything from nothing, so the posts for sure will look very elementary to a lot of people but I´m gonna expose my genuine progress in this community for the sake of tracking my progress and share my experience.
And this is the very first point of my trajectory. Plotting damped oscillation function in python.

First I installed python and visual studio code in my laptop.
Once I installed them, the next thing I did was to install numpy, scipy and matplotlib.
I executed this code in command prompt.

pip install numpy scipy matplotlib

I don´t have much knowledge about those libraries but I see numpy literally everywhere. This is the library that enables you to deal with multidimensional arrays and matrices along with large collection of high-level mathematical functions to operate on these arrays, that´s what wikipedia says.
scipy is one of the extensions of the numpy? I guess? something like that.
It adds more high complex operation like linear algebra in your tool kit.
matplotlib is basically doing visualizations task.

Once I have enough tools to get started, I wrote the following code that plots damped oscillation.

import numpy as np
import matplotlib.pyplot as plt


t=np.linspace(0,10,500)
y=np.exp(-0.3*t)*np.sin(2*np.pi*t)

plt.plot(t,y)
plt.xlabel("time[s]")
plt.ylabel("response")
plt.title("Damped oscillation")
plt.grid(True)
plt.show()

Enter fullscreen mode Exit fullscreen mode

this is the plot I got.

It´s working very nice.

And next thin I did was to associate this kind of plotting with physics. I decided to plot trajectories of thrown ball in the air with initial velocity v0 and angle theta.

the code is following

import numpy as np
import matplotlib.pyplot as plt



#first condition about v0 and theta
v0=20
theta=1/6*np.pi

#second condition about v0_2 and theta_2
v0_2=10
theta_2=1/3*np.pi


#gravitational accelaration value is constant
g=9.8


#first trajectory using first set

t_flight=2*v0*np.sin(theta)/g

t=np.linspace(0,t_flight,500)
x=v0*np.cos(theta)*t
y=v0*np.sin(theta)*t-(1/2)*g*t**2

#second trajectory using second set 

t_flight2=2*v0_2*np.sin(theta_2)/g    #t_flight changes because v0 changes to v0_2
t2=np.linspace(0,t_flight2,500)   #we need second medium value which we define as t2
x_2=v0_2*np.cos(theta_2)*t2
y_2=v0_2*np.sin(theta_2)*t2-(1/2)*g*t2**2


#plot the first trajectory 
plt.plot(x,y,label="1st intitial values")
plt.xlabel("x")
plt.ylabel("y")
plt.title("trajectory of thrown ball")
plt.grid(True)


#plot the second trajectory
plt.plot(x_2,y_2, label="2nd initial values")


#put the labels on the graph and show
plt.legend()
plt.show()

Enter fullscreen mode Exit fullscreen mode

this is the plot I got

I created two initial conditions.
And I think we have four vectors in this code, (x,y,x_2,y_2)
and as parametric values, we have (t, t2).
x and x_2 have constant distance between next values.
the difference between nth value and n+1 value in the vector x is the same for all n.
same thing for x_2 vector.
in other words, x and x_2 are linearly related to t and t2.

In the plot, the two vectors kinda merged together in the same scale.
I think that´s what´s happening.

y and y_2 are a little bit tricky. y doesn´t have linear relation with respect to t and t2 anymore. they have quadratic relationships.
but one thing which is important to keep in mind is that each entity in y is corresponding to each x in the same slot.
x=[x1,x2,x3,x4,,,,xn]
y=[y1,y2,y3,y4,,,,yn]
x and y have the same size and when the code plots the trajectory, it´s taking pair of (x1,y1), (x2,y2),, and so on.
same goes to x_2 and y_2.

Okay that´s all I wanted to write down in order to organize my thoughts.

I am sorry for the messa sentences and if I have any misunderstanding, I´d appreciate if you could correct me.

Thanks

Top comments (0)