K. Cooper - Washington State University
Transcript of K. Cooper - Washington State University
Limits Differentiation Integration Solving ODEs
Machine Calculus
K. Cooper1
1Department of MathematicsWashington State University
2018
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Sympy Calculus
Sympy has a full array of Integral and Differential Calculuscapability.
It can find limits, derivatives, antiderivatives, evaluate Taylorseries, and solve differential equations.
It can solve linear and nonlinear systems of equations.
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Limits
Sympy loves to compute limits.
sp.limit(sp.sin(x)/x,x,0)produces an answer of 1.
sp.limit((3*x*x+2*x-1)/(2*x*x-101*x),x,sp.oo)gives 3/2.
Note that infinity in Sympy is called oo. One can alsosometimes use an IEEE 754 infinity, called inf.
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Limits
We can also do one-sided limits:
sp.limit(1/x,x,0,’+’)oosp.limit(1/x,x,0,’-’)-oo
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Differentiation
The basic command is diff.diff(expr,var)
You can leave off the variable if it is clear.diff(x*x+2*x+1)gives 2*x + 2, butdiff(alpha*x*x+2*x+1)gives an error:Since there is more than one variable in the expression, the
variable(s) of differentiation must be supplied to differentiate
diff(alpha*x*x+2*x+1,x)produces 2*alpha*x + 2
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Derivatives
We could call the diff function as a method of the expression.
(alpha*x*x+2*x+1).diff(x)yields the obvious answer.
To get higher order derivatives, we just add one moreargument: the order of the derivative.
diff(alpha*x*x+2*x+1,x,2)(alpha*x*x+2*x+1).diff(x,2)both give 2*alpha.
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Example: Newton’s Method
Find all points where sin(x) has value 34 .
def ntn_mth(expr,var,x0,tol):deriv = expr.diff(var)digits = -log10(tol)+10x1 = x0-expr.subs(var,x0).evalf(digits)/ \deriv.subs(var,x0).evalf(digits)
while abs(x1-x0)>tol:x0 = x1x1 = x0-expr.subs(var,x0).evalf(digits)/ \deriv.subs(var,x0).evalf(digits)
return x1
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Newton Results
After importing this as nm, we call this as e.g.
nm.ntn_mth(sp.sin(x)-sp.Rational(3,4),x,0,.1e-25)nm.ntn_mth(sp.sin(x)-sp.Rational(3,4),x,3,.1e-25)
yielding 0.848062078981481008052944338998418080and 2.29353057460831223040969904428108480,respectively. Obviously we can just add integer multiples of 2πto get the rest.
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Amusement
Incidentallynm.ntn_mth(x*x-.5,x,1,.1e-25)produces 1/
√2 to 24 digits:
0.707106781186547524400844362104849039
Curiously,sp.sqrt(.5).evalf(25)gave the answer 0.7071067811865475727373109. TheNewton method gave a different answer: more, or lessaccurate?
sp.sqrt(sp.Rational(1,2)).evalf(25)yielded 0.7071067811865475244008444.
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Integration and Pretty Printing
We can antidifferentiate expressions.
expr=alpha*x*x+2*x+1sp.integrate(expr,x)gives alpha*x**3/3 + x**2 + x
If we are evaluating an integral, rather than an antiderivative,we need limits.
sp.integrate(expr,[x,0,x])produces alpha*x**3/3 + x**2 + x.sp.integrate(expr,(x,-1,x))yields alpha*x**3/3 + alpha/3 + x**2 + x.
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Pretty Printing
In this situation, some better presentation of results would help.Sympy can “pretty print” results.
sp.pprint(sp.integrate(expr,x))3
alpha*x 2-------- + x + x
3
We can turn on “pretty printing” for all results using the function
sp.init_printing()
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Integrate Anything
If the integral can’t be done using the Fundamental Theorem,then we can do it numerically.
sp.integrate(sp.sin(x**2+sp.exp(x)),(x,-1,2)))just returns the statement.
Butsp.integrate(sp.sin(x**2+sp.exp(x)),(x,-1,2)).evalf(20)gives 1.2536589539827120186.
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Inert Functions
Often we want to “set up, but do not evaluate” integrals orderivatives or whatever. Sympy has special inert functions forthat.
a = sp.Integral(x**2,x)gives/
|| 2| x dx|/
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Inert Evaluation
To evaluate an inert function, use the doit() method.
a.doit()results in3
x-3
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Solving Equations
Sympy can solve systems of equations.
sp.solve(expr,x)gives
____________ / ____________ \\/ -alpha + 1 - 1 -\\/ -alpha + 1 + 1/
[------------------, --------------------]alpha alpha
Note that no equals signs are required. The understanding isthat the expression is to be equal to zero. Thus, to solve x2 = 5we would usesp.solve(x*x-5,x).
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Solving Equations
... systems of equations.
sp.solve([expr,alpha+3],[x,alpha])produces [(-1/3, -3), (1, -3)]
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Example
Find the value of x where 2x2 − x − 1 is minimized.
sp.solve(sp.diff(2*x**2-x-1,x),x)yields [1/4].
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Example
Find the arc length of e−x2between -1 and 1.
integ = 1+sp.diff(sp.exp(-x*x),x)**2arclen=sp.integrate(integ,[x,-2,2]).evalf(20)produces 5.2518928987736186293.
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Formal Derivatives
To solve differential equations, Sympy needs to be able tocreate variables that depend on other variables: i.e. formalfunctions.
y = sp.Function(’y’)makes y into a function depending on other unspecifiedvariables.
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Derivatives
Once we have formal functions, we need to be able to takeformal derivatives of them.
yprime = sp.Derivative(y(t),t)producesd--(y(t))dt
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Derivatives
Since the independent variables were unspecified, we can dowhat we need.
ysubt = sp.Derivative(y(t,x),t)yieldsd-(y(t, x))dt
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Ordinary Differential Equations
Now we can solve DEs:
sp.dsolve(sp.Derivative(y(t),t)-y(t),y(t))results in
ty(t) = C1*e
sp.dsolve(sp.Derivative(y(t),t,2)+9*y(t),y(t))givesy(t) = C1*sin(3*t) + C2*cos(3*t)
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Components of an Expression
Often we need to pick off pieces of an expression. Eachexpression has an args array containing the pieces.
expr = alpha*x*x+2*x+1expr.argsreturns (1,2*x,alpha*x**2).
Thus, if we want just the linear term, we can ask for
expr.args[1]which gives 2*x
Remember that 2*x is itself an expression, which means it hasan args method. Thus, expr.args[1].args[0] is 2.
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Initial Value Problems
If we need to evaluate those constants, we can do it using theordinary solve() method. Here is one way (among many).We will solve y ′′ + 9y = 0, y(0) = 1, y ′(0) = −1.
eqn=sp.dsolve(sp.Derivative(y(t),t,2)+9*y(t),y(t))soln = eqn.args[1]This means soln = C1*sin(3*t) + C2*cos(3*t).
Now we just solvesp.solve(\[soln.subs(t,0)-1,diff(soln,t).subs(t,0)+1], \[C1,C2])which gives {C1: -1/3, C2: 1}
Cooper Washington State University
Symbolic Calculus
Limits Differentiation Integration Solving ODEs
Zillions
There are a zillion more things we can do and know aboutSympy. Like all such packages, it is desperately complex whenyou get into it.
Still, I hope we have seen that some easy things to do areeasy. . .
Cooper Washington State University
Symbolic Calculus