why these 2 python lists have different len's? - python

I have written the following code for adaptive step size RungeKutta RK 4th order integration method.
import numpy as np
import os
import matplotlib
from matplotlib import pyplot as plt
rhs_of_diff_Eq_str = "3 * t ** 2"
def first_derivative(t, y): # the first derivative of the function y(t)
first_derivative_value = 3 * t ** 2
return first_derivative_value
time_interval_lowerlimit = 0.0
time_interval_upperlimit = 1.0
dt = 0.01
ts = []
y = 0. # initial condition
t = 0. # initial condition
ys_step = ys_halfstep = ys_doublestep = ys = []
dy_min = 0.01
dy_max = 0.1
dt_min = 0.0001
y_tol = 0.0001
no_of_iterations = 0
while(t < 1):
no_of_iterations += 1
# for timestep = dt
k1 = first_derivative(t, y)
k2 = first_derivative(t + dt/2. , y + (dt/2.)*k1)
k3 = first_derivative(t + dt/2. , y + (dt/2.)*k2)
k4 = first_derivative(t + dt , y + dt *k3)
y_step = y + (dt/6.) * (k1 + 2*k2 + 2*k3 + k4)
ys_step.append(y_step) # for plotting y vs t, at the end of the script, after integration has finished
# for timestep = dt / 2
k1 = first_derivative(t, y)
k2 = first_derivative(t + dt/4. , y + (dt/4.)*k1)
k3 = first_derivative(t + dt/4. , y + (dt/4.)*k2)
k4 = first_derivative(t + dt/2. , y + (dt/2.)*k3)
y_halfstep = y + (dt/12.) * (k1 + 2*k2 + 2*k3 + k4)
ys_halfstep.append(y_halfstep)
# for timestep = dt * 2
k1 = first_derivative(t, y)
k2 = first_derivative(t + dt , y + dt * k1)
k3 = first_derivative(t + dt , y + dt * k2)
k4 = first_derivative(t + 2.*dt, y + 2.*dt * k3)
y_doublestep = y + (dt/3.) * (k1 + 2*k2 + 2*k3 + k4)
ys_doublestep.append(y_doublestep)
if (abs(y_step) <= y_tol): # fix the timestep to dt_min because otherwise we divide by 0 in comparisons below
if (dt != dt_min):
dt = dt_min
new_y = y_step
else: # can modify the timestep if needed
if ( (abs(y_step) > y_tol) and ( (abs(y_step - y_halfstep)/abs(y_step)) > dy_max ) ): # error is too large
dt = dt / 2.
new_y = y_halfstep
else:
if ( (abs(y_step) > y_tol) and ( (abs(y_step - y_doublestep)/abs(y_step)) < dy_min ) ) : # error too small, can increase dt
dt = 2. * dt
new_y = y_doublestep
else: # timestep is just right! keep it as it is and return y_step (i.e. the y-value computed using timestep = dt)
new_y = y_step
y = new_y
# print("y is :")
# print(y)
# print(len(y)) # error, object of type 'float' has no len()
ys.append(y)
# print("t is: ")
# print(t)
ts.append(t)
t += dt
print(len(ys)) #
print(len(ts)) #
print("no of iterations: ")
print(no_of_iterations)
plt.figure()
plt.plot(ts, ys, label='y values', color='red')
plt.xlabel('t')
plt.ylabel('y')
plt.title("RK4 adaptive step-size integration for dy/dt = f(y,t) \n" + "f(y,t) = " + rhs_of_diff_Eq_str)
plt.savefig("RK4_adaptive_step_size_results.pdf", bbox_inches='tight')
This results in error at the plotting instructions due to the 2 lists ts and ys having different number of elements.
I have been looking at the code for a while now and I don't see the reason why the ys always have 4 times the number of elements in the list ts after the script exits from the while-loop.
Can you please help me, maybe it's something obvious?
Thanks

The issues stile happen from this line ys_step = ys_halfstep = ys_doublestep = ys = [], there is a created four list but all refers the same memories and when you append the element one of that list it stile append the all that lists.
You can only change the as following:
ys_step = []
ys_halfstep = []
ys_doublestep = []
ys = []
it will worked.

Related

Drawing Poincare Section using Python

