Various body price for python animation

Hello so I am looking to animate a 3 frame gravity downside factor. I were given it to kinda paintings however since I used adaptive step dimension, every time the our bodies get nearer in combination, they decelerate. I am not too positive tips on how to repair this. I regarded into visible bundle however I do not truly comprehend it. Does any person have any concept?

The next is my code

#~~~IMPORTS~~~#

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

from IPython.show import set_matplotlib_formats

from IPython.show import HTML

set_matplotlib_formats(‘svg’, high quality=50)

plt.determine(facecolor=’white’)

#thers’s six equation dr/dt = v and dv/dt = gm~~~~~~

​

#—-CREATE FUNCTION BASED ON FIRST-ORDER EQUATION—-#

def f(r,t): #SIX INPUTS

x1o = r[0]

y1o = r[1]

vx1o = r[2]

vy1o = r[3]

​

x2o = r[4]

y2o = r[5]

vx2o = r[6]

vy2o = r[7]

​

x3o = r[8]

y3o = r[9]

vx3o = r[10]

vy3o = r[11]

​

x11 = vx1o

y11 = vy1o

x21 = vx2o

y21 = vy2o

x31 = vx3o

y31 = vy3o

​

ihatethis1 = np.sqrt((x2o-x1o)**2+(y2o-y1o)**2)

ihatethis2 = np.sqrt((x3o-x1o)**2+(y3o-y1o)**2)

ihatethis4 = np.sqrt((x2o-x3o)**2+(y2o-y3o)**2)

​

vx11 = G*m2*((x2o-x1o)/ihatethis1**3) + G*m3*((x3o-x1o)/ihatethis2**3)

vy11 = G*m2*((y2o-y1o)/ihatethis1**3) + G*m3*((y3o-y1o)/ihatethis2**3)

​

vx21 = G*m1*((x1o-x2o)/ihatethis1**3) + G*m3*((x3o-x2o)/ihatethis4**3)

vy21 = G*m1*((y1o-y2o)/ihatethis1**3) + G*m3*((y3o-y2o)/ihatethis4**3)

​

vx31 = G*m1*((x1o-x3o)/ihatethis2**3) + G*m2*((x2o-x3o)/ihatethis4**3)

vy31 = G*m1*((y1o-y3o)/ihatethis2**3) + G*m2*((y2o-y3o)/ihatethis4**3)

​

go back np.array([x11,y11,vx11,vy11,x21,y21,vx21,vy21,x31,y31,vx31,vy31])

def RKstep(r0,t0,dt):

k1 = dt*f(r0,t0)

k2 = dt*f(r0 + 0.5*k1,t0+0.5*dt)

k3 = dt*f(r0 + 0.5*k2,t0+0.5*dt)

k4 = dt*f(r0 + k3,t0+dt)

r1 = r0 + (k1+ 2*k2 + 2*k3 + k4)/6

go back r1

​

def adaptivestep(r0,t0,dt):

r_half = RKstep(r0,t0,0.5*dt)

r_1 = RKstep(r_half,t0+0.5*dt,0.5*dt)

r_2 = RKstep(r0,t0,dt)

epsilon = 1.0e-5

trytofindmax = np.array ( [ abs(r_1[0]-r_2[0]), abs(r_1[1]-r_2[1]),abs(r_1[4]-r_2[4]),abs(r_1[5]-r_2[5]),abs(r_1[8]-r_2[8]),abs(r_1[9]-r_2[9])] )

rho = 30*epsilon*dt/trytofindmax.max() #exchange r to x and y np.max array of the every plant x and y variations

dt_ideal = dt*rho**0.25

​

is_acc = 1

if rho < 1:

is_acc = 0

# We will additionally test that our step dimension is not expanding via greater than double

if dt_ideal > 2.0*dt:

dt_ideal = 2.0*dt

# And test that our step dimension is not more than our max allowed price

if dt_ideal > dtmax:

dt_ideal = dtmax

# in spite of everything we will be able to make a listing containing the brand new state (r_1) and step dimension

# dt_ideal

state_dt = [r_1, dt_ideal, is_acc]

go back state_dt

&#x200B;

#~~~CONSTANT~~~#

G = 1;

m1 = 150;

m2 = 200;

m3 = 250;

&#x200B;

#~~~INITIAL POSITION IN X AND Y FOR EACH MASS~~~#

#r1o = (3,1);

#r2o = (-1,-2);

#r3o = (-1, 1);

x1o = 3;

y1o = 1;

vy1o = 0;

vx1o = 0;

&#x200B;

