getting actual numbers instead of symbolic results when using solve

조회 수: 1 (최근 30일)
Max
Max 2015년 4월 6일
댓글: tuan nguyen minh 2021년 9월 27일
Dear all,
I am stuck with a problem where I am trying to find a minimum-variance-portfolio, with the budget constraint, hence using the Lagrangian.
I have defined my variance-covariance-matrix as follows. The four unknown variables are the three weights and lambda. The Lagrangian is defined, and I want to use "solve" on the four equations below to get the four results. Since the variance-covariance matrix contains actual numbers, I should get actual numbers (i.e. asset weights) after putting the four equations with four unknowns into "solve", but it does not work at all and I don't know why.
Perhaps one can help.
Sigma = [0.04,0.023,0.045;0.023,0.03,0.02;0.045,0.02,0.06];
syms w1 w2 w3 lambda ;
risk = [w1,w2,w3]*Sigma*[w1;w2;w3];
Lagrangian = risk + lambda*(w1+w2+w3-1);
vals = solve(diff(Lagrangian,w1)==0,diff(Lagrangian,w2)==0,diff(Lagrangian,w3)==0,diff(Lagrangian,lambda)==0);

채택된 답변

A Jenkins
A Jenkins 2015년 4월 6일
Why do you say it doesn't work? I see your answers returned as a structure:
>> vals
vals =
lambda: [1x1 sym]
w1: [1x1 sym]
w2: [1x1 sym]
w3: [1x1 sym]
If you want to access each one to see the value, you can do the following:
>> vals.w1
ans =
15/88
If you don't like structures, have it return all your answers separately:
>> [lambda_star, w1_star, w2_star, w3_star]=solve(diff(Lagrangian,w1)==0,diff(Lagrangian,w2)==0,diff(Lagrangian,w3)==0,diff(Lagrangian,lambda)==0)
lambda_star =
-491/8800
w1_star =
15/88
w2_star =
65/88
w3_star =
1/11
(Also useful is the vpa() command if you want to evaluate the fraction to a decimal representation.)
  댓글 수: 4
Max
Max 2015년 4월 7일
now I am ashamed to open another question for this issue, but if I want to plot the portfolio risk "risk" (as above) as a function of w1 and w2 (and setting w3=1-w1-w2), what do I need to do? I.e. I want the two weights on the lower axis and the results total risk on the third axis, to get a nice colored surface with the minimum visible.
Do I need to define the "risk" function differently?
plot (w1,w2,risk) and plot3(w1,w2,risk) do not work!
tuan nguyen minh
tuan nguyen minh 2021년 9월 27일
thanks, you help me solve the question

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Quadratic Programming and Cone Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by