Clear Filters
Clear Filters

Define optimization objective independently of the dimension

2 views (last 30 days)
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.

Accepted Answer

Matt J
Matt J on 11 Jul 2018
Edited: Matt J on 11 Jul 2018
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 Comments
Honey Adams
Honey Adams on 11 Jul 2018
Edited: Honey Adams on 11 Jul 2018
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 on 11 Jul 2018
Edited: Matt J on 11 Jul 2018
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.

Sign in to comment.

More Answers (1)

Torsten
Torsten on 11 Jul 2018
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 Comments
Honey Adams
Honey Adams on 12 Jul 2018
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!