Next: , Previous: Algebra Tutorial, Up: Algebra Tutorial

#### 2.5.1 Basic Algebra

If you enter a formula in Algebraic mode that refers to variables, the formula itself is pushed onto the stack. You can manipulate formulas as regular data objects.

     1:  2 x^2 - 6       1:  6 - 2 x^2       1:  (3 x^2 + y) (6 - 2 x^2)
.                   .                   .

' 2x^2-6 <RET>        n                   ' 3x^2+y <RET> *


(•) Exercise 1. Do ' x <RET> Q 2 ^ and ' x <RET> 2 ^ Q both wind up with the same result (‘x’)? Why or why not? See 1. (•)

There are also commands for doing common algebraic operations on formulas. Continuing with the formula from the last example,

     1:  18 x^2 - 6 x^4 + 6 y - 2 y x^2    1:  (18 - 2 y) x^2 - 6 x^4 + 6 y
.                                     .

a x                                   a c x <RET>


First we “expand” using the distributive law, then we “collect” terms involving like powers of ‘x’.

Let's find the value of this expression when ‘x’ is 2 and ‘y’ is one-half.

     1:  17 x^2 - 6 x^4 + 3      1:  -25
.                           .

1:2 s l y <RET>               2 s l x <RET>


The s l command means “let”; it takes a number from the top of the stack and temporarily assigns it as the value of the variable you specify. It then evaluates (as if by the = key) the next expression on the stack. After this command, the variable goes back to its original value, if any.

(An earlier exercise in this tutorial involved storing a value in the variable x; if this value is still there, you will have to unstore it with s u x <RET> before the above example will work properly.)

Let's find the maximum value of our original expression when ‘y’ is one-half and ‘x’ ranges over all possible values. We can do this by taking the derivative with respect to ‘x’ and examining values of ‘x’ for which the derivative is zero. If the second derivative of the function at that value of ‘x’ is negative, the function has a local maximum there.

     1:  17 x^2 - 6 x^4 + 3      1:  34 x - 24 x^3
.                           .

U <DEL>  s 1                  a d x <RET>   s 2


Well, the derivative is clearly zero when ‘x’ is zero. To find the other root(s), let's divide through by ‘x’ and then solve:

     1:  (34 x - 24 x^3) / x    1:  34 - 24 x^2
.                          .

' x <RET> /                  a x


     1:  0.70588 x^2 = 1        1:  x = 1.19023
.                          .

0 a =  s 3                 a S x <RET>


Now we compute the second derivative and plug in our values of ‘x’:

     1:  1.19023        2:  1.19023         2:  1.19023
.              1:  34 x - 24 x^3   1:  34 - 72 x^2
.                   .

a .                r 2                 a d x <RET> s 4


(The a . command extracts just the righthand side of an equation. Another method would have been to use v u to unpack the equation ‘x = 1.19 to ‘x’ and ‘1.19’, then use M-- M-2 <DEL> to delete the ‘x’.)

     2:  34 - 72 x^2   1:  -68.         2:  34 - 72 x^2     1:  34
1:  1.19023           .            1:  0                   .
.                                  .

<TAB>               s l x <RET>        U <DEL> 0             s l x <RET>


The first of these second derivatives is negative, so we know the function has a maximum value at ‘x = 1.19023’. (The function also has a local minimum at ‘x = 0’.)

When we solved for ‘x’, we got only one value even though ‘0.70588 x^2 = 1’ is a quadratic equation that ought to have two solutions. The reason is that a S normally returns a single “principal” solution. If it needs to come up with an arbitrary sign (as occurs in the quadratic formula) it picks ‘+’. If it needs an arbitrary integer, it picks zero. We can get a full solution by pressing H (the Hyperbolic flag) before a S.

     1:  0.70588 x^2 = 1    1:  x = 1.19023 s1      1:  x = -1.19023
.                      .                       .

r 3                    H a S x <RET>  s 5        1 n  s l s1 <RET>


Calc has invented the variable ‘s1’ to represent an unknown sign; it is supposed to be either +1 or -1. Here we have used the “let” command to evaluate the expression when the sign is negative. If we plugged this into our second derivative we would get the same, negative, answer, so ‘x = -1.19023’ is also a maximum.

