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:
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:
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:
4. Plot the above two functions:
5. Save the elements of g(k,n) into an array so it can be passed to the programs that you defined:
6. Use the built-in length function to see how many locmin and locmax points were found by your programs:
7. Use your programs to find the three local minimum points for the function:
8. Use your programs to find the two local maximum points for the function:
9. Plot the function and show its three local minimum and two local maximum points:
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:
b. Use the built-in functions to obtain the local minimum and maximum points:
The results agree.
Always check the availability of built-in functions before writing new programs.