 # How to use minimizer (e.g., fminunc) with blackbox function

Via an iterative process, I would like to find coefficients for a blackbox objective function such that the sum of squared errors (SSE) between predicted and observed values are minimized. The objective function is nonlinear, has eight coefficients, and must be solved in a program that is independent of Octave.

Given the situation above, what I would like to do is enter an initial set of coefficients for the objective function, pass the coefficients to a minimizer, have the minimizer prompt me for the SSE, and then have the minimizer indicate the set of coefficients that should be used for the next iteration. SSEs from the objective function will be copied into Octave and when new coefficients are indicated by Octave, they will be used to solve the objective function.

The following script file and function were created to test the concept described above. The script and function work fine when f_x_y is computed by Octave. Values of x, y and f_x_y are displayed by printf commands. When a prompt is used for entering values of f_x_y, the script and function do not work. After two iterations, the following error appears:

``````error: binary operator '-' not implemented for 'cell' by 'cell' operations
error: called from
__fdjac__ at line 45 column 17
fminunc at line 213 column 12
TestScript at line 8 column 24
``````

I would greatly appreciate any suggestions for how to manually enter computed SSEs from a blackbox objective function into Octave so that one of Octave’s minimizers can help me select new coefficients for the objective function.

``````%TestScript.m
x_0 = 1;
y_0 = 1;

InitCoefs = [x_0  y_0];

f_x_y = @(InitCoefs) Get_f_x_y(InitCoefs);
[FinalCoefs,f_x_y_min] = fminunc(f_x_y,InitCoefs)
%End TestScript.m
``````
``````function f_x_y = Get_f_x_y(Coefs)

x = Coefs(1);
y = Coefs(2);

%Given output values of x and y from fminunc(), prompt the user to
%manually enter a value of f_x_y. The manually enter value of f_x_y
%will be used by fminunc() to select new values of x and y.

printf("The output value of x from fminunc() is x = %22.19e\n", x)
printf("The output value of y from fminunc() is y = %22.19e\n", y)

z = sqrt(x^2 + y^2);
f_x_y = z;

printf("The computed value of f_x_y is f_x_y = %22.19e\n", z)

%prompt={'Enter a value for z = f(x,y): '};
%title='Given x and y, prompt user for new z-value';
%f_x_y = inputdlg(prompt,title);     %User must enter a number

end %End Get_f_x_y.m
``````

`inputdlg` returns a cellstring. IIUC, you’d like function `Get_f_x_y` to return a double precision number.
You could do that e.g. by something like that:

``````input_str = inputdlg (prompt, title);
% Maybe do some consistency checks on the cellstring here
f_x_y = str2double (input_str{1})
``````

HTH

HTH,

Using str2double() made the script file and function work. Thank-you so much. 