find the extramum in matlab

Matlab provides several functions for optimizing functions and finding the minimum or maximum values. One of the commonly used functions in Matlab for finding the minimum or maximum of a function is fminbnd.

Syntax:

main.m
[x, fval, exitflag] = fminbnd(fun, xmin, xmax)
47 chars
2 lines

where,

  • fun: a function handle that takes a scalar input and returns a scalar value. This function should be continuous between xmin and xmax.
  • xmin: the lower bound of the interval.
  • xmax: the upper bound of the interval.
  • x: the value that minimizes or maximizes the function.
  • fval: the function value at x.
  • exitflag: a flag that indicates if the function converged or not.

To find the maximum of a function f(x) between a starting point xmin and an ending point xmax, the negative of the function -f(x) can be minimized using fminbnd.

Example:

main.m
%Define the function to be optimized
fun = @(x) -x^2 + 2*x + 3;

%Set the range of values for x
xmin = -10;
xmax = 10;

%Call the optimization function
[x, fval, exitflag] = fminbnd(fun, xmin, xmax);

%Print the results
disp(['x = ', num2str(x)])
disp(['f(x) = ', num2str(-fval)])
281 chars
14 lines

In this example, fminbnd is used to find the maximum value of the function -x^2 + 2*x + 3 between the range -10 and 10. The output shows that the maximum occurs at x=1 and the maximum value is f(x) = 4.

related categories

gistlibby LogSnag