To find the actual maximum value, we must plug our two values of ‘x’ into the original formula.

     2:  17 x^2 - 6 x^4 + 3    1:  24.08333 s1^2 - 12.04166 s1^4 + 3
1:  x = 1.19023 s1            .
.

r 1 r 5                   s l <RET>


(Here we see another way to use s l; if its input is an equation with a variable on the lefthand side, then s l treats the equation like an assignment to that variable if you don't give a variable name.)

It's clear that this will have the same value for either sign of s1, but let's work it out anyway, just for the exercise:

     2:  [-1, 1]              1:  [15.04166, 15.04166]
1:  24.08333 s1^2 ...        .
.

[ 1 n , 1 ] <TAB>            V M $<RET>  Here we have used a vector mapping operation to evaluate the function at several values of ‘s1’ at once. V M$ is like V M ' except that it takes the formula from the top of the stack. The formula is interpreted as a function to apply across the vector at the next-to-top stack level. Since a formula on the stack can't contain ‘$’ signs, Calc assumes the variables in the formula stand for different arguments. It prompts you for an argument list, giving the list of all variables in the formula in alphabetical order as the default list. In this case the default is ‘(s1)’, which is just what we want so we simply press <RET> at the prompt. If there had been several different values, we could have used V R X to find the global maximum. Calc has a built-in a P command that solves an equation using H a S and returns a vector of all the solutions. It simply automates the job we just did by hand. Applied to our original cubic polynomial, it would produce the vector of solutions ‘[1.19023, -1.19023, 0]’. (There is also an a X command which finds a local maximum of a function. It uses a numerical search method rather than examining the derivatives, and thus requires you to provide some kind of initial guess to show it where to look.) (•) Exercise 2. Given a vector of the roots of a polynomial (such as the output of an a P command), what sequence of commands would you use to reconstruct the original polynomial? (The answer will be unique to within a constant multiple; choose the solution where the leading coefficient is one.) See 2. (•) The m s command enables Symbolic mode, in which formulas like ‘sqrt(5)’ that can't be evaluated exactly are left in symbolic form rather than giving a floating-point approximate answer. Fraction mode (m f) is also useful when doing algebra.  2: 34 x - 24 x^3 2: 34 x - 24 x^3 1: 34 x - 24 x^3 1: [sqrt(51) / 6, sqrt(51) / -6, 0] . . r 2 <RET> m s m f a P x <RET>  One more mode that makes reading formulas easier is Big mode.  3 2: 34 x - 24 x ____ ____ V 51 V 51 1: [-----, -----, 0] 6 -6 . d B  Here things like powers, square roots, and quotients and fractions are displayed in a two-dimensional pictorial form. Calc has other language modes as well, such as C mode, FORTRAN mode, TeX mode and LaTeX mode.  2: 34*x - 24*pow(x, 3) 2: 34*x - 24*x**3 1: {sqrt(51) / 6, sqrt(51) / -6, 0} 1: /sqrt(51) / 6, sqrt(51) / -6, 0/ . . d C d F   3: 34 x - 24 x^3 2: [{\sqrt{51} \over 6}, {\sqrt{51} \over -6}, 0] 1: {2 \over 3} \sqrt{5} . d T ' 2 \sqrt{5} \over 3 <RET>  As you can see, language modes affect both entry and display of formulas. They affect such things as the names used for built-in functions, the set of arithmetic operators and their precedences, and notations for vectors and matrices. Notice that ‘sqrt(51)’ may cause problems with older implementations of C and FORTRAN, which would require something more like ‘sqrt(51.0)’. It is always wise to check over the formulas produced by the various language modes to make sure they are fully correct. Type m s, m f, and d N to reset these modes. (You may prefer to remain in Big mode, but all the examples in the tutorial are shown in normal mode.) What is the area under the portion of this curve from ‘x = 1’ to ‘2’? This is simply the integral of the function:  1: 17 x^2 - 6 x^4 + 3 1: 5.6666 x^3 - 1.2 x^5 + 3 x . . r 1 a i x  We want to evaluate this at our two values for ‘x’ and subtract. One way to do it is again with vector mapping and reduction:  2: [2, 1] 1: [12.93333, 7.46666] 1: 5.46666 1: 5.6666 x^3 ... . . [ 2 , 1 ] <TAB> V M$ <RET>                  V R -


(•) Exercise 3. Find the integral from 1 to ‘y’ of x sin(pi x) (where the sine is calculated in radians). Find the values of the integral for integers ‘y’ from 1 to 5. See 3. (•)

Calc's integrator can do many simple integrals symbolically, but many others are beyond its capabilities. Suppose we wish to find the area under the curve sin(x) ln(x)’ over the same range of ‘x’. If you entered this formula and typed a i x <RET> (don't bother to try this), Calc would work for a long time but would be unable to find a solution. In fact, there is no closed-form solution to this integral. Now what do we do?

