Struggling with fminsearch with vector inputs
8 次查看(过去 30 天)
显示 更早的评论
Hello,
I am trying to use vector as an input for fminsearch.
clear
clc
R = 1.0080;
sig = 0.75;
tempkgrid = linspace(-20,60,10)';
kgrid = [tempkgrid ; tempkgrid ; tempkgrid];
zgrid = [2;2;2;2;2;2;2;2;2;2;4;4;4;4;4;4;4;4;4;4;6;6;6;6;6;6;6;6;6;6];
K = kgrid;
Z = zgrid;
aconst1 = -20*ones(30,1);
aconst2 = 60*ones(30,1);
obj = @(Kp) (1/(1-1/sig)) * ((Z + K - Kp./R) > 0) .* (Z + K - Kp./R).^(1-1/sig) + ((Z + K - Kp./R) <= 0) * (-999999);
Kp2 = fminsearch(obj,aconst1);
If I run this, I get the following error message. 'Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 30-by-1'
Is there anyway I can run this?
Thanks in advance.
0 个评论
采纳的回答
Matt J
2021-11-1
编辑:Matt J
2021-11-1
Your objective function is not returning a scalar value at Kp=aconst1. We cannot know what objective you truly intended.
Regardless though, you will not have much luck using fminsearch on a problem with 30 unknowns. It is designed for much smaller problems (<7 unknowns or thereabouts).
2 个评论
Matt J
2021-11-3
编辑:Matt J
2021-11-3
It is theoretically proven to converge only for problems with a single unknown, but empirically, it tends to work well up to about 6 unknowns.
Also, the computation time per iteration can increase steeply with the number of unknowns, N. The algroithm requires N+1 evaluations of the objective function per iteration, so if each evaluation is O(N), the computational cost of the whole iteration will be O(N^2).
更多回答(1 个)
John D'Errico
2021-11-1
编辑:John D'Errico
2021-11-1
Fminsearch should NEVER be used with more than around say 6-8 unknowns. 30 unknowns is just impossible for fminsearch. PERIOD. I've not even looked at whether your problem is well-posed, as fminsearch is a waste of your time here.
2 个评论
Matt J
2021-11-4
You can always try, but I think it will be both more reliable and faster if you solve one equation at a time using fzero(). Or, if your eqautions happen to be polynomials like above, you should use roots().
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!