I was about to plot a Poincare section of the following DE, which is quite meaningful to have a periodic potential function V(x) = - cos(x) in this equation.
After calculating the solution using RK4 with time interval dt = 0.001, the one that python drew was as the following plot.
But according to the textbook(referred to 2E by J.M.T. Thompson and H.B. Stewart), the section would look like as
:
it has so much difference. For my personal opinion, since Poincare section does not appear as what writers draw, there must be some error in my code. However, I actually done for other forced oscillation DE, including Duffing's equation, and obtained the identical one as those in the textbook. So, I was wodering if there are some typos in the equation given by the textbook, or somewhere else. I posted my code, but might be quite messy to understand. So appreicate dealing with it.
import numpy as np
import matplotlib.pylab as plt
import matplotlib as mpl
import sys
import time
state = [1]
def print_percent_done(index, total, state, title='Please wait'):
percent_done2 = (index+1)/total*100
percent_done = round(percent_done2, 1)
print(f'\t⏳{title}: {percent_done}% done', end='\r')
if percent_done2 > 99.9 and state[0]:
print('\t✅'); state = [0]
####
no = 1
####
def multiple(n, q):
m = n; i = 0
while m >= 0:
m -= q
i += 1
return min(abs(n - (i - 1)*q), abs(i*q - n))
# system(2)
#Basic info.
filename = 'sinPotentialWell'
# a = 1
# alpha = 0.01
# w = 4
w0 = .5
n = 1000000
h = .01
t_0 = 0
x_0 = 0.1
y_0 = 0
A = [(t_0, x_0, y_0)]
def f(t, x, y):
return y
def g(t, x, y):
return -0.5*y - np.sin(x) + 1.1*np.sin(0.5*t)
for i in range(n):
t0 = A[i][0]; x0 = A[i][1]; y0 = A[i][2]
k1 = f(t0, x0, y0)
u1 = g(t0, x0, y0)
k2 = f(t0 + h/2, x0 + h*k1/2, y0 + h*u1/2)
u2 = g(t0 + h/2, x0 + h*k1/2, y0 + h*u1/2)
k3 = f(t0 + h/2, x0 + h*k2/2, y0 + h*u2/2)
u3 = g(t0 + h/2, x0 + h*k2/2, y0 + h*u2/2)
k4 = f(t0 + h, x0 + h*k3, y0 + h*u3)
u4 = g(t0 + h, x0 + h*k3, y0 + h*u3)
t = t0 + h
x = x0 + (k1 + 2*k2 + 2*k3 + k4)*h/6
y = y0 + (u1 + 2*u2 + 2*u3 + u4)*h/6
A.append([t, x, y])
if i%1000 == 0: print_percent_done(i, n, state, 'Solving given DE')
#phase diagram
print('showing 3d_(x, y, phi) graph')
PHI=[[]]; X=[[]]; Y=[[]]
PHI_period1 = []; X_period1 = []; Y_period1 = []
for i in range(n):
if w0*A[i][0]%(2*np.pi) < 1 and w0*A[i-1][0]%(2*np.pi) > 6:
PHI.append([]); X.append([]); Y.append([])
PHI_period1.append((w0*A[i][0])%(2*np.pi)); X_period1.append(A[i][1]); Y_period1.append(A[i][2])
phi_period1 = np.array(PHI_period1); x_period1 = np.array(X_period1); y_period1 = np.array(Y_period1)
print('showing Poincare Section at phi=0')
plt.plot(x_period1, y_period1, 'gs', markersize = 2)
plt.plot()
plt.title('phi=0 Poincare Section')
plt.xlabel('x'); plt.ylabel('y')
plt.show()
If you factor out some of the computation blocks, you can make the code more flexible and computations more direct. No need to reconstruct something if you can construct it in the first place. You want to catch the points where w0*t is a multiple of 2*pi, so just construct the time loops so you integrate in chunks of 2*pi/w0 and only remember the interesting points.
num_plot_points = 2000
h = .01
t,x,y = t_0,x_0,y_0
x_section,y_section = [],[]
T = 2*np.pi/w0
for k in range(num_plot_points):
t = 0;
while t < T-1.2*h:
x,y = RK4step(t,x,y,h)
t += h
x,y = RK4step(t,x,y,T-t)
if k%100 == 0: print_percent_done(k, num_plot_points, state, 'Solving given DE')
x_section.append(x); y_section.append(y)
with RK4step just containing the code of the RK4 step.
This will not solve the mystery. The veil gets lifted if you consider that x is the angle theta (of a forced pendulum with friction) on a circle. Thus to get points with the same spacial location it needs to be reduced by multiples of 2*pi. Doing that,
plt.plot([x%(2*np.pi) for x in x_section], y_section, 'gs', markersize = 2)
results in the expected plot

