To minimize a function with bounds in Matlab, you can use the fmincon function. This function minimizes a constrained nonlinear multivariable function, subject to optional linear constraints and variable bounds.
Here's an example code:
main.m397 chars15 lines
In this example, we define the function to minimize (fun = @(x) x(1)^2 + x(2)^2), set the lower and upper bounds for the variables (lb = [-1, -1]; ub = [1, 1]), and call the fmincon function to minimize the function given the bounds. The [] inputs specify that we do not have any linear constraints to include.
Finally, we display the results of the optimization.
gistlibby LogSnag