Struggling with fminsearch with vector inputs

조회 수: 5 (최근 30일)
Chang seok Ma
Chang seok Ma 2021년 11월 1일
댓글: Matt J 2021년 11월 4일
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.

채택된 답변

Matt J
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
Chang seok Ma
Chang seok Ma 2021년 11월 2일
Hello, thanks for the answer.
So I intended to return a vector(Kp) that minimizes the objective function and starting point as aconst1.
I changed the code to scalarize the objective function.
Kp3 = fminsearch(@(c) norm(obj(c)) ,aconst1)
and I think it returns the value as I intended.
Could you give me more details about 'It is designed for much smaller problems'?
Does it mean that fminsearch is not suitable for vector?
Matt J
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
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
Chang seok Ma
Chang seok Ma 2021년 11월 4일
Thanks for the comments.
I am sorry but I am getting confused.
I intended to solve 30 equations with 30 unknown. However, each equation is independent. So it is just 30 different univariate functions.
I think it would be a problem if I want to solve something like
Y = f(x1,x2,x3,...,x30)
but here is what I intended with different samples
Y = x^2 + 2*x + 1
Y = x^2 + 4*x + 4
Y = x^2 + 6*x + 9
Y = x^2 + 8*x + 16
.....
I just wanted to get (-1, -2, -3, -4, ...) as return.
Does this still cause a problem in fminsearch?
Thank you.
Matt J
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 CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by