why is this Python while-loop terminating after 2 iterations only?

I am struggling with understanding why this while loop (below in the code) terminates after 2 iterations only.
It is from an adative-step size RungeKutta45 Fehlberg Method (https://www.uni-muenster.de/imperia/md/content/physik_tp/lectures/ss2017/numerische_Methoden_fuer_komplexe_Systeme_II/rkm-1.pdf) page 10/11.
The below results in this output:
$ python3 runge_kutta_45_adaptive_optimalstepsizes.py
error at this step: 0.0
error at this step: 1.6543612251060553e-24
no of iterations of the while loop was: 2
last time t was: 0.001
import numpy as np
import os
import matplotlib
from matplotlib import pyplot as plt
# using current y_n and t_n, finds the largest possible dt_new such that the TRUNCATION ERROR
# after 1 integration step with timestep = this new dt_new (the one we want to settle upon)
# REMAINS below some given desired accuracy epsilon_0
# ADAPTIVE STEP-SIZE CONTROL
# always change dt to dt = h_new (optimal timestep change)
rhs_of_diff_Eq_str = "3 * t ** 2"
def first_derivative(t, y): # the first derivative of the function y(t)
first_derivative_value = 3 * t ** 2
return first_derivative_value
def get_RKF4_approx(t, y, dt):
k1 = first_derivative(t, y )
k2 = first_derivative(t + dt/4. , y + dt*( (1./4.)*k1 ) )
k3 = first_derivative(t + dt*(3./8.) , y + dt*( (3./32.)*k1 + (9./32.)*k2 ) )
k4 = first_derivative(t + dt*(12./13.) , y + dt*( (1932./2197.)*k1 - (7200./2197.)*k2 + (7296./2197.)*k3 ) )
k5 = first_derivative(t + dt, y + dt*( (439./216.)*k1 - 8.*k2 + (3680./513.)*k3 - (845./4104)*k4 ) )
RKF4 = y + dt * ( (25./216)*k1 + (1408/2565.)*k3 + (2197./4104.)*k4 - (1./5.)*k5 )
return np.array([RKF4, k1, k2, k3, k4, k5])
def get_RKF5_approx_efficiently(t, y, dt, ks): # efficient ! re-uses derivative evaluations from RKF4 (previous) calculation.
# ks is a numpy array
# ks[0] is K1, ks[1] is K2, ... , ks[4] is K5
k6 = first_derivative(t + dt*(1./2.), y + dt*(-(8./27.)*ks[0] + 2.*ks[1] - (3544./2565.)*ks[2] + (1859./4104.)*ks[3] - (11./40.)*ks[4]) )
RKF5 = y + dt * ( (16./135.)*ks[0] + (6656./12825.)*ks[2] + (28561./56430.)*ks[3] - (9./50.)*ks[4] +(2./55.)*k6 )
return RKF5 # a number
ts = []
ys = []
tfinal = 10.0
nmax = 10**6
epsilon_0 = 10**(-6)
contor = 0
dt = 0.001
beta = 0.9
t = 0.0 # initial condition
y = 0.0 # initial condition
while (t < tfinal and contor < nmax):
contor += 1
container_from_RKF4method = get_RKF4_approx(t, y, dt)
RKF4 = container_from_RKF4method[0] # the RKF4 method's approximation for y_{n+1}
ks = container_from_RKF4method[1:]
RKF5 = get_RKF5_approx_efficiently(t, y, dt, ks)
error_at_this_step = abs(RKF5 - RKF4)
print("error at this step: {}".format(error_at_this_step))
if (error_at_this_step < epsilon_0 and error_at_this_step != 0.0):
# yes, step accepted! need optimal timestep
dt_new = beta * dt * (epsilon_0/error_at_this_step)**(0.25)
ts.append(t)
t += dt_new
dt = dt_new
y_new = RKF5
ys.append(y_new)
y = y_new
else:
if (error_at_this_step == 0.0): # it's perfect! keep carrying on with this timestep which gives 0 error.
ts.append(t)
t += dt
y_new = RKF5
ys.append(y_new)
y = y_new
else: # means that error_at_this_step > epsilon_0 and that error_at_this_step != 0
# no, step not accepted. reiterate step using a lower timestep
dt_new = beta * dt * (epsilon_0/error_at_this_step)**(0.2)
dt = dt_new
# no changes made to time t and y
# repeat this step (reiterate step)
# HERE THE PROBLEM SHALL BE! I DON'T KNOW WHY THE ABOVE 2 instructions are bad!
print("no of iterations of the while loop was: {}".format(contor))
ts = np.array(ts)
print("last time t was: {}".format(ts[-1]))
ys = np.array(ys)
plt.figure()
plt.plot(ts, ys, label='y values', color='red')
plt.xlabel('t')
plt.ylabel('y')
plt.title("RK45 adaptive step-size (optimal step-size always chosen) integration for dy/dt = f(y,t) \n" + "f(y,t) = " + rhs_of_diff_Eq_str)
plt.savefig("RK45_adaptive_step_size_optimal_step_size_results.pdf", bbox_inches='tight')
I have tried to look at instructions execution with breakpoint() and pressing n and/or s.
It seems that the while-loop literally stops after the 2nd iteration.
Do you see why this is the case?
Time t doesn't reach tfinal or contor=10**6-1=nmax!
The bit of the pdb debugging which shows the problem is:
> /mnt/c/Users/iusti/Desktop/runge_kutta_45_adaptive_optimalstepsizes.py(46)<module>()
-> while (t < tfinal and contor < nmax):
(Pdb) s
> /mnt/c/Users/iusti/Desktop/runge_kutta_45_adaptive_optimalstepsizes.py(79)<module>()
-> print("no of iterations of the while loop was: {}".format(contor))
(Pdb) s
[2]+ Stopped python3 runge_kutta_45_adaptive_optimalstepsizes.py
Thanks!
In the second iteration, try to print dt_new before the line t += dt_new in this block:
if (error_at_this_step < epsilon_0 and error_at_this_step != 0.0):
# yes, step accepted! need optimal timestep
dt_new = beta * dt * (epsilon_0/error_at_this_step)**(0.25)
ts.append(t)
t += dt_new
dt = dt_new
y_new = RKF5
ys.append(y_new)
y = y_new
I guess the dt_new value would be so big that adding it to t will cause t to be >= tfinal hence the while condition on the third iteration will not hold anymore which causes the termination.

How can I fix this error: TypeError: cannot unpack non-iterable int object

I'm getting TypeError: cannot unpack non-iterable int object in the line S, I, R = y of the function. I thought saving y as a matrix ([730, 3]) would solve this, it did not.
def SIRmodel(y,t):
S, I, R = y
return ([-alpha*S*I,alpha*S*I-beta*I,beta*I])
I call it below:
alpha=mygetrand(cdf,0,0.4,1.e-16)
beta = 1/11
t = np.linspace(0,730,1001) #two year time period
pop = 350000000 #population size
med_capacity = 250000
y=np.array([730,3])
x0=0
x1=0.4
def get_trajectory():
infec0 = pop*0.00001 #the initial infected
Iinit = (infec0/pop) #initial infected rate
Sinit = (pop - infec0)/pop #susceptable population
Rinit = 0
sol = rk4sys(SIRmodel,[Sinit,Iinit,Rinit],x0, x1)
hosp = np.zeros(3) #array of hospitalized
perc = [0.05, 0.1, 0.2]
return(sol)
sol = get_trajectory()
plt.plot(t,pop*sol[:,1],'r-',[0,730],[med_capacity,med_capacity],'b:');
#print(pop*soln[:,1])
The problem here is at the call sol = rk4sys(SIRmodel,[Sinit,Iinit,Rinit],x0, x1)
My rk4 is
def rk4sys(f, init, x0, x1):
h=(x0-x1)/730
vx = np.zeros(shape=(730,1))
vy = np.zeros(shape=(731,3))
vx[0] = x = x0 #pythonified assignment statements
vy[0,:] = y = init
for i in range(1, 730 + 1): #runge-kutta loop
k1 = f(x, y)
k2 = f(x + 0.5 * h, y + 0.5 * h* k1)
k3 = f(x + 0.5 * h, y + 0.5 * h* k2)
k4 = f(x + h, y + h* k3)
vx[i] = x = x0 + i * h
vy[i,:] = y = y + h*(k1 + k2 + k2 + k3 + k3 + k4) / 6.
return vx, vy
Swap the t and y values in the SIRmodel call
def SIRmodel(t,y):

Cannot get RK4 to solve for position of orbiting body in Python

I am trying to solve for the position of a body orbiting a much more massive body, using the idealization that the much more massive body doesn't move. I am trying to solve for the position in cartesian coordinates using 4th order Runge-Kutta in python.
Here is my code:
dt = .1
t = np.arange(0,10,dt)
vx = np.zeros(len(t))
vy = np.zeros(len(t))
x = np.zeros(len(t))
y = np.zeros(len(t))
vx[0] = 10 #initial x velocity
vy[0] = 10 #initial y velocity
x[0] = 10 #initial x position
y[0] = 0 #initial y position
M = 20
def fx(x,y,t): #x acceleration
return -G*M*x/((x**2+y**2)**(3/2))
def fy(x,y,t): #y acceleration
return -G*M*y/((x**2+y**2)**(3/2))
def rkx(x,y,t,dt): #runge-kutta for x
kx1 = dt * fx(x,y,t)
mx1 = dt * x
kx2 = dt * fx(x + .5*kx1, y + .5*kx1, t + .5*dt)
mx2 = dt * (x + kx1/2)
kx3 = dt * fx(x + .5*kx2, y + .5*kx2, t + .5*dt)
mx3 = dt * (x + kx2/2)
kx4 = dt * fx(x + kx3, y + x3, t + dt)
mx4 = dt * (x + kx3)
return (kx1 + 2*kx2 + 2*kx3 + kx4)/6
return (mx1 + 2*mx2 + 2*mx3 + mx4)/6
def rky(x,y,t,dt): #runge-kutta for y
ky1 = dt * fy(x,y,t)
my1 = dt * y
ky2 = dt * fy(x + .5*ky1, y + .5*ky1, t + .5*dt)
my2 = dt * (y + ky1/2)
ky3 = dt * fy(x + .5*ky2, y + .5*ky2, t + .5*dt)
my3 = dt * (y + ky2/2)
ky4 = dt * fy(x + ky3, y + ky3, t + dt)
my4 = dt * (y + ky3)
return (ky1 + 2*ky2 + 2*ky3 + ky4)/6
return (my1 + 2*my2 + 2*my3 + my4)/6
for n in range(1,len(t)): #solve using RK4 functions
vx[n] = vx[n-1] + fx(x[n-1],y[n-1],t[n-1])*dt
vy[n] = vy[n-1] + fy(x[n-1],y[n-1],t[n-1])*dt
x[n] = x[n-1] + vx[n-1]*dt
y[n] = y[n-1] + vy[n-1]*dt
Originally, no matter which way I tweaked the code, I was getting an error on my for loop, either "object of type 'float' has no len()" (I didn't understand what float python could be referring to), or "setting an array element with a sequence" (I also didn't understand what sequence it meant). I've managed to get rid of the errors, but my results are just wrong. I get vx and vy arrays of 10s, an x array of integers from 10. to 109., and a y array of integers from 0. to 99.
I suspect there are issues with fx(x,y,t) and fy(x,y,t) or with the way I have coded the runge-kutta functions to go with fx and fy, because I've used the same runge-kutta code for other functions and it works fine.
I greatly appreciate any help in figuring out why my code isn't working. Thank you.
Physics
The Newton law gives you a second order ODE u''=F(u) with u=[x,y]. Using v=[x',y'] you get the first order system
u' = v
v' = F(u)
which is 4-dimensional and has to be solved using a 4 dimensional state. The only reduction available is to use the Kepler laws which allow to reduce the system to a scalar order one ODE for the angle. But that is not the task here.
But to get the scales correct, for a circular orbit of radius R with angular velocity w one gets the identity w^2*R^3=G*M which implies that the speed along the orbit is w*R=sqrt(G*M/R) and period T=2*pi*sqrt(R^3/(G*M)). With the data given, R ~ 10, w ~ 1, thus G*M ~ 1000 for a close-to-circular orbit, so with M=20 this would require G between 50 and 200, with an orbital period of about 2*pi ~ 6. The time span of 10 could represent one half to about 2 or 3 orbits.
Euler method
You correctly implemented the Euler method to calculate values in the last loop of your code. That it may look un-physical can be because the Euler method continuously increases the orbit, as it moves to the outside of convex trajectories following the tangent. In your implementation this outward spiral can be seen for G=100.
This can be reduced in effect by choosing a smaller step size, such as dt=0.001.
You should select the integration time to be a good part of a full orbit to get a presentable result, with above parameters you get about 2 loops, which is good.
RK4 implementation
You made several errors. Somehow you lost the velocities, the position updates should be based on the velocities.
Then you should have halted at fx(x + .5*kx1, y + .5*kx1, t + .5*dt) to reconsider your approach as that is inconsistent with any naming convention. The consistent, correct variant is
fx(x + .5*kx1, y + .5*ky1, t + .5*dt)
which shows that you can not decouple the integration of a coupled system, as you need the y updates alongside the x updates. Further, the function values are the accelerations, thus update the velocities. The position updates use the velocities of the current state. Thus the step should start as
kx1 = dt * fx(x,y,t) # vx update
mx1 = dt * vx # x update
ky1 = dt * fy(x,y,t) # vy update
my1 = dt * vy # y update
kx2 = dt * fx(x + 0.5*mx1, y + 0.5*my1, t + 0.5*dt)
mx2 = dt * (vx + 0.5*kx1)
ky2 = dt * fy(x + 0.5*mx1, y + 0.5*my1, t + 0.5*dt)
my2 = dt * (vy + 0.5*ky1)
etc.
However, as you see, this already starts to become unwieldy. Assemble the state into a vector and use a vector valued function for the system equations
M, G = 20, 100
def orbitsys(u):
x,y,vx,vy = u
r = np.hypot(x,y)
f = G*M/r**3
return np.array([vx, vy, -f*x, -f*y]);
Then you can use a cook-book implementation of the Euler or Runge-Kutta step
def Eulerstep(f,u,dt): return u+dt*f(u)
def RK4step(f,u,dt):
k1 = dt*f(u)
k2 = dt*f(u+0.5*k1)
k3 = dt*f(u+0.5*k2)
k4 = dt*f(u+k3)
return u + (k1+2*k2+2*k3+k4)/6
and combine them into an integration loop
def Eulerintegrate(f, y0, tspan):
y = np.zeros([len(tspan),len(y0)])
y[0,:]=y0
for k in range(1, len(tspan)):
y[k,:] = Eulerstep(f, y[k-1], tspan[k]-tspan[k-1])
return y
def RK4integrate(f, y0, tspan):
y = np.zeros([len(tspan),len(y0)])
y[0,:]=y0
for k in range(1, len(tspan)):
y[k,:] = RK4step(f, y[k-1], tspan[k]-tspan[k-1])
return y
and invoke them with your given problem
dt = .1
t = np.arange(0,10,dt)
y0 = np.array([10, 0.0, 10, 10])
sol_euler = Eulerintegrate(orbitsys, y0, t)
x,y,vx,vy = sol_euler.T
plt.plot(x,y)
sol_RK4 = RK4integrate(orbitsys, y0, t)
x,y,vx,vy = sol_RK4.T
plt.plot(x,y)
You are not using rkx, rky functions anywhere!
There are two return at the end of function definition you should use
return [(kx1 + 2*kx2 + 2*kx3 + kx4)/6, (mx1 + 2*mx2 + 2*mx3 + mx4)/6] (as pointed out by #eapetcho). Also, your implementation of Runge-Kutta is not clear to me.
You have dv/dt so you solve for v and then update r accordingly.
for n in range(1,len(t)): #solve using RK4 functions
vx[n] = vx[n-1] + rkx(vx[n-1],vy[n-1],t[n-1])*dt
vy[n] = vy[n-1] + rky(vx[n-1],vy[n-1],t[n-1])*dt
x[n] = x[n-1] + vx[n-1]*dt
y[n] = y[n-1] + vy[n-1]*dt
Here is my version of the code
import numpy as np
#constants
G=1
M=1
h=0.1
#initiating variables
rt = np.arange(0,10,h)
vx = np.zeros(len(rt))
vy = np.zeros(len(rt))
rx = np.zeros(len(rt))
ry = np.zeros(len(rt))
#initial conditions
vx[0] = 10 #initial x velocity
vy[0] = 10 #initial y velocity
rx[0] = 10 #initial x position
ry[0] = 0 #initial y position
def fx(x,y): #x acceleration
return -G*M*x/((x**2+y**2)**(3/2))
def fy(x,y): #y acceleration
return -G*M*y/((x**2+y**2)**(3/2))
def rk4(xj, yj):
k0 = h*fx(xj, yj)
l0 = h*fx(xj, yj)
k1 = h*fx(xj + 0.5*k0 , yj + 0.5*l0)
l1 = h*fy(xj + 0.5*k0 , yj + 0.5*l0)
k2 = h*fx(xj + 0.5*k1 , yj + 0.5*l1)
l2 = h*fy(xj + 0.5*k1 , yj + 0.5*l1)
k3 = h*fx(xj + k2, yj + l2)
l3 = h*fy(xj + k2, yj + l2)
xj1 = xj + (1/6)*(k0 + 2*k1 + 2*k2 + k3)
yj1 = yj + (1/6)*(l0 + 2*l1 + 2*l2 + l3)
return (xj1, yj1)
for t in range(1,len(rt)):
nv = rk4(vx[t-1],vy[t-1])
[vx[t],vy[t]] = nv
rx[t] = rx[t-1] + vx[t-1]*h
ry[t] = ry[t-1] + vy[t-1]*h
I suspect there are issues with fx(x,y,t) and fy(x,y,t)
This is the case, I just checked my code for fx=3 and fy=y and I got a nice trajectory.
Here is the ry vs rx plot:

numpy.float64 error in my Runge-Kutta 4th-order method for a series of differential equations

This is a 4th order Runge-Kutta method I've made to eventually graph some differential equations.
The goal is to create a 4 by 100,000x.1 array that gives me the value of x, y, dx, dy at every point in the timestep, so that I can graph any equation with those 4 parameters.
#Assumptions
x0, y0 = -.250, .433
x1, y1 = -.250,-.433
x2, y2 = .500, .000
R = .2
C = .5
d = .25
#Imports
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as intgr
import math
#ag = [[ x0, y0], [ x1, y1], [ x2, y2]]
mag = [[-.250,.433], [-.250,-.433], [.500,.000]]
def der( xin, t ):
mag = [[-.250,.433],[-.250,-.433],[.500,.000]]
x = xin[0]
y = xin[1]
vx = xin[2]
vy = xin[3]
dx = vx
dy = vy
vx2 = 0
vy2 = 0
vx1 = -R * vx - C * x
vy1 = -R * vy - C * y
for i in range( mag.__len__() - 1 ):
vx2 = vx2 + ( ( mag[i][0] - x )
/ ( ( mag[i][0] - x )**2
+ ( mag[i][1] - y )**2
+ d**2
)**1.5
)
vy2 = vy2 + ( ( mag[i][1] - y )
/ ( ( mag[i][0] - x )**2
+ ( mag[i][1] - y )**2
+ d**2
)**1.5
)
vx3 = vx1 + vx2
vy3 = vy1 + vy2
array = [dx,dy,vx3,vy3]
return array
dt = .1
t = np.arange( 0, 100000, dt )
xzero = [.2, .2, 0, 0]
def RK4( func, xzero, t ):
rows = xzero.__len__()
columns = t.__len__()
x = np.zeros( ( rows, columns ) )
x_t = 0
ind = 0
x[:,ind] = xzero
dt = t[1] - t[0]
for time in t[0:len( t ) - 1]:
ind = ind + 1
K1 = dt * func( x[:,ind-1], time )
K2 = dt * func( x[:,ind-1] + .5 * K1, time + .5 * dt )
K3 = dt * func( x[:,ind-1] + .5 * K2, time + .5 * dt )
K4 = dt * func( x[:,ind-1] + K3, time + dt )
x[:,ind] = x[:,ind-1] + ( 1.0 / 6.0 ) * ( K1
+ 2 * K2
+ 2 * K3
+ K4
)
return x
print( RK4( func = der, xzero = xzero, t = t ) )
Produces a numpy float 64 error
I'm not exactly sure why but some variable in my code isn't being interpreted as a number?
Thanks for the help in advance and let me know if I should provide more code or a larger context.
The error message:
You are trying to multiply a floating point number with an instance of a list.
This kind of operation is actually well defined for integers, where you get the concatenation of multiple copies of the input list ( Given a = [1, 2, 3]; print( 2*a ) returns [1, 2, 3, 1, 2, 3] ). Thus the error message.
Solution:
You will want to use numpy consistently and especially the vector arithmetic that its array object provides.
As a first point, the return of the ODE function from RK4() should be rather articulated
as:
return np.array( [dx, dy, vx3, vy3] )

Categories