LINEBURG

 << Ïðåä. ñòð. ñòðàíèöà 11(âñåãî 16)ÎÃËÀÂËÅÍÈÅ Ñëåä. ñòð. >>
nn=nb*ns; %total intervals
options(13)=1;
options(14)=10000;
um0=ones(nb,1)/nb;
%um=[0.1,0.3,0.6]â€™;

ul=zeros(nb,1);
uu=ones(nb,1);
um=constr(â€™model1_2â€™,um0,options,ul.uu,[],xinit,par);
[f,g,xm] = model1_2(um,xinit,par)
sm=zeros(nn,1);
v=zeros(nn,1);

for ii =1: nb
for jj= 1:ns
sm((ii-1)*ns+jj)=um(ii)/ns;
end
end

v(1)=sm(1);

for ii=2:nn
v(ii)=sm(ii)+v(ii-1);
154 OPTIMAL CONTROL MODELS IN FINANCE

end

v(nn)=0.9999999999999;
s=zeros(nn+1,1);
s(1)=0;
for ii = 2: nn+1
s(ii)=v(ii-1);
end

s
figure;
plot(xm(:,1),xm(:,2),â€™*b-â€™)

xlabel(â€™state function xm1â€™)

%Model 1_2.m

function [f,g,xm] = model1_2(um,xinit,par)
nx=par(1);
nu=par(2);
nb=par(3);
ns=par(4); % ns is small interval
nn=par(3)*par(4) ; % nn is total interval
sm=zeros(nn,1);
for ii =1: nb
for jj= 1:ns
sm((ii-1)*ns+jj)=um(ii)/ns;
end
end
xm=zeros(nn+1,nx);
xm(1,:)=xinit;
lm=zeros(nn+1,nx);
ma=nx;
t=0;
it=1;
hs=1/nn;
px=â€™model1_3â€™;

xm=nqq(px,nx,nu,nn,xm,ma,t,it,hs,sm,xm,lm,par);
zz=zeros(nn+1,nx);
zz(1,:) = 0;
ma=1;
t=0;
it=1;
hs=1/nn;
x=xm(:,2);
px=â€™model1_4â€™;

jm=nqq(px,nx,nu,nn,zz,ma,t,it,hs,sm,x,lm,par);
APPENDIX A: CSTVA Program List 155

%f=jm(nn+1);
g(1)=sum(sm)-1;
xf=xm(nn+1,1);
ed=â€™model1_5â€™;
f=jm(nn+1)+feval(ed,xf,par);

%Model1_3.m compute differential equation

function ff = model1_3(t,it,z,yin,hs,sm,xm,lm,par)
nn=1/hs;
nb=par(3);
ns=par(4);
rr=nn/(3*ns);
p=par(5);
r=par(7);
c=par(9);
T=par(10);
%for i=1:rr,
% us(3*i-2)=0;
% ur(3*i-2)=0;
% us(3*i-1)=0;
% ur(3*i-1)=par(6)/par(7);
% us(3*i)=par(6)/par(7);
% ur(3*i)=0;
%end

for i=1:rr,
us(3*i-2)=par(6)/par(7); % []
C
ur(3*i-2)=0;
us(3*i-1)=0; % []
B
ur(3*i-1)=par(6)/par(7);
us(3*i)=0; % []
A
ur(3*i)=0;
end

ts=zeros(nn,1);

if ns > 1
for ii= 1:nb
for jj=1 :ns
ts(ii)=ts(ii)+sm((ii-1)*ns+jj);
end
end
else
for ii= 1: nb
ts(ii)=sm(ii);
end
end
156 OPTIMAL CONTROL MODELS IN FINANCE

pt=nb*ts(floor((it-1)/ns)+1,1);
ff(1)= T*c*([1-ur(1,floor((it-1)/ns)+1)]*r*z(2)-p*z(1))*pt;
ff(2)= T*r*z(2)*[ur(1,floor((it-1)/ns)+1)+us(1,
floor((it-1)/ns)+1)*(1-z(2)/((1-d)
*z(1)))]*pt;

%Model1_4.m

function ff = model1_4(t,it,z,yin,hs,sm,xm,lm,par)
nn=1/hs;
fr=nn*mod(t,hs);
xmt=(1-fr)*xm(it)+fr*xm(it+1);
nn=1/hs;
rr=nn/(3*ns);
p=par(5);
r=par(7);
T=par(10);
%for i=1:rr,
% ur(3*i-2)=0;
% ur(3*i-1)=0;
% ur(3*i)=par(6)/par(7);
%end

for i=1:rr,
ur(3*i-2)=0;
ur(3*i-1)=par(6)/par(7);
ur(3*i)=0;
end

umx=zeros(nn,1);
umx(2) = sm(1);
for ii=3:nn
umx(ii)=umx(ii-1) + sm(ii-1);
end
qt=nn*sm(floor(it),1)*(t-hs*(it-1))+umx(floor(it),1);
ll=exp(-p*qt*T)*[1-ur(1,floor(it))]*r*xmt;
ff=-nn*ll*sm(floor(it),1)*T;

%Model1_5.m

function ff = model1_5(xf,par)
p=par(5);
T=par(10);
ff=-xf*exp(-p*T);

4. Program D: Three Value-Control Model in Chapter 5
%Project3_1.m computation for the integral
APPENDIX A: CSTVA Program List 157

