简体   繁体   English

Matlab @fmincon 错误:“提供的目标函数必须返回一个标量值”

[英]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.我可以使用 cftool 函数进行拟合,但使用最小二乘法对我的二进制数据没有意义。 Just to illustrate...只是为了说明...

在此处输入图片说明

So, my goal is to fit this data using the fmincon function.所以,我的目标是使用 fmincon 函数拟合这些数据。

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."我正在尝试使用 fmincon 将函数拟合到此数据,但收到错误“使用 fmincon 时出错(第 609 行)提供的目标函数必须返回一个标量值。” I don't understand a) what this means, or b) how I can fix it.我不明白 a) 这意味着什么,或者 b) 我如何解决它。

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 .错误来自函数 LL,它返回的值的数量等于time的长度。

To properly use fmincon , you need to have a function that returns only one value.要正确使用fmincon ,您需要有一个只返回一个值的函数。

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?在这种情况下,为什么不简单地使用 Matlab 的内置函数进行多项逻辑回归?

B = mnrfit(time,error)

Regarding your function LL , are you sure you have entered the function correctly and are not missing a parentheses?关于您的函数LL ,您确定您正确输入了函数并且没有缺少括号吗?

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

Without the parentheses, you function is equivalent to 1 + a*log(x)^b没有括号,你的函数相当于 1 + a*log(x)^b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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