How write these equation in Matlab for optimization

Dear Friends,
I want to write this optimization problem in matlab.
Anybody here to help me write thi in Matlab please.
the only one decision variable is

 採用された回答

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020 年 7 月 5 日

0 投票

First you don't need the variable w, your problem is to maximize corr(Q,S), which is a unconstrained problem in x. Second, by your problem definition, Q is a single value, and correlation between single values doesn't make much sense, so I believe what you mean by corr is that the values should be equal or Q is a vector and you forgot a summation index. Considering the second hypothesis an example of how to do it could be seen here:
% Random data
S = 1:100;
c = randn(100,4);1
% Function to minimize
f = @(x,S,c)-min(min((corrcoef(S', (x*c')/sum(x) )) )); % Negative so min = max. Two min's since corrcoef gives a matrix as output
% Minimizer
[x,fval] = fminsearch(@(x)f(x,S,c),[1,1,1,1])
You can use this code as basis to implement your problem.

11 件のコメント

Muhammad Usama
Muhammad Usama 2020 年 7 月 5 日
Q and SA are vectors
Muhammad Usama
Muhammad Usama 2020 年 7 月 5 日
why we are not using corr?
as corr(Vector A, Vector B) would be a single value
Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020 年 7 月 5 日
You can use corr, it will give the same answer, I just forgot about it. You would just need to change the line to
f = @(x,S,c)-corr(S', ((x*c')/sum(x))' );
Muhammad Usama
Muhammad Usama 2020 年 7 月 5 日
編集済み: Muhammad Usama 2020 年 7 月 5 日
Actully in f
there was
sum(x*c')/sum(x)
%instead of
(x*c')/sum(x)
however when I write sum with numerator it not woks
Also, c would be rectangular matrix, so
sum(x.*c(:,i))/sum(x)
Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020 年 7 月 5 日
I replaced sum with a matrix operation. If you want to have the sum it would be:
sum(x.*c,2)'/sum(x)
Which is the same
Muhammad Usama
Muhammad Usama 2020 年 7 月 5 日
how can we restrict 'x' value to be non-negtive in this ?
Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020 年 7 月 5 日
sum(abs(x).*c,2)'/sum(abs(x))
Muhammad Usama
Muhammad Usama 2020 年 7 月 5 日
But it is giving -ve values of x as well
I want to constraint x >= 0
Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020 年 7 月 5 日
Suppose the optimizer finds a value of -2 for a x entry. When it goes to the function the -2 becomes +2 because of the absolute. Which means that if you do
[x,fval] = fminsearch(@(x)f(x,S,c),[1,1,1,1])
x = abs(x);
You will still get the same cost function, i.e., the abs(x) is exact the same minimum.
Muhammad Usama
Muhammad Usama 2020 年 7 月 5 日
Thank you Sir.
one more thing, I want to add sum(x) ==1
is it possible?
Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020 年 7 月 5 日
You have this already. Note that you divide by sum(x). This is a linear operation, so it would be the same as:
(x/(sum(x)))*c'
which would give: sum(x) ==1. In other words, if you normalize x by it's sum you'll get the same result as the not normalized one.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by