Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Lecture slides for UCLA LS 30B, Spring 2020

Views: 14462
License: GPL3
Image: ubuntu2004
Kernel: SageMath 9.1
def axes3d(x_range, y_range, z_range, **options): """Draw x, y, and z axes in 3D, and optionally a grid on the xy-plane 'x_range' should have the form (x, xmin, xmax), and likewise for 'y_range' and 'z_range'. Here the 'x' can be either a symbolic variable, or a string. Either way, it is only used as a label for the corresponding axis. Options: 'origin' - Locate the axes so that they all intersect at this point (default (0,0,0)) 'grid' - Whether or not to draw a grid on the xy-plane (default True) All other options are as for the 'line3d' and 'arrow3d' commands, and are passed directly on to them. """ x0, y0, z0 = options.pop("origin", (0, 0, 0)) grid = options.pop("grid", True) gridoptions = options.copy() options.setdefault("color", "black") gridoptions.setdefault("color", "gray") x, xmin, xmax = x_range y, ymin, ymax = y_range z, zmin, zmax = z_range xmin, xmax = min(x0, xmin), max(x0, xmax) ymin, ymax = min(y0, ymin), max(y0, ymax) zmin, zmax = min(z0, zmin), max(z0, zmax) p = Graphics() p += arrow3d((xmin, y0, z0), (xmax, y0, z0), width=1, **options) p += arrow3d((x0, ymin, z0), (x0, ymax, z0), width=1, **options) p += arrow3d((x0, y0, zmin), (x0, y0, zmax), width=1, **options) p += text3d(str(x), (xmax + (xmax - xmin)*0.05, y0, z0), **options) p += text3d(str(y), (x0, ymax + (ymax - ymin)*0.05, z0), **options) p += text3d(str(z), (x0, y0, zmax + (zmax - zmin)*0.05), **options) xgridsize = 10**int(log(0.68*max(-xmin, xmax), 10)) ygridsize = 10**int(log(0.68*max(-ymin, ymax), 10)) zgridsize = 10**int(log(0.68*max(-zmin, zmax), 10)) gridmin = ceil(xmin/xgridsize) * xgridsize for xval in srange(gridmin, xmax+xgridsize/100.0, xgridsize): #p += text3d("{:.0f}".format(float(xval)), (xval, ygridsize/5.0, 0), **options) if grid: p += line3d(((xval, ymin, 0), (xval, ymax, 0)), thickness=1, **gridoptions) gridmin = ceil(ymin/ygridsize) * ygridsize for yval in srange(gridmin, ymax+ygridsize/100.0, ygridsize): #p += text3d("{:.0f}".format(float(yval)), (xgridsize/5.0, yval, 0), **options) if grid: p += line3d(((xmin, yval, 0), (xmax, yval, 0)), thickness=1, **gridoptions) gridmin = ceil(zmin/zgridsize) * zgridsize #for zval in srange(gridmin, zmax+zgridsize/100.0, zgridsize): #p += text3d("{:.0f}".format(float(zval)), (xgridsize/5.0, 0, zval), **options) return p
xmin, xmax = -7.5, 7.5 ymin, ymax = -7.5, 7.5 zmin, zmax = 0, 6.5 f(x,y) = 3 + sin(2*(x-2)/3)*cos((y-3)/2) - 2*cos((x-2)/3)*sin((y-3)/5) x0, y0 = 2, 3 z0 = f(x0,y0) df_dx = diff(f, x) df_dy = diff(f, y) L(x, y) = f(x0, y0) + df_dx(x0, y0)*(x - x0) + df_dy(x0, y0)*(y - y0) print((x0,y0,z0)) print(df_dx(x0, y0), df_dy(x0, y0)) p = axes3d((x, -7.5, 7.5), (y, -7.5, 7.5), ("z", 0, 6)) p += plot3d(f(x,y), (x, xmin, xmax), (y, ymin, ymax), opacity=0.7) p += sphere((x0,y0,z0), 0.15, color="green") p += plot3d(L(x,y), (x, x0-2, x0+2), (y, y0-2, y0+2), color="lightgreen", opacity=0.8) p.show(frame=False)
(2, 3, 3) 2/3 -2/5
f(x,y) = 3 + sin(2*(x-2)/3)*cos((y-3)/2) - 2*cos((x-2)/3)*sin((y-3)/5)
p = axes3d((x, -7.5, 7.5), (y, -7.5, 7.5), ("z", 0, 6)) p += plot3d(f(x,y), (x, -7, 7), (y, -7, 7), opacity=0.7) p.show(frame=False)
p += point((2, 3, f(2,3)), size=60, color="green") p.show(frame=False)
df_dx = diff(f, x) df_dx(2,3)
2/3
df_dy = diff(f, y) df_dy(2,3)
-2/5
print(f(2,3))
3
L(x, y) = 3 + 2/3*(x - 2) + -2/5*(y - 3)
p += plot3d(L(x,y), (x, -1, 5), (y, 0, 6), color="lightgreen", opacity=0.9) p.show(frame=False)