function project3_1=project3_1(nb,ns) % nb is big
interval of time t and ns is small interval
par = [2,1,nb,ns];
xinit= [3,5];
nb=par(3);% big intervals
ns=par(4);% small intervals
nn=nb*ns; %total intervals
options(13)=1;
options(14)=10000;
um0=ones(nb,1)/nb;
ul=zeros(nb,1);
uu=ones(nb,1);
um=constr(â€™project3_2â€™,um0,options,ul,uu,[],xinit,par);

[f,g,xm] = project3_2(um,xinit,par)
sm=zeros(nn,1);
v=zeros(nn,1);

for ii =1: nb
for jj= 1:ns
sm((ii-1)*ns+jj)=um(ii)/ns;
end
end
v(1)=sm(1);
for ii=2:nn
v(ii)=sm(ii)+v(ii-1);
end

v(nn)=0.9999999999999;
s=zeros(nn+1,1);
s(1)=0;
for ii = 2: nn+1
s(ii)=v(ii-1);
end

s
figure;
plot(s,xm(:,1),â€™ob-â€™)
hold on;
t= [0:1/10:1];

plot(t,4*sin(5*t+1), â€™+r:â€™)
xlabel(â€™Time Tâ€™)
ylabel(â€™State function xm and given fit function
4*sin(5*t+1) â€™)
figure;

plot(xm(:,1),xm(:,2),â€™*b-â€™)
xlabel(â€™state function xm1â€™)
158 OPTIMAL CONTROL MODELS IN FINANCE

ylabel(â€™state function xm2â€™)

%Project3_2.m integral

function [f,g,xm] = project3_2(um, xinit, par)
nx=par(1);
nu=par(2);
nb=par(3);
ns=par(4); % ns is small interval
nn=par(3)*par(4) ; % nn is total interval
sm=zeros(nn,1);

for ii =1: nb
for jj= 1:ns
sm((ii-1)*ns+jj)=um(ii)/ns;
end
end

xm=zeros(nn+1,nx);
xm(1,:)=xinit;
lm=zeros(nn+1,nx);
ma=nx;
t=0;
it=1;
hs=1/nn;
px=â€™project3_3â€™;
xm=nqq(px,nx,nu,nn,xm,ma,t,it,hs,sm,xm,lm,ns);

zz=zeros(nn+1,nx);
zz(1,:) = 0;
ma=1;
t=0;
it=1;
hs=1/nn;
px=â€™project3_4â€™;
x=xm(:,1);

jm=nqq(px,nx,nu,nn,zz,ma,t,it,hs,sm,x,lm,ns);
f=jm(nn+1);
g(1)=sum(sm)-1;

%Project3_3.m compute differential equation

function ff = project3_3(t,it,z,yin,hs,sm,xm,lm,ns)
nn=1/hs;
T=5;
B=0.0;
B=0.1;
APPENDIX A: CSTVA Program List 159

B=0.2;
nb=nn/ns;
rr=nn/(3*ns);
%for i=1:rr,
% ut(3*i-1)=-1;
% ut(3*i)=0;
% ut(3*1+1)=1;
%end

for i=1:rr,
ut(3*i-2)=-2;
ut(3*i-1)=0;
ut(3*i)=2;
end
ts=zeros(nn,1);

if ns > 1
for ii= 1:nb
for jj=1:ns
ts(ii)=ts(ii)+sm((ii-1)*ns+jj);
end
end
else
for ii= 1: nb
ts(ii)=sm(ii);
end
end

pt=nb*ts((floor((it-1)/ns)+1),1);
ff(1)= T * z(2)*pt;
ff(2)= (-T * z(1) + T *ut(1,(floor((it-1)/ns) + 1)) â€“
T^2 * B *z(2))*pt;

%Project3_4.m
function ff = project3_4(t,it,z,yin,hs,sm,x,lm,ns)
nn=1/hs;
fr=nn*mod(t,hs);
xmt=(1-fr)*x(it)+fr*x(it+1);
nb=nn/ns;
umx=zeros(nn,1);
umx(2)=sm(1);

for ii = 3: nn
umx(ii)=sm(ii-1)+umx(ii-1);
end
qt=nn*sm(floor(it),1)*(t-hs*(it-1))+umx(floor(it),1);
ll=abs(xmt-4*sin(5*qt+1);% |x1(t)-cos(0.4*t+2.85)|
ff=ll*nn*sm(floor(it),1);
Appendix B
Some Computation Results

1. Results for Program A
um: the optimal time intervals.
f: the result of the objective funtion
g: the result of the constraints
s: optimal switching times
xm: the values of the state variable

project1_1(2)
um =
0.1271
0.8729
f=
0.0627
g=
-4.5940e-11
xm =
0
0.1271
0.1271

project1_1(4)
um =
0.0299
0.4868
0.2912
0.1921
f=
0.0291
g=
-7.8249e-13
xm =
162 OPTIMAL CONTROL MODELS IN FINANCE

0
0.0299
0.0299
0.3211
0.3211
project1_1(6)
um =
0.0154
0.3612
0.1226
0.1763
0.2135
0.1111
f=
0.0184
g=
-3.4402e-11
xm =
0
0.0154
0.0154
 << Ïðåä. ñòð. ñòðàíèöà 11(âñåãî 16)ÎÃËÀÂËÅÍÈÅ Ñëåä. ñòð. >>