Question No 1. (B) : 'Enter No of Independent Variables: '
Question No 1. (B) : 'Enter No of Independent Variables: '
(b)
With the help of MATLAB, solve the problem using the Steepest Descent Method.
Answer:
change =
0.5000
Matlab code:
clear
clc
for ii = 1:nn
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
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)
Answer:
Matlab code:
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):
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:
algorithm: 'interior-point-convex'
firstorderopt: 1.3759e-16
constrviolation: 0
iterations: 3
linearsolver: 'dense'
cgiterations: []
lambda =
Answer:
Matlab code:
%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
and constraints are satisfied to within the value of the constraint tolerance.
x=
0.4490
0.0612
-0.7755
fval =
-0.7347
exitflag =
output =
firstorderopt: 1.4150e-10
constrviolation: 0
iterations: 5
linearsolver: 'dense'
cgiterations: []
lambda =
Question No 5.(c)
Solve using Matlab:
Matlab code:
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
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
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
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)
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
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
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
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.