{{{id=6| # Here are some examples for the following two procedures: # f(x,y) = -(x^3+y^3-3*x*y) on [-1,2] x [-1,2] # f(x,y) = -(x^2+y^2) on [-1,1] x [-1,1] # f(x,y) = -(x^2+y^2-2*x*y+1) on [-1,1] x [-1,1] # f(x,y) = x*exp(-x^2-y^2) on [-1,1] x [-1,1] # f(x,y) = six(x)*cos(y) on [-pi,pi] x [-pi,pi] # f(x,y) = six(x)*sin(y) on [-pi,pi] x [-pi,pi] # f(x,y) = cos(x)*cos(y) on [-pi,pi] x [-pi,pi] # f(x,y) = sin(x)*sin(y)*sin(x+y) on [0,pi] x [0,pi] # f(x,y) = sin(x)+sin(y)+sin(x+y) on [0,pi] x [0,pi] /// }}} {{{id=1| var('x,y') @interact def example(f=x*exp(-x^2-y^2), Range_x='(-1,1)', Range_y='(-1,1)', View = selector(['Filled 2d Contour Plot', '2d Plot with Contour Lines', '3d Plot'], nrows=1, label="View")): xmin, xmax = sage_eval(Range_x); ymin, ymax = sage_eval(Range_y) if View == '3d Plot': P = plot3d(f, (x, xmin, xmax), (y, ymin, ymax)) ff = fast_float(f.diff(x), 'x', 'y') gg = fast_float(f.diff(y), 'x', 'y') hh = fast_float(f, 'x', 'y') rects = Graphics() step = 20 xborder = (xmax-xmin)/(2*step) yborder = (ymax-ymin)/(2*step) for i in range(step): for j in range(step): xx=xmin+i*(xmax-xmin)/(step-1) yy=ymin+j*(ymax-ymin)/(step-1) vx=ff(xx,yy) vy=gg(xx,yy) norm=sqrt(vx^2/xborder^2+vy^2/yborder^2) if norm != 0: vx=vx/norm vy=vy/norm rects += line3d([(xx,yy,hh(xx,yy)), (xx+vx,yy+vy,hh(xx+vx,yy+vy))], arrow_head=True, rgbcolor=(0,0,0)) show(P+rects) else: if View == 'Filled 2d Contour Plot': C = contour_plot(f, (x,xmin,xmax), (y,ymin,ymax), plot_points=30, contours=15, cmap='autumn') elif View == '2d Plot with Contour Lines': C = contour_plot(f, (x,xmin,xmax), (y,ymin,ymax), plot_points=30, contours=15, fill=False) V = plot_vector_field( [f.diff(x),f.diff(y)], (x,xmin,xmax), (y,ymin,ymax) ) show(C+V) /// }}} {{{id=2| /// }}} {{{id=3| # For the following procedure, you have to specify the domain of the function in the first four lines. # I am sorry for this inconvenience. # Convert irrational numbers to float, e.g., use "xmin=float(-pi)" instead of "xmin=-pi". /// }}} {{{id=4| xmin=float(-pi) xmax=float(pi) ymin=float(-pi) ymax=float(pi) var('x,y') @interact def example(f=sin(x)*sin(y), Start_x=((xmin+xmax)/2,(xmin, xmax)), Start_y=((ymin+ymax)/2,(ymin, ymax)), View = selector(['Filled 2d Contour Plot', '2d Plot with Contour Lines', '3d Plot'], nrows=1, label="View")): step_size=min((xmax-xmin)/100,(ymax-ymin)/100) steps=1000 ff = fast_float(f.diff(x), 'x', 'y') gg = fast_float(f.diff(y), 'x', 'y') hh = fast_float(f, 'x', 'y') if View == '3d Plot': P = plot3d(f, (x, xmin, xmax), (y, ymin, ymax)) rects = Graphics() step = 20 xborder = (xmax-xmin)/(2*step) yborder = (ymax-ymin)/(2*step) for i in range(step): for j in range(step): xx=xmin+i*(xmax-xmin)/(step-1) yy=ymin+j*(ymax-ymin)/(step-1) vx=ff(xx,yy) vy=gg(xx,yy) norm=sqrt(vx^2/xborder^2+vy^2/yborder^2) if norm != 0: vx=vx/norm vy=vy/norm rects += line3d([(xx,yy,hh(xx,yy)), (xx+vx,yy+vy,hh(xx+vx,yy+vy))], arrow_head=True, rgbcolor=(0,0,0)) steps = int(steps) points = Graphics() xx = Start_x yy = Start_y for i in range(steps): nx = xx + step_size * ff(xx,yy) ny = yy + step_size * gg(xx,yy) if nx >= xmin: if nx <= xmax: if ny >= ymin: if ny <= ymax: points += line([(xx,yy,hh(xx,yy)),(nx,ny,hh(nx,ny))],color='red',thickness=3) xx = nx yy = ny starting_point = point3d([(Start_x,Start_y,hh(Start_x,Start_y))],size=10,color='red') show(P+rects+points+starting_point) else: if View == 'Filled 2d Contour Plot': C = contour_plot(f, (x,xmin,xmax), (y,ymin,ymax), plot_points=30, contours=15, cmap='autumn') elif View == '2d Plot with Contour Lines': C = contour_plot(f, (x,xmin,xmax), (y,ymin,ymax), plot_points=30, contours=15, fill=False) V = plot_vector_field( [f.diff(x),f.diff(y)], (x,xmin,xmax), (y,ymin,ymax) ) steps = int(steps) points = Graphics() xx = Start_x yy = Start_y for i in range(steps): nx = xx + step_size * ff(xx,yy) ny = yy + step_size * gg(xx,yy) if nx >= xmin: if nx <= xmax: if ny >= ymin: if ny <= ymax: points += line([(xx,yy),(nx,ny)]) xx = nx yy = ny starting_point = point([(Start_x,Start_y)], pointsize=50) show(C+V+starting_point+points) /// }}} {{{id=5| /// }}}