简体   繁体   中英

Matlab @fmincon error: "Supplied objective function must return a scalar value"

EDIT: To help clarify my question, I'm looking to get a fit to the following data:

在此处输入图片说明

I can get a fit using the cftool function, but using a least squares approach doesn't make sense with my binary data. Just to illustrate...

在此处输入图片说明

So, my goal is to fit this data using the fmincon function.

ORIGINAL POST:

I have data from a movement control experiment in which participants were timed while they performed a task, and given a score (failure or success) based on their performance. As you might expect, we assume participants will make less errors as they have more time to perform the task.

I'm trying to fit a function to this data using fmincon, but get the error "Error using fmincon (line 609) Supplied objective function must return a scalar value." I don't understand a) what this means, or b) how I can fix it.

I provide some sample data and code below. Any help greatly appreciated.

%Example Data:

time = [12.16 11.81 12.32 11.87 12.37 12.51 12.63 12.09 11.25
7.73 8.18 9.49 10.29 8.88 9.46 10.12 9.76 9.99 10.08
7.48 7.88 7.81 6.7 7.68 8.05 8.23 7.84 8.52 7.7 
6.26 6.12 6.19 6.49 6.25 6.51 6 6.79 5.89 5.93 3.97 4.91 4.78 4.43
3.82 4.72 4.72 4.31 4.81 4.32 3.62 3.71 4.29 3.46 3.9 3.73 4.15
3.92 3.8 3.4 3.7 2.91 2.84 2.7 2.83 2.46 3.19 3.44 2.67 3.49 2.71
3.17 2.97 2.76 2.71 2.88 2.52 2.86 2.83 2.64 2.02 2.37 2.38
2.53 3.03 2.61 2.59 2.59 2.44 2.73 ]

error = [0  0   0   0   0   0   0   0   0   1   0   0   0   0   1   1   1   0   0   0   0   1   1   1   1   1   1   0   0   0   0   1   1   1   0   1   0   1   0   1   1   0   1   1   0   1   1   1   1   1   1   1   1   1   1   1   1   0   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1];

%Code:

% initial parameters - a corresponds to params(1), b corresponds to params(2)
a = 3.0;
b = -0.01;

LL = @(params) 1/1+params(1)*(log(time).^params(2));
LL([a b]); 
pOpt = fmincon(LL,[a b],[],[]); 

The mistakes comes from the function LL, that returns a number of values equal to the length of time .

To properly use fmincon , you need to have a function that returns only one value.

I believe logistic regression would fit your data and purposes nicely. In that case, why not simply use Matlab's built-in function for multinomial logistic regression?

B = mnrfit(time,error)

Regarding your function LL , are you sure you have entered the function correctly and are not missing a parentheses?

LL = @(params) 1/(1+params(1)*(log(time).^params(2)));

Without the parentheses, you function is equivalent to 1 + a*log(x)^b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM