From: Petr Pošík on
Hi.

I have the following setup:
1) a function of several variables implemented in MATLAB which I want to optimize, i.e. to find variable values for which the function gives minimal value,
2) a very good local optimization procedure implemented in C which I want to use repeatedly to optimize the above mentioned function, and
3) a procedure for generating the local search starting points implemented in MATLAB again.

Thus, I need to run a MATLAB function which will iteratively generate a starting point and run a C-function local search procedure from that point. I must pass a function defined in MATLAB to that C-implemented local search.

Is it possible?

The local search procedure is a standard implementation of certain method - it is well possible that by trying to translate it to MATLAB I would not only loose some performance, but also introduce some bogs, which I certainly do not want to.

Thanks for any suggestions.

Petr
From: Jan Simon on
Dear Petr,

you can feed your C-Mex function with a function handle and call it from the C part with mexCallMATLAB and the 'feval' command. Calling M-code from C is not really fast, so it will be a bottleneck if your function is called millions of times.

Good luck, Jan
From: Petr Pošík on
"Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <i1mnk6$qud$1(a)fred.mathworks.com>...
> you can feed your C-Mex function with a function handle and call it from the C part with mexCallMATLAB and the 'feval' command. Calling M-code from C is not really fast, so it will be a bottleneck if your function is called millions of times.

Jan, thanks for your reply.
Yes, indeed, the function will be called millions of times. Do you see any other possibility of using the original C code for the local optimization procedure inside MATLAB that would not represent a performance bottleneck?
From: Jan Simon on
Dear Petr,

> 1) a function of several variables implemented in MATLAB which I want to optimize, i.e. to find variable values for which the function gives minimal value,

What about converting this function to C also? This should not be too complicated but saves a lot of time by avoiding calls of mexCallMATLAB.

Anyhow, I'd implement both methods to support debugging.

Kind regards, Jan