gregovin

Differential Plotter

Apr 30th, 2022
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. # Tells the program how big the window should be, and how far apart
  5. # the derivatives should render
  6. xDets={
  7.     "max":10,
  8.     "min":-10,
  9.     "step":1
  10.     }
  11. yDets={
  12.     "max":210,
  13.     "min":-10,
  14.     "step":10
  15.     }
  16. maxSteps=30000
  17. solStepSize=0.01
  18. H=120
  19. initialPoints=[[0,42],[0,101],[0,150]]
  20. colors=['g','m','y','r']
  21. #the derivative function used
  22. def diff(x,y):
  23.     return y*(2-0.01*y)-H
  24.  
  25. #computes how many times the step goes into the specified range, and rounds up
  26. #to be as close as possible
  27. def getSize(xDets,yDets):
  28.     xSteps=int(np.ceil((xDets["max"]-xDets["min"])/xDets["step"])+1)
  29.     ySteps=int(np.ceil((yDets["max"]-yDets["min"])/yDets["step"])+1)
  30.     return xSteps,ySteps
  31.  
  32. xSteps,ySteps=getSize(xDets,yDets)
  33. # Establishes arrays for the x and y axis with each entry at the gridlines
  34. x = np.linspace(xDets["min"],xDets["max"],xSteps)
  35. y = np.linspace(yDets["min"],yDets["max"],ySteps)
  36.  
  37. # creates a pair for every point so X, Y can represent the whole grid
  38. X, Y= np.meshgrid(x,y)
  39.  
  40. # Computes the derivative at all points
  41. Diffs=diff(X,Y)
  42.  
  43. # Constructs a normalized vector in the direction of the derivative
  44. Lens=np.sqrt(Diffs**2+1)
  45. U=1/Lens
  46. V=Diffs/Lens
  47.  
  48.  
  49. #plots it
  50. fig, ax=plt.subplots()
  51. fig.savefig('graph.png')
  52. ax.quiver(X,Y,U,V,color="C0",angles="xy",
  53.           scale_units="xy",width=0.0015)
  54.  
  55. def plotSoln(x0,y0):
  56.     points = []
  57.     x,y=x0,y0
  58.     steps=0
  59.     while steps<maxSteps and isInBounds(x,y)and [x,y] not in points:
  60.         diffs=[1,diff(x,y)]
  61.         points.append([x,y])
  62.         steps+=1
  63.         norm=np.sqrt(diffs[0]**2+diffs[1]**2)
  64.         x+= solStepSize*diffs[0]/norm
  65.         y+= solStepSize*diffs[1]/norm
  66.     steps=0
  67.     x,y=x0,y0
  68.     diffs =[1,diff(x,y)]
  69.     steps+=1
  70.     norm=np.sqrt(diffs[0]**2+diffs[1]**2)
  71.     x-= solStepSize*diffs[0]/norm
  72.     y-= solStepSize*diffs[1]/norm
  73.     while steps<maxSteps and isInBounds(x,y)and [x,y] not in points:
  74.         points.insert(0,[x,y])
  75.         diffs =[1,diff(x,y)]
  76.         steps+=1
  77.         norm=np.sqrt(diffs[0]**2+diffs[1]**2)
  78.         x-= solStepSize*diffs[0]/norm
  79.         y-= solStepSize*diffs[1]/norm
  80.     return points
  81.        
  82. def isInBounds(x,y):
  83.     return xDets["min"]<=x and xDets["max"]>=x and yDets["min"]<=y and yDets["max"]>=y
  84. for idx,pt in enumerate(initialPoints):
  85.     soln=np.array(plotSoln(pt[0],pt[1]))
  86.     plt.plot(soln[:,0],soln[:,1],color=colors[idx])
  87.  
  88. ax.set(xlim=(xDets["min"],xDets["max"]),ylim=(yDets["min"],yDets["max"]))
  89. ax.axhline(y=0, color='k')
  90. ax.axvline(x=0, color='k')
  91.  
  92. plt.show()
  93.  
Advertisement
Add Comment
Please, Sign In to add comment