gregovin

Untitled

May 11th, 2022
1,588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.64 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. xDets={
  5.     "max":10,
  6.     "min":-10,
  7.     "step":1
  8.     }
  9. yDets={
  10.     "max":10,
  11.     "min":-10,
  12.     "step":1
  13.     }
  14. plotlims={
  15.     "maxSteps":30000,
  16.     "solStepSize":0.05,
  17.     }
  18. initialPoints =[[0,1]]
  19. initialPointsT =[]
  20. colors=['g','m','y','r']
  21. def diff(x,y):
  22.     return 1-x+4*y
  23.  
  24. def diffsT(x,y):
  25.     return [x+y,x-y]
  26.  
  27. def diffsHigher(ys,x):
  28.     return x*ys[0]+ys[1]+ys[2]/x
  29.  
  30. def isInBounds(x,y):
  31.     return xDets["min"]<=x and xDets["max"]>=x and yDets["min"]<=y and yDets["max"]>=y
  32. def plotSoln(x0,y0):
  33.     points=[]
  34.     x,y=x0,y0
  35.     steps=0
  36.     while steps<plotlims["maxSteps"] and isInBounds(x,y) and [x,y] not in points:
  37.         diffs=[1,diff(x,y)]
  38.         points.append([x,y])
  39.         steps+=1
  40.         norm=np.sqrt(diffs[0]**2+diffs[1]**2)
  41.         x+= plotlims["solStepSize"]*diffs[0]/norm
  42.         y+= plotlims["solStepSize"]*diffs[1]/norm
  43.     steps=0
  44.     x,y=x0,y0
  45.     diffs =[1,diff(x,y)]
  46.     steps+=1
  47.     norm=np.sqrt(diffs[0]**2+diffs[1]**2)
  48.     x-= plotlims["solStepSize"]*diffs[0]/norm
  49.     y-= plotlims["solStepSize"]*diffs[1]/norm
  50.     while steps<plotlims["maxSteps"] and isInBounds(x,y)and [x,y] not in points:
  51.         points.insert(0,[x,y])
  52.         diffs =[1,diff(x,y)]
  53.         steps+=1
  54.         norm=np.sqrt(diffs[0]**2+diffs[1]**2)
  55.         x-= plotlims["solStepSize"]*diffs[0]/norm
  56.         y-= plotlims["solStepSize"]*diffs[1]/norm
  57.     return points
  58. def bplot(x0,y0):
  59.     points = []
  60.     x,y=x0,y0
  61.     steps=0
  62.     ss=plotlims["solStepSize"]
  63.     while steps<plotlims["maxSteps"] and isInBounds(x,y) and [x,y] not in points:
  64.         points.append([x,y])
  65.         x+=ss
  66.         y=((1-x)*ss+y)/(1-4*ss)
  67.         steps +=1
  68.     steps=0
  69.     x,y=x0,y0
  70.     y=(1-4*ss)*y+(1-x)*ss
  71.     x-=ss
  72.     while steps<plotlims["maxSteps"] and isInBounds(x,y) and [x,y] not in points:
  73.         points.insert(0,[x,y])
  74.         y=(1-4*ss)*y+(1-x)*ss
  75.         x-=ss
  76.         steps +=1
  77.     return points
  78. def getSize(xDets,yDets):
  79.     xSteps=int(np.ceil((xDets["max"]-xDets["min"])/xDets["step"])+1)
  80.     ySteps=int(np.ceil((yDets["max"]-yDets["min"])/yDets["step"])+1)
  81.     return xSteps,ySteps
  82. def plotSolnT(x0,y0):
  83.     points=[]
  84.     x,y=x0,y0
  85.     steps=0
  86.     while steps<plotlims["maxSteps"] and isInBounds(x,y) and [x,y] not in points:
  87.         diffs=diffsT(x,y)
  88.         points.append([x,y])
  89.         steps+=1
  90.         norm=np.sqrt(diffs[0]**2+diffs[1]**2)
  91.         x+= plotlims["solStepSize"]*diffs[0]/norm
  92.         y+= plotlims["solStepSize"]*diffs[1]/norm
  93.     steps=0
  94.     x,y=x0,y0
  95.     diffs =[1,diffsT(x,y)]
  96.     steps+=1
  97.     norm=np.sqrt(diffs[0]**2+diffs[1]**2)
  98.     x-= plotlims["solStepSize"]*diffs[0]/norm
  99.     y-= plotlims["solStepSize"]*diffs[1]/norm
  100.     while steps<plotlims["maxSteps"] and isInBounds(x,y)and [x,y] not in points:
  101.         diffs=diffsT(x,y)
  102.         points.insert(0,[x,y])
  103.         steps+=1
  104.         norm=np.sqrt(diffs[0]**2+diffs[1]**2)
  105.         x-= plotlims["solStepSize"]*diffs[0]/norm
  106.         y-= plotlims["solStepSize"]*diffs[1]/norm
  107.     return points
  108. fig, ax=plt.subplots()  
  109. for idx,pt in enumerate(initialPoints):
  110.     soln=np.array(plotSoln(pt[0],pt[1]))
  111.     plt.plot(soln[:,0],soln[:,1],color=colors[idx])
  112.  
  113. for idx,pt in enumerate(initialPoints):
  114.     soln=np.array(bplot(pt[0],pt[1]))
  115.     plt.plot(soln[:,0],soln[:,1],color=colors[idx+len(initialPoints)])
  116. for idx,pt in enumerate(initialPointsT):
  117.     soln=np.arry(plotSoln(pt[0],pt[1]))
  118.     plt.plot(soln[:,0],soln[:,1],color=colors[idx+len(initialPoints)])
  119. ax.set(xlim=(xDets["min"],xDets["max"]),ylim=(yDets["min"],yDets["max"]))
  120. ax.axhline(y=0, color='k')
  121. ax.axvline(x=0, color='k')
  122.  
  123. plt.show()
  124.  
Advertisement
Add Comment
Please, Sign In to add comment