Define optimization objective independently of the dimension

2 次查看(过去 30 天)
How do i define this objective function in matlab .the x are all vectors and i would like to solve this using the genetic algorithm.can matlab identify the function without expanding the function .what if i have 20 variables to deal deal with?
Minimze ∑_(i=1:n ) (A0i + A1*x_i1 + A2*x_i2 + A3*x_i3 - y )^2 + 1⁄6(B0 + B1*x_i1 + B2*x_i2 + B3*x_i3- z )^2
A AND B are my paramters.

采纳的回答

Matt J
Matt J 2018-7-11
编辑:Matt J 2018-7-11
I don't know why you would want to use the genetic algorithm for such a simple quadratic function. However, the general answer to your question is that you should be using vectorized commands to write these expressions. I.e., you would put your x_ij in a matrix X and write the function something like below. Notice that this works regardless of the size of X.
X=_____;
y=_____;
z=_____;
p_opt = ga(@(p) myfitness(p,X,y,z),______);
function out=myfitness(params, X,y,z)
n=size(X,2);
A=params(1:n);
B=params(n+1:end);
out= norm(X*A-y)^2 +1/6*norm(X*B-z)^2 ;
end
  13 个评论
Honey Adams
Honey Adams 2018-7-11
编辑:Honey Adams 2018-7-11
Thank you so much, Matt. Did you see the code attached? That was the complexity I was referring to.tHIS I was manually doing it.
Matt J
Matt J 2018-7-11
编辑:Matt J 2018-7-11
Thank you for your answer matt. the quaprog couldn't solve this due to the problem being non_covex.
Incidentally, you should be using lsqlin(), not quadprog(), to solve this problem as it is more directly tailored to linear least squares optimization.
Also, The problem is convex, but depending on how small the eigenvalues of your H-matrix are, quadprog can have difficulty recognizing that.

请先登录,再进行评论。

更多回答(1 个)

Torsten
Torsten 2018-7-11
Write your objective as
(x*A-y).'*(x*A-y) + 1/6*(x*B-z).'*(x*B-z)
and expand.
I get
[A.', B.']*[x.'*x, 0; 0, 1/6*x.'*x]*[A;B] + [-2*y.'*x,-1/3*z.'*x]*[A;B] + constant term
This can directly used for quadprog.
Best wishes
Torsten.
  5 个评论
Honey Adams
Honey Adams 2018-7-12
Matt, I figured out the hessian. Good, you clarified it. I have to make effort to distinguish when to use which optimizer.i used quadprog because the original problem has linear constraints and the objective function, quadratic in nature as described in the Matlab documentation. But like you said, in some situations, my hessian is non-definite function non_convex and cant be solved with quadprog. Thank you, Thorsten. You teachers are making my study of Matlab great.Thank you all.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Linear Least Squares 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by