One approach would be to do the integral numerically. It is not hard to do this by hand using vector mapping and reduction. It is rather slow, though, since the sine and logarithm functions take a long time. We can save some time by reducing the working precision.

     3:  10                  1:  [1, 1.1, 1.2,  ...  , 1.8, 1.9]
2:  1                       .
1:  0.1
.

10 <RET> 1 <RET> .1 <RET>        C-u v x


(Note that we have used the extended version of v x; we could also have used plain v x as follows: v x 10 <RET> 9 + .1 *.)

     2:  [1, 1.1, ... ]              1:  [0., 0.084941, 0.16993, ... ]
1:  ln(x) sin(x)                    .
.

' sin(x) ln(x) <RET>  s 1    m r  p 5 <RET>   V M $<RET>   1: 3.4195 0.34195 . . V R + 0.1 *  (If you got wildly different results, did you remember to switch to Radians mode?) Here we have divided the curve into ten segments of equal width; approximating these segments as rectangular boxes (i.e., assuming the curve is nearly flat at that resolution), we compute the areas of the boxes (height times width), then sum the areas. (It is faster to sum first, then multiply by the width, since the width is the same for every box.) The true value of this integral turns out to be about 0.374, so we're not doing too well. Let's try another approach.  1: ln(x) sin(x) 1: 0.84147 x + 0.11957 (x - 1)^2 - ... . . r 1 a t x=1 <RET> 4 <RET>  Here we have computed the Taylor series expansion of the function about the point ‘x=1’. We can now integrate this polynomial approximation, since polynomials are easy to integrate.  1: 0.42074 x^2 + ... 1: [-0.0446, -0.42073] 1: 0.3761 . . . a i x <RET> [ 2 , 1 ] <TAB> V M$ <RET>         V R -


Better! By increasing the precision and/or asking for more terms in the Taylor series, we can get a result as accurate as we like. (Taylor series converge better away from singularities in the function such as the one at ln(0), so it would also help to expand the series about the points ‘x=2’ or ‘x=1.5’ instead of ‘x=1’.)

(•) Exercise 4. Our first method approximated the curve by stairsteps of width 0.1; the total area was then the sum of the areas of the rectangles under these stairsteps. Our second method approximated the function by a polynomial, which turned out to be a better approximation than stairsteps. A third method is Simpson's rule, which is like the stairstep method except that the steps are not required to be flat. Simpson's rule boils down to the formula,

     (h/3) * (f(a) + 4 f(a+h) + 2 f(a+2h) + 4 f(a+3h) + ...
+ 2 f(a+(n-2)*h) + 4 f(a+(n-1)*h) + f(a+n*h))


where ‘n’ (which must be even) is the number of slices and ‘h’ is the width of each slice. These are 10 and 0.1 in our example. For reference, here is the corresponding formula for the stairstep method:

     h * (f(a) + f(a+h) + f(a+2h) + f(a+3h) + ...
+ f(a+(n-2)*h) + f(a+(n-1)*h))


Compute the integral from 1 to 2 of sin(x) ln(x)’ using Simpson's rule with 10 slices. See 4. (•)

Calc has a built-in a I command for doing numerical integration. It uses Romberg's method, which is a more sophisticated cousin of Simpson's rule. In particular, it knows how to keep refining the result until the current precision is satisfied.

Aside from the commands we've seen so far, Calc also provides a large set of commands for operating on parts of formulas. You indicate the desired sub-formula by placing the cursor on any part of the formula before giving a selection command. Selections won't be covered in the tutorial; see Selecting Subformulas, for details and examples.