Part II. Calculations and Calculus with Maple
Section 3. Functions as Transformations
Functions are introduced in calculus in the general form y = f (x). The letters x and y are called the independent and dependent variables respectively and the letter f is often referred to as the "function rule". In this manual we will refer to f as a function or sometimes a function rule or a transformation rule or as a transformation. Many applications of calculus are easier to handle when the relevant functions are entered as transformations.
Using a transformation to define a function
Consider the function
that was introduced in the calculus example in Section 2.
Another calculus example: Find the tangent line to the graph of this function at x = 2 and plot it.
Begin with the definition of the function as a transformation using what is called "arrow notation". Read the input (and output) as "f transforms x to x^2*sin(x)".
> | f := x -> x^2*sin(x); |
The input arrow is made by typing a minus sign and an input prompt: ->. Do not put a space between them.
The function is named f, but any name can be used. Likewise, although the letter x was chosen to denote the independent variable, any other symbol would serve exactly the same purpose, even an assigned variable.
Having done this, the value of f when x = a is obtained by entering f (a). Recall that when variables are used to define a function, function evaluation requires the use of the eval (or subs) procedure.
> | f(0), f(1), f(2), f(3); evalf(%,3); |
The following plot shows the graph of f over the interval [0,3].
> | plot( f(x), x=0..3); |
The derivative function, also as a transformation, is computed with the following entry
D(f);
D is called the derivative operator.
> | D(f); |
Note that D(f) is indeed a function defined by a transformation, the arrow tells us so. If the derivative formula is needed, simply enter D(f)(x).
> | D(f)(x); |
The derivative formula can also be obtained by using the derivative procedure, diff, applied to f (x)
diff(f(x),x);
> | diff(f(x),x); |
Since D(f) is the derivative function, a derivative value, say at x = a, is obtained by entering D(f)(a). Read this as "The derivative of f at a".
For example, the derivative of f at 2 is computed below, first exactly and then approximately.
> | D(f)(2); evalf(%); |
This is the slope of the tangent line to the graph of f at the point (2 , f (2)).
The function whose graph is the tangent line at ((2 , f (2)) is defined in the next entry. We name it T.
> | T := x -> f(2) + D(f)(2)*(x - 2); |
The entry T(x) will display the formula for the line in exact terms. Apply evalf to see the formula in decimal form.
> | T(x); evalf[4](%); |
The next plot shows the graph of f and T together over the interval from 0 to 3. The graph of f is colored red, the tangent line is blue.
> | plot( [f(x),T(x)], x=0..3, color=[red,blue]); |
Parametric plots
Tangent lines are usually drawn over shorter intervals. This can be accomplished in Maple by adding the tangent line to the plot as a parametrized curve. Maple uses the following entry to designate a parametrized curve in 2-space
[t,T(t),t=1.5..2.5]
Just put this list into the plot procedure, along with f (x). Everything else stays the same.
> | plot( [f(x),[t,T(t),t=1.5..2.5]], x=0..3, color=[red,blue]); |
Plot several tangent lines
Several tangent lines can be plotted by defining the function L so that L(a,x) is the formula for the tangent line to the graph of f at the point (a , f (a)). L is a function of two variables. See below.
> | L := (a,x) -> f(a) + D(f)(a)*(x - a); |
The sequence operator can then be used to make a sequence of tangent line parametrizations at points evenly spaced along the curve. The sequence is named Lines (output suppressed). There are four of them.
> | Lines := [t,L(a,t),t=a-0.3..a+0.3] $ a=0..3: |
Now put Lines into the plot procedure (along with f (x)).
> | plot( [f(x), Lines], x=-0.3..3, color=[red,blue$4]); |
Note the use of the sequence operator, $, inside of the plot procedure to make the four "blue"s that are needed to color the tangent lines.
The next plot is a little fancier. The axes have been removed (axes=none) and the equation
thickness=[1,2$4]
tells Maple to draw the curve at the default thickness and make the four tangent line segments twice as thick. A title has also been added to the plot. The text for a title must be placed inside of double quotes.
> | plot( [f(x), Lines], x=-0.3..3, color=[red,blue$4], axes=none,
thickness=[1,2$4], title="Four Tangent Lines"); |
Find the length of the curve
The integral for the length of a curve defined by y = f (x) over the interval x = a to x = b is
.
Arc length integrals are notoriously difficult to evaluate exactly, and this one is no exception.
> | ArcLength = int(sqrt(1+D(f)(x)^2),x=0..3); |
As good as Maple is with integration it is unable to find an antiderivative. Here is a 10 digit approximation.
> | evalf(%); |
Let's see how this compares to the sum of the lengths of the three secant line segments determined by the four points (k , f (k)), k = 0, 1, 2, 3. The kth segment is the hypotenuse of a right triangle with base 1 and side length
, k = 0, 1, 2. Add the lengths using the add procedure, then evaluate in floating point form.
> | SecantApprox = add( sqrt(1 + (f(k+1)-f(k))^2), k=0..2): evalf(%); |
Now approximate the arc length with the sum of the lengths of three tangent line segments. The kth segment will be the tangent line at the point (0.5+k , f (0.5+k)) stretching over a unit interval on the x axis. Right triangles with base 1 can still be used, but the side of the triangle has length (plus or minus) D(f)(0.5+k). You may recognize the following addition formula as a midpoint approximation to the integral.
> | TangentLineApprox = add( sqrt(1 + D(f)(0.5+k)^2), k=0..2): evalf(%); |
Both approximations are too small, but they are fairly close to one another. Let's try to see why.
The following picture shows the three tangent line segments used for TangentLineApprox.
> | plot( [f(x),[t,L(0.5+k,t),t=k..k+1]$k=0..2], x=0..3,
color=[red,blue$3], axes=none, title="Tangent Lines"); |
The three secant lines are shown below. The kth line joins (k , f (k)) to (k+1 , f (k+1)), k=0, 1, 2.
> | plot( [f(x),[[k,f(k)],[k+1,f(k+1)]]$k=0..2], x=0..3,
color=[red,blue$3], axes=none, title="Secant Lines"); |
The pictures make it clear why the approximations are too small and are roughly the same.
An area calculation
Find the area between the graph of f and the graph of the secant line joining the endpoints of the graph over the interval from 0 to 3.
The function defined by the secant line will be named S. It's graph is a line through the origin with slope
.
> | S := x -> (f(3)-f(0))/(3-0)*x; |
We want to find the area of the region between the two curves shown below.
> | plot( [f(x),S(x)], x=0..3, color=red); |
The area equals the integral of the absolute value of f (x) - S(x) from x = 0 to x = 3. Maple uses
abs(x)
for the absolute value of x.
> | Area = int(abs(f(x)-S(x)),x=0..3); |
Because of the absolute value, Maple is unable to find the exact value of the integral. Evaluate numerically using evalf.
> | evalf(%); |
In an attempt to find an exact area formula we might try to get the exact value for the x coordinate of the point of intersection of the two graphs near x = 0.5. Try the solve procedure.
> | solve( f(x)=S(x), {x}); |
Maple found the solution x = 0. It did not find x = 3 nor did it find the exact x value we want (according to the graph a little to the right of x = 0.5). Try fsolve with a specified range for x.
> | b := fsolve( f(x)=S(x), x, 0.5..1); |
Using b another integral formula for the area can be used, this one without any absolute value signs.
> | Area2 = int(S(x)-f(x),x=0..b) + int(f(x)-S(x),x=b..3); |
Maple was able to integrate exactly, but the answer is still approximate because b is in floating point form.
Higher derivatives
Using the diff procedure, the formula for the second derivative of f(x) is obtained using
diff(f(x),x,x);
Recall that f was defined earlier in this section.
> | f(x);
diff(f(x),x,x); |
The third derivative requires the sequence x,x,x, or x$3, and so on.
> | diff(f(x),x$3); |
Using the D operator, the derivative function is D(f) and the second derivative is either D(D(f)) or
(D@@2)(f);
> | (D@@2)(f); |
The third derivative is D(D(D(f))), or D@@3)(f), and so on.
> | (D@@3)(f); |
For example, the following entry defines the function P as the second order Taylor polynomial approximation to the function f at the point a = 2.
> | P := x -> f(2) + D(f)(2)*(x-2) + 1/2*(D@@2)(f)(2)*(x-2)^2; |
This is referred to as a quadratic approximation. Enter P(x) to see the exact formula and apply evalf to see the quadratic in floating point form.
> | P(x); evalf(%,4); |
The quadratic approximation plots like this.
> | plot( [f(x),P(x)], x=0..3, color=[red,blue]); |
Near x = 2 the approximation is very good. The next plot uses the axes=framed option to obtain the kind of picture that is popular in engineering texts. The two curves are colored black, and the approximation is plotted using the DASH line style.
> | plot( [f(x),P(x)], x=1.5..2.5, axes=boxed, color=black,
linestyle=[SOLID,DASH]); |
Maple has a procedure named mtaylor that will output Taylor polynomial approximations. The syntax for the second order Taylor polynomial for the function f at x = 2 is shown below.
> | mtaylor( f(x), x=2, 3); |
Unevaluated derivatives
If an undefined function, say g(x), is put into diff, then Maple outputs an unevaluated derivative.
> | diff(g(x),x); |
The same is true for higher order derivatives.
> | diff(g(x),x,x); |
The chain rule
Maple is smart about differentiation. For example, the following entry shows that Maple knows the chain rule.
> | diff(g(x(t)),t); |
The output reads as "the derivative of g evaluated at x(t), multiplied by the derivative of x(t) with respect to t". This is the chain rule. The next entry illustrates one of many multivariable chain rule formulas: g is now a function of two variables, x and y are each functions of one variable.
> | diff(g(x(t),y(t)),t); |
The expression
in the output is standard notation for the partial derivative of g with respect to its first variable evaluated at (x(t),y(t)). Similarly,
is the partial derivative of g with respect to its second variable evaluated at (x(t),y(t)).
Here is one more example: g is a function of 3 variables. x and phi are both functions of one variable. The input asks for the derivative of
with respect to t. The output is the chain rule formula for this derivative.
> | diff(g(x(phi(t)),phi(t),t),t); |
Just a little bit about differential equations
Unevaluated derivatives are exactly what are needed to define a differential equation. The next entry defines what is called a first order, linear, differential equation. The equation is given the name DE.
> | DE := diff(y(t),t) + y(t) = t; |
The unknown function is y, it is a function of the variable t and everywhere it appears in the equation it must be entered as y(t).
The next equation, named DE2, is called a Bernoulli equation. The unknown function is x, it is also a function of t and must be entered as x(t).
> | DE2 := diff(x(t),t) + x(t) = t^2*x(t)^3; |
Maple knows all about these equations. For example, it can solve both of them symbolically using a procedure called dsolve. To obtain the solution to DE just enter
dsolve( DE )
> | dsolve( DE ); |
The output is the solution equation; _C1 denotes an arbitrary constant that arises in the solution process.
The next entry asks for the solution to DE2 satisfying the condition x(0) = 1. The solution equation is named soln.
> | soln := dsolve( {DE2, x(0)=1} ); |
Here is the graph of the solution. (Read rhs as "right hand side of").
> | plot( rhs(soln), t=-5..5); |
Working with the solution
The solution to DE2 satisfying x(0) = 1 looks like a function, but it is not. It is the solution equation and remember,
equations do not assign values.
That is why we had to refer to its "right hand side" to display the graph.
To get specific values out of "soln" use the eval procedure. The output is a nice looking equation.
> | eval(soln,t=0); evalf(%); |
The next input uses fsolve to find the positive t value such that x(t) = 0.4 and then checks it. See the graph.
> | fsolve( rhs(soln)=0.4, {t}, 0..2);
eval(soln,%); |
To check that soln is a solution to DE2, use subs and simplify.
> | subs(soln,DE2); |
> | simplify(%); |
This output is an identity, true for all t, telling us that "soln" is a solution to DE2 valid over the entire t axis. (The expression in the denominator of the formula for x(t) is always positiveŃcan you see why?)
Use the unapply procedure to make functions out of expressions
It is possible to make a function out of "soln". The natural way to do it is with the following entry
f := t -> rhs(soln)
However, Maple recommends that a procedure called unapply be used instead. It has the following syntax
unapply( expression, variable)
The output is "expression" as a function of "variable". In our situation it works like this.
> | f := unapply(rhs(soln),t); |
> | plot( f(t), t=-5..5); |
Repeat after me: "The unapply procedure is the preferred way to make a function out of an expression."
Go to: Next Section Previous Section