x2o = -1;

y2o = -2;

vy2o = 0;

vx2o = 0;

x3o = -1;

y3o = 1;

vy3o = 0;

vx3o = 0;

r0 = np.array([x1o,y1o,vx1o,vy1o,x2o,y2o,vx2o,vy2o,x3o,y3o,vx3o,vy3o])

#~~~TIME STUF~~~#

”’to = 0.0;

tmax = 2;

nstep = 100;

dt = (tmax – to)/nstep;

t = np.linspace(to, tmax, nstep);”’

t0 = 0.0

nstep = 1000

dtmax = .06

dt = 0.03

dt_noa = dtmax/10.0

t_current = t0

t = []

# we additionally need instances for the fundamental Runge-Kutta approach

# (noa as in no adaptive step dimension)

t_current_noa = t0

t_noa = []

r_rk_noa = np.empty([12,nstep])

r_rk = np.empty([12,nstep]);

is_acc = 0

for ok in vary(nstep):

if ok == 0:

# Assign preliminary values

r_rk[:,k] = r0

r_rk_noa[:,k] = r0

else:

# carry out the fundamental Runge-Kutta

r_rk_noa[:,k] = RKstep(r_rk_noa[:,k-1],t_current_noa,dt_noa)

# carry out the adaptive step dimension

is_acc = 0

whilst is_acc == 0:

# repeat the method if the step dimension was once too huge and the outcome

# didn’t meet the accuracy threshold

next_step = adaptivestep(r_rk[:,k-1],t_current,dt)

r_rk[:,k] = next_step[0]

dt = next_step[1]

is_acc = next_step[2]

# replace the present instances and upload the ones instances to the time lists

t.append(t_current)

t_current += dt

t_noa.append(t_current_noa)

t_current_noa += dt_noa

”’for ok in vary(nstep):

if ok == 0:

r_rk[k] = r0

else:

r_rk[k] = RKstep(r_rk[k-1],t[k],dt)”’

x_11_rk = r_rk[0]

y_11_rk = r_rk[1]

vx_11_rk = r_rk[2]

vy_11_rk = r_rk[3]

&#x200B;

x_21_rk = r_rk[4]

y_21_rk = r_rk[5]

vx_21_rk = r_rk[6]

vy_21_rk = r_rk[7]

&#x200B;

x_31_rk = r_rk[8]

y_31_rk = r_rk[9]

vx_31_rk = r_rk[10]

vy_31_rk = r_rk[11]

”’

x_11_rk_noa = r_rk_noa[0]

y_11_rk_noa = r_rk_noa[1]

vx_11_rk_noa = r_rk_noa[2]

vy_11_rk_noa = r_rk_noa[3]

&#x200B;

x_21_rk_noa = r_rk_noa[4]

y_21_rk_noa = r_rk_noa[5]

vx_21_rk_noa = r_rk_noa[6]

vy_21_rk_noa = r_rk_noa[7]

&#x200B;

x_31_rk_noa = r_rk_noa[8]

y_31_rk_noa = r_rk_noa[9]

vx_31_rk_noa = r_rk_noa[10]

vy_31_rk_noa = r_rk_noa[11]

t = np.array(t)

t_noa = np.array(t_noa)”’

#====ANIMATION====#

fig, ax = plt.subplots()

# Set the boundaries of our plot.

ax.set_xlim(( -20, 20))

ax.set_ylim((-20, 20))

#ax.set_aspect(1)

# Create a marker. That is the purpose we’re going to transfer round

line1, = ax.plot([], [], ‘r.’,ms=10)

line2, = ax.plot([], [], ‘b.’,ms=12)

line3, = ax.plot([], [], ‘g.’,ms=14)

def init():

line1.set_data([], [])

line2.set_data([], [])

line3.set_data([], [])

go back (line1,line2,line3,)

def animate(n):

x11 = x_11_rk[n]

y11 = y_11_rk[n]

&#x200B;

x21 = x_21_rk[n]

y21 = y_21_rk[n]

&#x200B;

x31 = x_31_rk[n]

y31 = y_31_rk[n]

&#x200B;

line1.set_data(x11,y11)

line2.set_data(x21,y21)

line3.set_data(x31,y31)

go back (line1,line2,line3,)

#loop via time

anim = animation.FuncAnimation(fig, animate, init_func=init,

frames=len(t), period=40,

blit=True)

HTML(anim.to_jshtml())



View Reddit via TheDoldrumAreaView Supply

You may also like...

WP2Social Auto Publish Powered By : XYZScripts.com