Using fminunc with blackbox

5 views (last 30 days)
jeff
jeff on 23 Mar 2016
Answered: sanya gode on 29 Oct 2019
A blackbox code (obj_b3.p) defines a function that takes a vector of length 2 and returns some value based on that function. I want to find the global minimizer, and instead of guess and check I want to use fminunc (have never used before, have looked at doc and help and tried googling examples but they all have known functions that they make anonymous). To do this I intend on using a lot of x0 (guess) values.
how do i set up the basic code?
what I've tried:
v = [1;1] % creates a vector of length 2 (blackbox code won't accept a 1,2, only a 2,1)
x0=[0;0]; % Initial guess for fminunc
v = fminunc(obj_b3,x0)
matlab returns that there aren't enough inputs.
Can someone post a simple example of how to do this using a blackbox.m file (hope its obvious that the blackbox.p file does not allow one to actually see the code to evaluate).
Thanks!

Accepted Answer

Alan Weiss
Alan Weiss on 23 Mar 2016
fun = @obj_b3;
x0 = [0;0];
x = fminunc(fun,x0)
You might want to consult the documentation on optimizing a simulation or ODE in case your black-box function does not distinguish between nearby values very well.
Alan Weiss
MATLAB mathematical toolbox documentation
  2 Comments
jeff
jeff on 23 Mar 2016
I actually just used x = fminunc(@obj_b3,x) (thats without the options), but your answer is exactly the same idea. Can you explain why the @ symbol is necessary in matlab? I have seen minimal documentation on it and it just eludes me.
Alan Weiss
Alan Weiss on 23 Mar 2016
You should read about function handles.
The point is, how can you tell MATLAB to work with a function? I mean, you want to take a function ( fminsearch ) that operates on another function (your objective). So how should you pass the objective? The answer is with a handle to the objective.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

More Answers (1)

sanya gode
sanya gode on 29 Oct 2019
Hello,
I am working on Equivalent Circuit Modeling for estimating the battery parametrs. I read in MAthworks documentation that the model can be Blackboxed for sharing with certain customers so that they can modeify it accourding o the application for which they are using the battery without knowing the internal parameters. I read that once the model is created we need to generate the code and than we have create a new simulink model based on the generated code.
Can you please elaborate on how exactly do we need to blackbox the model based on the generated code ?
Your answers will be very helpful and appreciated.
Thanks in advance.

Community Treasure Hunt

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

Start Hunting!