Next: , Previous: , Up: Answers to Exercises   [Contents][Index]

#### 2.7.26 List Tutorial Exercise 8

The first step is to build a list of values of ‘x’.

```1:  [1, 2, 3, ..., 21]  1:  [0, 1, 2, ..., 20]  1:  [0, 0.25, 0.5, ..., 5]
.                       .                       .

v x 21 RET              1 -                     4 /  s 1
```

Next, we compute the Bessel function values.

```1:  [0., 0.124, 0.242, ..., -0.328]
.

V M ' besJ(1,\$) RET
```

(Another way to do this would be 1 TAB V M f j.)

A way to isolate the maximum value is to compute the maximum using V R X, then compare all the Bessel values with that maximum.

```2:  [0., 0.124, 0.242, ... ]   1:  [0, 0, 0, ... ]    2:  [0, 0, 0, ... ]
1:  0.5801562                      .                  1:  1
.                                                     .

RET V R X                      V M a =                RET V R +    DEL
```

It’s a good idea to verify, as in the last step above, that only one value is equal to the maximum. (After all, a plot of ‘sin(x)’ might have many points all equal to the maximum value, 1.)

The vector we have now has a single 1 in the position that indicates the maximum value of ‘x’. Now it is a simple matter to convert this back into the corresponding value itself.

```2:  [0, 0, 0, ... ]         1:  [0, 0., 0., ... ]    1:  1.75
1:  [0, 0.25, 0.5, ... ]        .                        .
.

r 1                         V M *                    V R +
```

If a = had produced more than one ‘1’ value, this method would have given the sum of all maximum ‘x’ values; not very useful! In this case we could have used v m (`calc-mask-vector`) instead. This command deletes all elements of a “data” vector that correspond to zeros in a “mask” vector, leaving us with, in this example, a vector of maximum ‘x’ values.

The built-in a X command maximizes a function using more efficient methods. Just for illustration, let’s use a X to maximize ‘besJ(1,x)’ over this same interval.

```2:  besJ(1, x)                 1:  [1.84115, 0.581865]
1:  [0 .. 5]                       .
.

' besJ(1,x), [0..5] RET            a X x RET
```

The output from a X is a vector containing the value of ‘x’ that maximizes the function, and the function’s value at that maximum. As you can see, our simple search got quite close to the right answer.

Next: List Tutorial Exercise 9, Previous: List Tutorial Exercise 7, Up: Answers to Exercises   [Contents][Index]