Clear Filters
Clear Filters

Optimization problem using fminunc

3 views (last 30 days)
Hancheng Zhu
Hancheng Zhu on 28 Nov 2023
Edited: Dyuman Joshi on 28 Nov 2023
I have an optimization problem
where x is a N-dimensional vector need to be optimized and a1,...,a5 are parameters. I have written the objective function as a m.file
f = fun(x,a1,a2,a3,a4,a5).
When I use fminunc(fun,x0,[],a1,a2,a3,a4,a5) to solve this function with initial x=x0, it goes wrong and says "the objective function is undefined at the initial point". I don't know how to solve it.
  1 Comment
Dyuman Joshi
Dyuman Joshi on 28 Nov 2023
Edited: Dyuman Joshi on 28 Nov 2023
I assume a1, a2, a3, a4 and a5 are numerical parameters. Pass the parameters like this -
f = @(x) fun(x, a1, a2, a3, a4, a5);
sol = fminunc(fun,x0);
In addition to John's answer, I would mention to check if fun() is defined for x=x0.
In case you are using conditions to define the function, there's a chance that fun might not have been defined at all for x=x0.

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 28 Nov 2023
Apparently, your objective function is not well defined at the initial point. I know, this is a wild guess, but that is exactly what fminunc told you. So what is the problem?
Use a different starting point for the optimization!
It is likely you chose some point where the function returns an inf or NaN. Start it somewhere else. WTP?

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!