Info
此问题已关闭。 请重新打开它进行编辑或回答。
using linear equality constraint within the loop
1 次查看(过去 30 天)
显示 更早的评论
My assignment is to optimize a portfolio given a set of stock.
I already have one linear equality constraint that makes certain stocks sum to a certain amount of weight in the portfolio.
However, I also have to put two additional constraints to the portfolio that makes the total sum of the weights equal to sum (in the vector W) which is a row vector it is written as sum(W) = 1.00
. and the portfolio return has to equal a specific number that the current iteration is using in the loop.
it is written as ERMean*W' = ERGrid(1,i); (it is inside the loop).
for the minization, I am using "fmincon" that already has linear equality constraints
but my professor said these two constraints that I explained "hardcoded within the loop". I am confused as to what he is saying.
here is how the code looks:
i=1; while i<=cols(ERGrid); ERMean*W' = ERGrid(1,i); sum(W) = 1.00; [W] = fmincon('objfun',x0,swtOther_A,swtOther_b,swtOther_Aeq,swtOther_beq,swtMinWt1,swtMaxWt1,[],options); ERGrid(2:swtN+1,i) = W; i=i+1; end;
But it gives me error when running the ERMean and sum(W) lines in the loop. For the ERMean line it is saying: "The expression to the left of the equals sign is not a valid target for an assignment."
For the sum(W) line it is saying: "Subscript indices must either be real positive integers or logicals."
Can someone PLEASE help me on this issue? I have been struggling with it for a while.
Thanks.
5 个评论
Geoff Hayes
2014-5-30
编辑:Geoff Hayes
2014-5-30
Sameer - you should be able to just edit your original question and modify the text there, rather than posting a new comment. The above comment is only somewhat easier to read but has not been formatted as per the link. Just highlight the portion of the text that is code and then press the {} Code button.
And you've posted different code too! Why?
回答(0 个)
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!