Solve Blocks > Example: Optimization Functions
  
Example: Optimization Functions
This example shows how to pass a vector of guess values to either the Maximize or Minimize functions.
1. Define function f(x).
Click to copy this expression
2. Define x as a vector of guess values.
Click to copy this expression
3. Pass vector x as an argument to the Maximize function.
Click to copy this expression
The error occurs because PTC Mathcad expects x to be a single value and not a vector.
To avoid this error, we need to iterate over the guess values and send them one by one to the Maximize function, as shown below.
4. Use the range operator to define a vector of guess values.
Click to copy this expression
5. Define two new functions to perform maximize and minimize on function f(x).
Click to copy this expression
Click to copy this expression
6. Write a short program to iterate through the guess values and calculate the maximum and minimum for that guess value and save the result to vectors maximums and minimums, respectively.
Click to copy this expression
You can achieve the same results using the vectorization operator, as follows:
Click to copy this expression
Both Ms and Qs return a vector of two 7-element vectors containing the calculated maximums and minimums.
7. Define variable j as the length of the returned vector of maximum values.
Click to copy this expression
8. Plot function f(x) along with the maximum and minimum points returned by the program.
Click to copy this expression
9. Plot function f(x) along with the maximum and minimum points returned by the vectorization operator.
Click to copy this expression
Both methods return the exact results.