# Using fminunc with blackbox

1 回表示 (過去 30 日間)
jeff 2016 年 3 月 23 日

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!

サインインしてコメントする。

### 採用された回答

Alan Weiss 2016 年 3 月 23 日
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 件のコメントなしを表示なしを非表示
jeff 2016 年 3 月 23 日
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 2016 年 3 月 23 日
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

サインインしてコメントする。

### その他の回答 (1 件)

sanya gode 2019 年 10 月 29 日
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 ?

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by