Question No 1.
(b)
With the help of MATLAB, solve the problem using the Steepest Descent Method.
Answer:
Following is the solution:
change =
0.5000
Solution is: [00 5.00e-01 5.00e-01]
Matlab code:
clear
clc
nn = input('Enter no of independent variables: ');
for ii = 1:nn
eval(sprintf('x%d = sym(''x%d'');', ii, ii))
end
syms x y z
f = (x^2+y-z)^3;
arg_names = symvar(f)
f = symfun(f, arg_names)
syms phi(t)
x0 = [0 1 0];
change = 1;
xi = x0;
g = gradient(f, arg_names);
kk=1;
while change > eps
gt = double(subs(g, arg_names, xi));
if gt == 0
break;
end
tt = xi - t*gt';
phi = (subs(f, arg_names, tt));
phi = symfun(phi, t);
phi(t) = simplify(phi);
phi_dash = diff(phi);
ti = solve(phi_dash, t);
ti = double(ti);
im = imag(ti);
re = real(ti);
ri = im./re;
ti(ri>1e-3) = [];
ti = real(ti);
temp = double(phi(ti));
[~, te] = min(abs(temp));
ti = ti(te);
xt = xi;
xi = xi - ti*gt'
change = (xi-xt)*(xi-xt)'
error(kk)=change;
kk=kk+1;
end
kk=1:kk-1;
plot(kk,error)
xlabel('Iteration')
ylabel('Error')
title('Plot showing Error convergence')
disp(strrep(['Solution is: [' num2str(xi, ' %0.2d') ']'], ',)',
')'))
Question No 2.(d)
Solve the KKT conditions using an appropriate solver in MATLAB.
Answer:
Matlab code:
clear all; clc
% Starting point
x0 = [0; 0; 0; 0; 0;]; % x1, x2, u1, s1, s1,
options = optimset('Algorithm','levenberg-marquardt');
x = fsolve(@kktsystem_u1,x0, options) % Case 1
x = fsolve(@kktsystem_u1s1,x0, options) % Case 2
x = fsolve(@kktsystem_s1,x0, options) % Case 3
x = fsolve(@kktsystem_s1u1,x0, options) % Case 4
function F = kktsystem_u1(x)
F = [-8*x(1)-2; -2*x(2)+2; x(1)+3*x(2)+2*x(3)+x(5)^2];
end
function F = kktsystem_u1s1(x)
F = [-8*x(1)-2+x(4); -2*x(2)+2+x(4); x(1)+3*x(2)+2*x(3)+x(5)^2;
x(1)+3*x(2)+2*x(3)];
end
function F = kktsystem_s1(x)
F = [-8*x(1)-2; -2*x(2)+2; x(1)+3*x(2)+2*x(3)];
end
function F = kktsystem_s1u1(x)
F = [2*x(1)-2+x(3)+x(3); 2*x(2)-2+x(3)-x(4); x(1)+x(2)-4; x(1)-
x(2)-2+x(5)^2];
end
Matlab solution:
Equation solved.
Case1:
x=
-0.2500
1.0000
-1.3750
-0.0000
Case2:
x=
-0.3529
0.5882
-0.7059
-0.8235
-0.0000
Case3:
x=
-0.2500
1.0000
-1.3750
Case4:
x=
3.0000
1.0000
-2.0000
-2.0000
0.0000
Question No 3.(a):
Formulate the problem as a standard constrained minimization problem using an appropriate
solver in MATLAB.
Answer:
f=[0 ; 0]
H=[0 0;0 0]
A=[1 1;1 2];
b=[5;4]
[x,fval,exitflag,output,lambda] = quadprog(H,f,A,b)
Matlab answers: x =
5.0000
-1.0000
fval =
exitflag =
output =
struct with fields:
message: '↵Minimum found that satisfies the constraints.↵↵Optimization completed
because the objective function is non-decreasing in ↵feasible directions, to within the value of
the optimality tolerance,↵and constraints are satisfied to within the value of the constraint
tolerance.↵↵<stopping criteria details>↵↵Optimization completed: The relative dual feasibility,
2.500003e-11,↵is less than options.OptimalityTolerance = 1.000000e-08, the complementarity
measure,↵1.250000e-10, is less than options.OptimalityTolerance, and the relative maximum
constraint↵violation, 0.000000e+00, is less than options.ConstraintTolerance = 1.000000e-08.↵↵'
algorithm: 'interior-point-convex'
firstorderopt: 1.3759e-16
constrviolation: 0
iterations: 3
linearsolver: 'dense'
cgiterations: []
lambda =
struct with fields:
ineqlin: [2×1 double]
eqlin: [0×1 double]
lower: [2×1 double]
upper: [2×1 double]
Question No 4.(a)
ii. Solve this problemusing an appropriate solver inMATLAB.
Answer:
Matlab code:
A=[3 4 -2;-3 2 1;2 3 4];
b=[10;-2;5];
f=[1;-2;4];
syms x1 x2 x3
syms x1 x2 x3
%f=x1-2*x2+4*x3+x1^2+2*x2^2+3*x3^2+x1*x2;
%H=hessian(f,[x1,x2,x3])
H=[2 1 0;1 4 0; 0 0 6]
[x,fval,exitflag,output,lambda] = quadprog(H,f,A,b)
Matlab solution
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.
<stopping criteria details>
x=
0.4490
0.0612
-0.7755
fval =
-0.7347
exitflag =
output =
struct with fields:
message: '↵Minimum found that satisfies the constraints.↵↵Optimization completed
because the objective function is non-decreasing in ↵feasible directions, to within the value of
the optimality tolerance,↵and constraints are satisfied to within the value of the constraint
tolerance.↵↵<stopping criteria details>↵↵Optimization completed: The relative dual feasibility,
2.276224e-15,↵is less than options.OptimalityTolerance = 1.000000e-08, the complementarity
measure,↵2.017899e-11, is less than options.OptimalityTolerance, and the relative maximum
constraint↵violation, 9.325873e-16, is less than options.ConstraintTolerance = 1.000000e-08.↵↵'
algorithm: 'interior-point-convex'
firstorderopt: 1.4150e-10
constrviolation: 0
iterations: 5
linearsolver: 'dense'
cgiterations: []
lambda =
struct with fields:
ineqlin: [3×1 double]
eqlin: [0×1 double]
lower: [3×1 double]
upper: [3×1 double]:
Question No 5.(c)
Solve using Matlab:
Matlab code:
classdef PortfolioDemo < Portfolio
methods (Access = 'public', Static = false, Hidden = false);
function obj = PortfolioDemo(varargin)
m = [ 0.05; 0.1; 0.12; 0.18 ];%supposed values
C = [ 0.0064 0.00408 0.00192 0; 0.00408 0.0289 0.0204
0.0119;0.00192 0.0204 0.0576 0.0336; 0 0.0119 0.0336 0.1225 ];
%supposed values
p = Portfolio;
p = Portfolio(p, 'LowerBound', zeros(size(m)))
p = Portfolio(p, 'LowerBudget', 1, 'UpperBudget', 1)
p = Portfolio(p, 'AssetMean', m, 'AssetCovar', C)
if nargin < 1 || isempty(varargin)
return
elseif isa(varargin{1}, 'PortfolioDemo')
obj = varargin{1};
if ~isscalar(obj)
error('finance:PortfolioDemo:PortfolioDemo:NonScalarPortfolioDemo
Object', ...
['A non-scalar PortfolioDemo object was
passed into the constructor.\n' ...
'Only scalar PortfolioDemo objects can be
processed by the constructor.']);
end
if nargin > 1
arglist = varargin(2:end);
else
return
end
else
arglist = varargin;
for i = 1:numel(arglist)
if isa(arglist{i}, 'PortfolioDemo')
error('finance:PortfolioDemo:PortfolioDemo:ImproperObjectInput',.
..
['A PortfolioDemo object was passed
incorrectly into the constructor.\n' ...
'Only the first argument may be a
PortfolioDemo object with syntax\n\t', ...
'obj = PortfolioDemo(obj,
''Property1'', value1, ... );']);
end
end
end
% separate parameters and values from argument list
parameters = arglist(1:2:end);
values = arglist(2:2:end);
% make sure pairs of parameters and values
if numel(parameters) ~= numel(values)
error('finance:PortfolioDemo:PortfolioDemo:InvalidParameterValueP
airs',...
['Invalid syntax for parameter-value pairs
for PortfolioDemo constructor. ', ...
'Syntax must be either\n\t' ...
'obj = PortfolioDemo(''Property1'',
value1, ... );\n' ...
'or\n\t' ...
'obj = PortfolioDemo(obj, ''Property1'',
value1, ... );']);
end
% make sure parameters are strings
for i = 1:numel(parameters)
if ~ischar(parameters{i})
error('finance:PortfolioDemo:PortfolioDemo:InvalidParameterString
',...
['Non-string parameter encountered for a
parameter-value pair. ', ...
'Syntax must be either\n\t' ...
'obj = PortfolioDemo(''Property1'',
value1, ... );\n' ...
'or\n\t' ...
'obj = PortfolioDemo(obj, ''Property1'',
value1, ... );']);
end
end
% parse arguments
obj = parsearguments(obj, parameters, values);
% check arguments
obj = checkarguments(obj);
end
[pwgt, pbuy, psell] = maximizeSharpeRatio(obj);
end
methods (Access = 'public', Static = false, Hidden = true)
function objstate = checkobject(obj)
if isa(obj,'PortfolioDemo') && isscalar(obj)
objstate = true;
else
objstate = false;
end
end
end
end
Explanation:
In this task the portfolio optimization is performed on the given function with the help of
Matlab financial tool box. That tool box has many other feature which are used in it. So in order
to solve the problem as mentioned in the question the values of R are supposed. Those values
are choosed which give the best result based on the hit and trail method. Following are the
some of the results which are obtained from the Matlab.
Following is another plot based on the dummy data which is taken from mathwroks.com. Then
on that data the above mentioned function is applied.
Question No 5.(d)
Solve using MATLAB.
classdef PortfolioDemo < Portfolio
methods (Access = 'public', Static = false, Hidden = false);
function obj = PortfolioDemo(varargin)
m = [ 0.05; 0.1; 0.12; 0.18 ];%supposed values
C = [ 0.0064 0.00408 0.00192 0; 0.00408 0.0289 0.0204
0.0119;0.00192 0.0204 0.0576 0.0336; 0 0.0119 0.0336 0.1225 ];
%supposed values
p = Portfolio;
p = Portfolio(p, 'LowerBound', zeros(size(m)))
p = Portfolio(p, 'LowerBudget', 1, 'UpperBudget', 1)
p = Portfolio(p, 'AssetMean', m, 'AssetCovar', C)
if nargin < 1 || isempty(varargin)
return
elseif isa(varargin{1}, 'PortfolioDemo')
obj = varargin{1};
if ~isscalar(obj)
error('finance:PortfolioDemo:PortfolioDemo:NonScalarPortfolioDemo
Object', ...
['A non-scalar PortfolioDemo object was
passed into the constructor.\n' ...
'Only scalar PortfolioDemo objects can be
processed by the constructor.']);
end
if nargin > 1
arglist = varargin(2:end);
else
return
end
else
arglist = varargin;
for i = 1:numel(arglist)
if isa(arglist{i}, 'PortfolioDemo')
error('finance:PortfolioDemo:PortfolioDemo:ImproperObjectInput',.
..
['A PortfolioDemo object was passed
incorrectly into the constructor.\n' ...
'Only the first argument may be a
PortfolioDemo object with syntax\n\t', ...
'obj = PortfolioDemo(obj,
''Property1'', value1, ... );']);
end
end
end
% separate parameters and values from argument list
parameters = arglist(1:2:end);
values = arglist(2:2:end);
% make sure pairs of parameters and values
if numel(parameters) ~= numel(values)
error('finance:PortfolioDemo:PortfolioDemo:InvalidParameterValueP
airs',...
['Invalid syntax for parameter-value pairs
for PortfolioDemo constructor. ', ...
'Syntax must be either\n\t' ...
'obj = PortfolioDemo(''Property1'',
value1, ... );\n' ...
'or\n\t' ...
'obj = PortfolioDemo(obj, ''Property1'',
value1, ... );']);
end
% make sure parameters are strings
for i = 1:numel(parameters)
if ~ischar(parameters{i})
error('finance:PortfolioDemo:PortfolioDemo:InvalidParameterString
',...
['Non-string parameter encountered for a
parameter-value pair. ', ...
'Syntax must be either\n\t' ...
'obj = PortfolioDemo(''Property1'',
value1, ... );\n' ...
'or\n\t' ...
'obj = PortfolioDemo(obj, ''Property1'',
value1, ... );']);
end
end
% parse arguments
obj = parsearguments(obj, parameters, values);
% check arguments
obj = checkarguments(obj);
end
[pwgt, pbuy, psell] = maximizeSharpeRatio(obj);
end
methods (Access = 'public', Static = false, Hidden = true)
function objstate = checkobject(obj)
if isa(obj,'PortfolioDemo') && isscalar(obj)
objstate = true;
else
objstate = false;
end
end
end
end
Explanation:
In this task the portfolio optimization is performed on the given function with the help of
Matlab financial tool box. That tool box has many other feature which are used in it. So in order
to solve the problem as mentioned in the question the values of R are supposed. Those values
are choosed which give the best result based on the hit and trail method. Following are the
some of the results which are obtained from the Matlab.
Following is another plot based on the dummy data which is taken from mathwroks.com. Then
on that data the above mentioned function is applied.