3.0

3.0 | 2 ratings Rate this file 19 downloads (last 30 days) File Size: 2.35 KB File ID: #21563

Generalized Objective Function

by Anthony Kendall

 

24 Sep 2008 (Updated 29 Sep 2008)

Code covered by BSD License  

Allows any MATLAB function to easily be optimized

Download Now | Watch this File

File Information
Description

In order to optimize any MATLAB function, there are two options: 1) rewrite the function to take only 1 array of inputs, reading all others from a file, perhaps, or from a global variable (undesirable), or 2) write an interface layer that handles this.

This function provides an interface layer that is completely general, and allows any MATLAB function to be used in an optimization loop. To do this, the first call to this function initializes it, storing the data necessary to evaluate and calculate the objective function for optimization. Then, this function 'objective_function.m' is called by an optimization routine such as fminsearch.

Note, this can be used with any optimization routine. It serves as an interface layer between the routine and the function to optimize, while also calculating the objective function value.

There are several functional forms of objective function written in here already, including sum of squared residuals, mean absolute error, mean percent absolute error, and log mean absolute error. Adding others is trivial.

This does NOT require the optimization toolbox.

MATLAB release MATLAB 7.6 (R2008a)
Other requirements Should work with any version 7+ that supports function handles.
Zip File Content  
Other Files license.txt,
objective_function.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
26 Sep 2008 Riccardo Meldolesi

I thought that this was what anonymous functions were for. Am I missing something here?

26 Sep 2008 John D'Errico

It IS what anonymous functions do for us. And, they will do it far better and more easily than this submission. Plus, they will surely be more efficient.

The only reason I did not say anything immediately when this appeared is this submission is claimed to work on older releases of Matlab, and anonymous functions were introduced only in version 7. Since that ability is untested by the author and I am unable to test it, I won't say anything more, nor should I provide a rating.

26 Sep 2008 Jos x@y.z

It DOES fail on <7 releases, e.g. due to ugly (sic!) lines like this "funcHandle = eval(['@',funcName]);"
So, stick to anonymous functions, or use inline functions in <7 releases.

Nonetheless, thumbs up for the help, H1 line, ample comments, and example. I decided to rate it with 1 star since it does not add anything of value to recent releases.

28 Sep 2008 Anthony Kendall

Thanks for the tip about anonymous functions, I do see how they work now.

However, you are wrong that this is EXACTLY what the anonymous functions do. To minimize and objective function in general like this, you will either need separate m-files for your various objective functions, or will have to re-create them as anonymous functions every time you use them. There are TWO function calls here: 1) to the actual MATLAB function which you are minimizing, and 2) to the objective function that will evaluate the target function's output and provide a single value for optimization.

I encourage anyone embedding optimization in larger programs, or working with optimization that involves multiple target functions and different objective functions to give this a try.

This function is simpler than anonymous functions, and though duplicate in some fashions, not necessarily useless.

As far as the "ugly" line, that is how function handles are defined using variable inputs. Sometimes eval is necessary, even though it is not the most optimizable MATLAB facility.

I corrected my mistake about the version numbering, thank you for that.

24 Nov 2008 bipin mittal  
Please login to add a comment or rating.
Updates
29 Sep 2008

Per user comment on MATLAB version compatibility.

Tag Activity for this File
Tag Applied By Date/Time
optimization Anthony Kendall 22 Oct 2008 10:21:24
objective Anthony Kendall 22 Oct 2008 10:21:24
function Anthony Kendall 22 Oct 2008 10:21:24
generalized Anthony Kendall 22 Oct 2008 10:21:24
matlab Anthony Kendall 22 Oct 2008 10:21:24
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com