Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- xDets={
- "max":10,
- "min":-10,
- "step":1
- }
- yDets={
- "max":10,
- "min":-10,
- "step":1
- }
- plotlims={
- "maxSteps":30000,
- "solStepSize":0.05,
- }
- initialPoints =[[0,1]]
- initialPointsT =[]
- colors=['g','m','y','r']
- def diff(x,y):
- return 1-x+4*y
- def diffsT(x,y):
- return [x+y,x-y]
- def diffsHigher(ys,x):
- return x*ys[0]+ys[1]+ys[2]/x
- def isInBounds(x,y):
- return xDets["min"]<=x and xDets["max"]>=x and yDets["min"]<=y and yDets["max"]>=y
- def plotSoln(x0,y0):
- points=[]
- x,y=x0,y0
- steps=0
- while steps<plotlims["maxSteps"] and isInBounds(x,y) and [x,y] not in points:
- diffs=[1,diff(x,y)]
- points.append([x,y])
- steps+=1
- norm=np.sqrt(diffs[0]**2+diffs[1]**2)
- x+= plotlims["solStepSize"]*diffs[0]/norm
- y+= plotlims["solStepSize"]*diffs[1]/norm
- steps=0
- x,y=x0,y0
- diffs =[1,diff(x,y)]
- steps+=1
- norm=np.sqrt(diffs[0]**2+diffs[1]**2)
- x-= plotlims["solStepSize"]*diffs[0]/norm
- y-= plotlims["solStepSize"]*diffs[1]/norm
- while steps<plotlims["maxSteps"] and isInBounds(x,y)and [x,y] not in points:
- points.insert(0,[x,y])
- diffs =[1,diff(x,y)]
- steps+=1
- norm=np.sqrt(diffs[0]**2+diffs[1]**2)
- x-= plotlims["solStepSize"]*diffs[0]/norm
- y-= plotlims["solStepSize"]*diffs[1]/norm
- return points
- def bplot(x0,y0):
- points = []
- x,y=x0,y0
- steps=0
- ss=plotlims["solStepSize"]
- while steps<plotlims["maxSteps"] and isInBounds(x,y) and [x,y] not in points:
- points.append([x,y])
- x+=ss
- y=((1-x)*ss+y)/(1-4*ss)
- steps +=1
- steps=0
- x,y=x0,y0
- y=(1-4*ss)*y+(1-x)*ss
- x-=ss
- while steps<plotlims["maxSteps"] and isInBounds(x,y) and [x,y] not in points:
- points.insert(0,[x,y])
- y=(1-4*ss)*y+(1-x)*ss
- x-=ss
- steps +=1
- return points
- def getSize(xDets,yDets):
- xSteps=int(np.ceil((xDets["max"]-xDets["min"])/xDets["step"])+1)
- ySteps=int(np.ceil((yDets["max"]-yDets["min"])/yDets["step"])+1)
- return xSteps,ySteps
- def plotSolnT(x0,y0):
- points=[]
- x,y=x0,y0
- steps=0
- while steps<plotlims["maxSteps"] and isInBounds(x,y) and [x,y] not in points:
- diffs=diffsT(x,y)
- points.append([x,y])
- steps+=1
- norm=np.sqrt(diffs[0]**2+diffs[1]**2)
- x+= plotlims["solStepSize"]*diffs[0]/norm
- y+= plotlims["solStepSize"]*diffs[1]/norm
- steps=0
- x,y=x0,y0
- diffs =[1,diffsT(x,y)]
- steps+=1
- norm=np.sqrt(diffs[0]**2+diffs[1]**2)
- x-= plotlims["solStepSize"]*diffs[0]/norm
- y-= plotlims["solStepSize"]*diffs[1]/norm
- while steps<plotlims["maxSteps"] and isInBounds(x,y)and [x,y] not in points:
- diffs=diffsT(x,y)
- points.insert(0,[x,y])
- steps+=1
- norm=np.sqrt(diffs[0]**2+diffs[1]**2)
- x-= plotlims["solStepSize"]*diffs[0]/norm
- y-= plotlims["solStepSize"]*diffs[1]/norm
- return points
- fig, ax=plt.subplots()
- for idx,pt in enumerate(initialPoints):
- soln=np.array(plotSoln(pt[0],pt[1]))
- plt.plot(soln[:,0],soln[:,1],color=colors[idx])
- for idx,pt in enumerate(initialPoints):
- soln=np.array(bplot(pt[0],pt[1]))
- plt.plot(soln[:,0],soln[:,1],color=colors[idx+len(initialPoints)])
- for idx,pt in enumerate(initialPointsT):
- soln=np.arry(plotSoln(pt[0],pt[1]))
- plt.plot(soln[:,0],soln[:,1],color=colors[idx+len(initialPoints)])
- ax.set(xlim=(xDets["min"],xDets["max"]),ylim=(yDets["min"],yDets["max"]))
- ax.axhline(y=0, color='k')
- ax.axvline(x=0, color='k')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment