Code covered by the BSD License  

Highlights from
Generalized Objective Function

3.0

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

Generalized Objective Function

by

 

24 Sep 2008 (Updated )

Allows any MATLAB function to easily be optimized

| 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.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (5)
24 Nov 2008 bipin mittal  
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.

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.

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 Riccardo Meldolesi

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

Contact us