Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
periodic boundary contions
#1
Hi all

I want to add periodic boundary conditions to the plot below.
Conditions:
If x>pi then x=x - 2*pi

if x<-pi then x=x + 2* pi

If z>pi then z=z - 2 * pi

if z<-pi then z=z + 2* pi

I tried if,elif,else but didn't work

How can I do that? Can anyone help? Thank you



import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate
 
def f1(t,r):

    theta,x,z=r
    ftheta = (-1/2)*(np.cos(x)*np.cos(z)+np.sin(theta)/124)
    fx = 2*np.sin(theta)-(1/2)*np.cos(x)*np.sin(z)
    fz = 2*np.cos(theta)+(1/2)*np.sin(x)*np.cos(z)
    return ftheta,fx, fz
 
sol=integrate.solve_ivp(f1,(0,2000),(np.pi/4,-np.pi/2,-np.pi/2), t_eval=np.linspace(0, 2000,100000))
theta,x,z=sol.y


#Plotting
plt.plot(x,z,label = 'with \u03A6')
plt.xlabel('x')
plt.ylabel('z')

plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])    
plt.yticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])

plt.xlim(-np.pi,np.pi)
plt.ylim(-np.pi,np.pi)

plt.show()
Reply
#2
You can perhaps try
x = np.mod(x + np.pi, 2 * np.pi) - np.pi
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Solving periodic cot(x) function TheGoodTheBadTheUgly 1 1,606 Sep-26-2021, 06:07 AM
Last Post: ndc85430
  How to create a column with periodic entries? Mark17 2 2,045 Dec-16-2020, 09:58 PM
Last Post: Mark17
  How to extract (x,y) coordinates of the boundary of a .tif image? Pythonista 23 16,400 Feb-18-2017, 09:17 AM
Last Post: merlem

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020