matlab fminunc
时间: 2023-10-22 07:07:15 浏览: 71
fminunc is a function in MATLAB that computes the unconstrained minimum of a scalar function. It uses the gradient of the function to find the minimum.
Syntax:
[x,fval,exitflag,output] = fminunc(fun,x0,options)
where:
- fun: the function handle to the objective function
- x0: the initial guess for the minimum
- options: a structure that specifies the options for the optimization algorithm
- x: the minimum found by the algorithm
- fval: the value of the objective function at the minimum
- exitflag: a flag that indicates whether the algorithm converged to a solution or not
- output: a structure that contains information about the optimization process
Example:
Suppose we want to find the minimum of the function f(x) = x^2 + 2x + 1. We can define the function handle as follows:
fun = @(x) x^2 + 2*x + 1;
We can then use fminunc to find the minimum:
[x,fval,exitflag,output] = fminunc(fun,-5);
The output will be:
x = -1.0000
fval = 0
exitflag = 1
output =
struct with fields:
iterations: 3
funcCount: 12
stepsize_min: 1.0000e-08
cg: [1x1 struct]
message: 'Optimization terminated: gradient small enough to continue searching for solutions.'
This indicates that the minimum is -1, the value of the function at the minimum is 0, and the algorithm converged to a solution.
阅读全文