Differentiation and Integration in Sage Integrals Numerical Integration
Total Page:16
File Type:pdf, Size:1020Kb
MAT 305: Mathematical Computing John Perry Limits Differentiation Explicit differentiation MAT 305: Mathematical Computing Implicit differentiation Integration Differentiation and Integration in Sage Integrals Numerical integration Summary John Perry University of Southern Mississippi Fall 2011 MAT 305: Mathematical Computing Outline John Perry Limits Differentiation 1 Limits Explicit differentiation Implicit differentiation Integration Integrals 2 Differentiation Numerical integration Explicit differentiation Summary Implicit differentiation 3 Integration Integrals Numerical integration 4 Summary MAT 305: Mathematical Computing Maxima, Sympy, GSL John Perry Limits Maxima: symbolic Calculus Differentiation Explicit differentiation Storied history Implicit differentiation • Integration Written in LISP Integrals • Numerical integration Sometimes buggy in non-obvious ways • Summary Sympy: symbolic Calculus Far more recent • Written in Python • Often slower than Maxima • Eventual replacement for Maxima? • GSL: numerical Calculus GNU Scientific Library • MAT 305: Mathematical Computing Outline John Perry Limits Differentiation 1 Limits Explicit differentiation Implicit differentiation Integration Integrals 2 Differentiation Numerical integration Explicit differentiation Summary Implicit differentiation 3 Integration Integrals Numerical integration 4 Summary MAT 305: Mathematical Computing The limit() command John Perry Limits Differentiation Explicit differentiation Implicit differentiation Integration limit( f (x), x=a, direction) where Integrals Numerical integration f (x) is a function in x • Summary a • 2 R direction is optional, but if used has form • dir=’left’ or • dir=’right’ • MAT 305: Mathematical Computing Examples John Perry Limits sage: limit(x**2-1,x=4) Differentiation Explicit differentiation 15 Implicit differentiation Integration Integrals sage: limit(x/abs(x),x=0) Numerical integration und (Translation: “undefined”) Summary sage: limit(x/abs(x),x=0,dir=’right’) 1 sage: limit(x/abs(x),x=0,dir=’left’) -1 sage: limit(sin(1/x),x=0) ind (Translation: “indeterminate, but bounded”) MAT 305: Mathematical Computing Examples with infinite limits John Perry Limits sage: limit(e**(-x),x=infinity) Differentiation Explicit differentiation 0 Implicit differentiation Integration Integrals sage: limit((1+1/x)**x,x=infinity) Numerical integration e (An indeterminate form!) Summary sage: limit((3*x**2-1)/(2*x**2+cos(x)),x=infinity) 3/2 sage: limit(ln(x)/x,x=infinity) 0 (Another indeterminate form!) sage: limit(x/ln(x),x=infinity) +Infinity MAT 305: Mathematical Computing Outline John Perry Limits Differentiation 1 Limits Explicit differentiation Implicit differentiation Integration Integrals 2 Differentiation Numerical integration Explicit differentiation Summary Implicit differentiation 3 Integration Integrals Numerical integration 4 Summary MAT 305: Mathematical Computing The diff() command John Perry Limits Differentiation Explicit differentiation Implicit differentiation Integration diff( f (x), x, n) where Integrals Numerical integration f (x) is a function of x • Summary differentiate f with respect to x • “semi-optional”: mandatory if f has other variables • e.g., partial differentiation, or unknown constants • (optional) compute the nth derivative of f (x) • MAT 305: Mathematical Computing Examples John Perry Limits Differentiation sage: diff(e**x) Explicit differentiation e^x Implicit differentiation Integration sage: diff(x**10, 5) Integrals 30240*x^5 Numerical integration Summary sage: diff(sin(x), 99) -cos(x) sage: var(’y’) y sage: diff(x**2+y**2-1) . output cut. ValueError: No differentiation variable specified. sage: diff(x**2+y**2-1, x) 2*x MAT 305: Mathematical Computing 1 picture = 1000 words John Perry Limits sage: f(x) = sin(x) Differentiation Explicit differentiation Implicit differentiation sage: df(x) = diff(f) Integration Integrals Numerical integration sage: m0 = df(0) Summary sage: p0 = point((0,f(0)),pointsize=30) sage: fplot = plot(f,xmin=-pi/2,xmax=pi/2, thickness=2) sage: tan_line = plot(m0*(x-0)+f(0),xmin=-pi/2, xmax=pi/2,rgbcolor=(1,0,0)) sage: show(p0+fplot+tan_line,aspect_ratio=1) MAT 305: Mathematical Computing 1 picture = 1000 words John Perry Limits Differentiation 1.5 Explicit differentiation Implicit differentiation Integration 1 Integrals Numerical integration Summary 0.5 -1.5 -1 -0.5 0.5 1 1.5 -0.5 -1 -1.5 MAT 305: Mathematical Computing The implicit_diff() command John Perry Limits Differentiation Explicit differentiation Implicit differentiation There is no implicit_diff() command. To differentiate Integration implicitly, Integrals Numerical integration define yvar as a variable using the var() command; Summary • define yf as an implicit function of x using the function() • command; move everything to one side (as in implicit plots); • differentiate the non-zero side of the equation; and • solve() for diff(yf) • MAT 305: Mathematical Computing Example John Perry Limits sage: y = function(’y’,x) Differentiation sage:y Explicit differentiation Implicit differentiation y(x) (. so y is recognized as a function of x) Integration sage: diff(y) Integrals dy Numerical integration D[0](y)(x) (Sage’s dx ) Summary sage: expr = x**2 + y**2 - 1 sage: diff(expr) 2*y(x)*D[0](y)(x) + 2*x sage: deriv = diff(expr) sage: solve(deriv,diff(y)) [D[0](y)(x) == -x/y(x)] x . that is, y0 (x) = . − y MAT 305: Mathematical Computing Use computer memory, not yours John Perry Limits Differentiation Explicit differentiation Implicit differentiation Integration sage: y = function(’y’,x) Integrals Numerical integration Summary sage: yprime = diff(y) sage: deriv = diff(x**2+y**2-1) sage: solve(deriv,yprime) [D[0](y)(x) == -x/y(x)] MAT 305: Mathematical Computing 1 picture = 1000 words John Perry Limits sage: yvar = var(’y’) Differentiation Explicit differentiation sage: yf = function(’y’,x) Implicit differentiation sage: yprime = diff(yf) Integration Integrals sage: f = yf**2 - x**3 + x Numerical integration sage: df = diff(f) Summary sage: solve(df,yprime) [D[0](y)(x) == 1/2*(3*x^2 - 1)/y(x)] sage: mx(x,yvar) = 1/2*(3*x**2 - 1)/yvar sage: fplot = implicit_plot(yvar**2-x**3+x, (x,-2,2),(yvar,-2,2),color=’red’) sage: tan_line = plot(mx(-0.5,sqrt(3/8)) *(x+0.5)+sqrt(3/8), xmin=-2,xmax=2) sage: show(fplot+tan_line,aspect_ratio=1) MAT 305: Mathematical Computing 1 picture = 1000 words John Perry Limits 2 Differentiation Explicit differentiation 1.5 Implicit differentiation Integration Integrals 1 Numerical integration Summary 0.5 -0 -0.5 -1 -1.5 -2 -2 -1.5 -1 -0.5 -0 0.5 1 1.5 2 MAT 305: Mathematical Computing Outline John Perry Limits Differentiation 1 Limits Explicit differentiation Implicit differentiation Integration Integrals 2 Differentiation Numerical integration Explicit differentiation Summary Implicit differentiation 3 Integration Integrals Numerical integration 4 Summary MAT 305: Mathematical Computing The integral() command John Perry Limits Differentiation Explicit differentiation Implicit differentiation Integration Integrals Numerical integration integral( f (x), x, xmin, xmax) where Summary f (x) is a function of the (optional) variable x • (optional) xmin and xmax are limits of integration • MAT 305: Mathematical Computing Example John Perry Limits Differentiation Explicit differentiation sage: integral(x**2) Implicit differentiation 1/3*x^3 Integration Integrals Numerical integration sage: integral(x**2,x,0,1) Summary 1/3 sage: integral(1/x,x,1,infinity) . output cut. ValueError: Integral is divergent. sage: integral(1/x**2,x,1,infinity) 1 MAT 305: Mathematical Computing Beware the Jabberwock. John Perry Limits Differentiation Explicit differentiation Implicit differentiation Integration sage: integral(1/x**3,x,1,infinity) Integrals Numerical integration . output cut. Summary ValueError: Integral is divergent. (What the—? a Maxima bug!) (This error should not occur in Sage after version 4.1.1) MAT 305: Mathematical Computing His vorpal sword in hand. John Perry Limits Differentiation Explicit differentiation Implicit differentiation Integration Integrals Numerical integration Fortunately, Sympy works great for this integral: Summary sage: integral(1/x**3,1,infinity, algorithm=’sympy’) 1/2 (Correct answer!) MAT 305: Mathematical Computing Snicker snack! John Perry Limits Differentiation Explicit differentiation Maxima bug confirmed Implicit differentiation Integration Integrals http://trac.sagemath.org/sage_trac/ticket/6420 Numerical integration Summary Maxima 5.13.0 was correct • in older versions of Sage • Bug introduced in Maxima 5.16.3 • Sage 4.0.2–4.1.1 • Bug fixed in Maxima 5.18.1 • Sage 4.1.2 Maxima 5.19 • − MAT 305: Mathematical ? Computing 1 picture = 1000 words John Perry sage: fplot = plot(sin(x),xmin=0,xmax=pi, Limits fill=’axis’) Differentiation Explicit differentiation Implicit differentiation sage: farea = integral(sin(x),x,0,pi) Integration Integrals sage: areatext = text(farea,(pi/2,0.5),fontsize=40) Numerical integration Summary sage: fplot+areatext 1 0.75 0.5 2 0.25 1 2 3 MAT 305: Mathematical Computing Numerical integration: Review John Perry Limits Differentiation Explicit differentiation Not all integrals can be simplified into elementary functions Implicit differentiation Integration Integrals Numerical integration Summary MAT 305: Mathematical Computing Numerical integration: Review John Perry Limits Differentiation Explicit differentiation Not all integrals can be simplified into elementary functions Implicit differentiation Integration Integrals Numerical integration Example Summary 2