How To Draw A Direction Field For A System Of Differential Equations
I had a lot of fun making one of these as a hobby project using pygame. I plotted the slope at each pixel, using shades of blueish for positive and shades of carmine for negative. Black is for undefined. This is dy/dx = log(sin(x/y)+cos(y/ten))
:
You can zoom in & out - here is zoomed in on the center upper part here:
and besides click on a point to graph the line going through that point:
Information technology's just 440 lines of code, so here is the .zip of all the files. I gauge I'll extract relevant $.25 here.
The equation itself is input as a valid Python expression in a string, east.g. "log(sin(x/y)+cos(y/10))"
. This is then compile
d. This part here graphs the colour field, where cocky.func.eval()
gives the dy/dx
at the given signal. The code is a bit complicated here because I made information technology return in stages - commencement 32x32 blocks, then 16x16, etc. - to make information technology snappier for the user.
def graphcolorfield(self, sqsizes=[32,16,8,iv,two,1]): su = ScreenUpdater(50) lastskip = self.xscreensize quitit = False for squaresize in sqsizes: xsquaresize = squaresize ysquaresize = squaresize if squaresize == 1: self.screen.lock() y = 0 while y <= self.yscreensize: x = 0 skiprow = y%lastskip == 0 while x <= self.xscreensize: if skiprow and x%lastskip==0: ten += squaresize continue color = (255,255,255) try: m = cocky.func.eval(*cocky.ct.untranscoord(x, y)) if k >= 0: if 1000 < 1: c = 255 * m color = (0, 0, c) else: #c = 255 - 255 * (1.0/m) #colour = (c, c, 255) c = 255 - 255 * (1.0/grand) color = (c/2.0, c/2.0, 255) else: pm = -m if pm < ane: c = 255 * pm color = (c, 0, 0) else: c = 255 - 255 * (ane.0/pm) colour = (255, c/ii.0, c/ii.0) except: color = (0, 0, 0) if squaresize > i: self.screen.fill(color, (x, y, squaresize, squaresize)) else: self.screen.set_at((x, y), color) if su.update(): quitit = True intermission x += xsquaresize if quitit: suspension y += ysquaresize if squaresize == 1: self.screen.unlock() lastskip = squaresize if quitit: interruption
This is the code which graphs a line through a point:
def _grapheqhelp(cocky, sx, sy, stepsize, numsteps, color): ten = sx y = sy i = 0 pygame.draw.line(self.screen, colour, (x, y), (10, y), 2) while i < numsteps: lastx = 10 lasty = y endeavour: m = cocky.func.eval(x, y) except: return x += stepsize y = y + m * stepsize screenx1, screeny1 = cocky.ct.transcoord(lastx, lasty) screenx2, screeny2 = self.ct.transcoord(x, y) #print "(%f, %f)-(%f, %f)" % (screenx1, screeny1, screenx2, screeny2) try: pygame.draw.line(self.screen, color, (screenx1, screeny1), (screenx2, screeny2), two) except: return i += 1 stx, sty = self.ct.transcoord(sx, sy) pygame.depict.circumvolve(self.screen, colour, (int(stx), int(sty)), 3, 0)
And information technology runs backwards & forrard starting from that point:
def graphequation(self, sx, sy, stepsize=.01, color=(255, 255, 127)): """Graph the differential equation, given the starting betoken sx and sy, for length length using stepsize stepsize.""" numstepsf = (cocky.xrange[ane] - sx) / stepsize numstepsb = (sx - cocky.xrange[0]) / stepsize self._grapheqhelp(sx, sy, stepsize, numstepsf, colour) self._grapheqhelp(sx, sy, -stepsize, numstepsb, color)
I never got effectually to cartoon bodily lines considering the pixel approach looked besides cool.
Source: https://stackoverflow.com/questions/18832763/drawing-directions-fields
Posted by: nicholsyall1945.blogspot.com
0 Response to "How To Draw A Direction Field For A System Of Differential Equations"
Post a Comment