fmincon is not working

5 次查看(过去 30 天)
John Hey
John Hey 2023-6-23
移动Torsten 2023-6-23
I Have defined a function (see below) and it works fine - I can calculate the value of the function for any parameter values (refprice and r). However, when I try and run fminunc it gives an error message relating to the function
fun=@(refprice,r)sum((prices-refprice*ot+(exp(-r*t).*ot')').^2);
ERROR MESSAGE
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of
rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise
multiplication.
Error in analysedata>@(refprice,r)sum((prices-refprice*ot+(exp(-r*t).*ot')').^2) (line 13)
fun=@(refprice,r)sum((prices-refprice*ot+(exp(-r*t).*ot')').^2);
Error in fminunc (line 307)
f = feval(funfcn{3},x,varargin{:});
Error in analysedata (line 17)
[ests,ss]=fminunc(fun,pars0);

回答(1 个)

Torsten
Torsten 2023-6-23
编辑:Torsten 2023-6-23
You must define
F = @(x) fun(x(1),x(2))
and pass "F" instead of "fun" to fminunc.
  1 个评论
John Hey
John Hey 2023-6-23
移动:Torsten 2023-6-23
Torsten,
You are wonderful. Many, many thanks.
John

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by