Programs > Example: Finding Local Min/Max of Vectors
  
Example: Finding Local Min/Max of Vectors
1. Write a program, using a combination of conditional statements and loops, to find the local minimums of data sets:
Click to copy this expression
Function locmin scans input vector v and compares each element with its two neighbors. If element k is smaller than the element before it and the element following it, then it is a local minimum and its value and index are added to output vector m.
2. Utilize the above program to write a second program to find the maximums of the same data set:
Click to copy this expression
Function locmax uses the results received from calling locmin with -v. A local minimum of -v is a mirror image of a local maximum at the same index. Therefore, each value of a local minimum is multiplied by –1. The index-value pair is saved as a single element in output vector M.
3. Define function f that uses the built-in function dbinom that returns the probability density for value k:
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
4. Plot the above two functions:
Click to copy this expression
5. Save the elements of g(k,n) into an array so it can be passed to the programs that you defined:
Click to copy this expression
6. Use the built-in length function to see how many locmin and locmax points were found by your programs:
Click to copy this expression
Click to copy this expression
7. Use your programs to find the three local minimum points for the function:
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
8. Use your programs to find the two local maximum points for the function:
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
9. Plot the function and show its three local minimum and two local maximum points:
Click to copy this expression
10. Compare the obtained results using your programs with those obtained using the built-in functions localmin and localmax (which require as input an nx2 matrix):
a. Build the nx2 input matrix:
Click to copy this expression
Click to copy this expression
Click to copy this expression
b. Use the built-in functions to obtain the local minimum and maximum points:
Click to copy this expression
Click to copy this expression
The results agree.
* 
Always check the availability of built-in functions before writing new programs.