Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News Sign UpSign In
| Download
Project: sir model
Views: 1353
plot(2*x+exp(-x), (x,-4,4))
plot(2*x+exp(-x), (x,-4,4), axes_labels=['$x$', '$2x+e^{-x}$'], color='green', linestyle='--', thickness=3) print('\n\n NOTE: Sage understands Latex syntax for typesetting math expressions in axes labels.')
NOTE: Sage understands Latex syntax for typesetting math expressions in axes labels.
Some hints: Sage uses syntax like x^2, 5*x, etc., for arithmetic operations. Also, all functions require parentheses, like exp(-x), or sin(3*x), etc.
# Piecewise plots. f1 = x^2 f2 = 5-x f3 = ln(x) f = piecewise( [ ((-infinity,0),f1), ((0,4),f2), ((4,infinity),f3) ] ) f.plot((x,-4,10), axes_labels=['$x$', '$f(x)$']) # NOTE: If we don't want vertical lines at the points of discontinuity, use the following instead: #f.plot(x,-4,10, exclude=[0,4])
# Another way to do the same/similar piecewise plot: f1 = plot(x^2, x, -4, 0) # I cannot use -infinity for lower bound here. So I used -4. f2 = plot(5-x, x, 0, 4) f3 = plot(ln(x), x, 4, 10) # I cannot use infinity for upper bound. So I used 10. (f1+f2+f3).show(xmin=-4, xmax=10, axes_labels=['$x$', '$f(x)$'])
f = plot(piecewise( [ ((-infinity,0),f1), ((0,4),f2), ((4,infinity),f3) ] )) pt1 = point([(0,0), (4,1)], color='black', pointsize=50) pt2 = point([(0,5), (4,ln(4))], color='white', pointsize=50, faceted='true') (f+pt1+pt2).show(xmin=-4, xmax=10, axes_labels=['$x$', '$f(x)$'])
y = var('y') # This defines y as another variable. By default you get only x. implicit_plot( cos(x*y)==x^2 - y, (x,-3,3), (y,-2,8) ) print('\n NOTE the "==" sign used to denote the conditional equality.')
NOTE the "==" sign used to denote the conditional equality.
# parametric plots t = var('t') parametric_plot( ( sin(t^2-cos(t)), cos(3*t) ), (t,-pi/2,pi) )
# Plots of (x,y) data: Can use the "scatter_plot" command, the "point" command, or the "line" command. # Must create a list of 2d ordered pairs of points. # It doesn't work with 2 separate lists -- it must be one list of 2-tuples. xl = [-4.8, -3.1, -2.2, -0.7, 0.2, 2.5, 4.0, 5.6] yl = [3.7, 2.9, 2.4, 2.2, 3.5, 5.8, 9.6, 6.3] datapoints = zip(xl, yl) # The "zip" command creates matched pairs from 2 separate lists. scatter_plot(datapoints, axes_labels=['$x$', '$y$'], markersize=30) point(datapoints, axes_labels=['$x$', '$y$'], pointsize=30) line(datapoints, axes_labels=['$x$', '$y$'], color='blue')
f = open('./globtemp.csv') # This file has comma separated data, with a plain text header line. datapoints = [] # This will assemble my data, in the form of a list of ordered pairs headerline = f.readline() # Read & discard the header line line = f.readline() # This is the first line of actual data while (line !=''): xy = line.split(',') # The comma splits each line into two items of data datapoints.append((float(xy[0]),float(xy[1]))) line = f.readline() point(datapoints, axes_labels=['year', 'temp $^\circ$C'], pointsize=30)
y = var('y') p = plot(2*x+exp(-x), (x,-3,3), color='green', linestyle='--', thickness=2, aspect_ratio=1) q = implicit_plot( cos(x*y)==x^2 - y, (x,-3,3), (y,-2,8), figsize=8, aspect_ratio=1 ) show(p+q)