To find the maximum of a function in MATLAB, you can use the fminbnd function. This function finds the minimum of a function, so to find the maximum, you can simply negate the function and then find the minimum:
main.m215 chars9 lines
In this example, we define the function fun to maximize, which is a quadratic with a maximum at x=5. We then use fminbnd to find the minimum of the negated function -fun(x) over the interval [0, 10], which gives us the maximum of fun(x). The output xmax is the value of x that maximizes the function, and -ymax is the value of the maximum.
Note that fminbnd requires that the function be defined on a finite interval, so we provide it with the interval [0, 10] in this example. You may need to adjust the interval depending on the function you are maximizing.
gistlibby LogSnag