LINEBURG

 << Пред. стр. страница 10(всего 16)ОГЛАВЛЕНИЕ След. стр. >>
uu=ones(nn,1);
146 OPTIMAL CONTROL MODELS IN FINANCE

% upper bound of um, t(nn)вЂ”t(nn-1) <= 1

um=constr(вЂ˜project1_2вЂ™,um0,options,ul.uu,[],xinit,par)
% use the MATLAB вЂњconstrвЂќ to get the optimal time intervals

[f,g,xm]=project1_2(um,xinit,par)
v(1)=um(1);
for ii=2:nn
v(ii)=um(ii)+v(ii-1);
end
% obtain the real switching time t

v(nn)=0.9999999999999;
v=[0 v];
hold on
plot(v,xm,вЂ˜rx-вЂ™)
% plot the state function to switch time t

xlabel(вЂ˜Time TвЂ™)
ylabel(вЂ˜State function xm and given fit function 1/2*tвЂ™)

Project1_2.m
%. Function of calculating differential equations and intervals

function [f,g,xm]=project1_2(um,xinit,par)
nx=par(1);
nu=par(2);
nn=par(3);
ps=1;
xm=zeros(nn+1,nx);
xm(1,:)=xinit;
lm=zeros(nn+1,nx);
% co-state function, polygonal function. In this case no
co-state function

ma=nx;
% Component of the state functions.

t=0;
% scale time t

it=1;
% counter it
hs=1/nn;
px=вЂ˜project1_3вЂ™;
% form of the right side of the transformation of the
differential equation

xm=nqq(px,nx,nu,nn,xm,ma,t,it,hs,um,xm,lm,ps);
APPENDIX A: CSTVA Program List 147

% use SCOM package вЂњnqqвЂќ to do the integral calculation

zz=zeros(nn+1,nx);
% here is the first xm in the calling function

zz(1,:)=0;
ma=1;
% set the number of the integral

t=0;
it=1;
hs=1/nn;
px=вЂ˜project1_4_1вЂ™;
% form of the right side of the linear transformation of the
integral

jm=nqq(px,nx,nu,nn,zz,ma,t,it,hs,um,xm,lm,ps)
f=jm(nn+1);
% the result of the integral

g(1)=sum(um)-1;
% calculate the constraint

Project1_3.m
% Form of the right side of linear transformation of the
differential equation

function ff = project1_3(t,it,z,yin,hs,um,xm,lm,ps)
nn=1/hs;
rr=nn/2;
for i=1:rr,
u(2*i-1)=1;
u(2*i)=0;
end
% map the control pattern ut=0,1,. . . in successive time
intervals
pt=nn*um(floor(it),1);
ff=(u(1,floor(it)))*pt;
% Piecewise-linear transformation d/d(xt) = (1/h)*(t(j+1)-t(j))*u(j)

Project1_4_1.m
% Form of the right side of the integral

function ff = project1_4_1 (t,it,z,yin,hs,um,xm,lm,ps)
nn=1/hs;
fr=nn*mod(t,hs);
% Linear interpolation
148 OPTIMAL CONTROL MODELS IN FINANCE

umx=zeros(nn,1);
umx(2) = um(1);
for ii=3:nn
umx(ii)=umx(ii-1) + um(ii-1);
end
xmt=(1-fr)*xm(it)+fr*xm(it+1);
% End-points of the time intervals

qt=nn*um(floor(it),1)*(t-hs*(it-1))+umx(floor(it),1);% time t
ll=abs (xmt-(1/2)*qt);
ff=nn*ll*um(floor(it),1); %|x(t)-1/2*t|

Project1_4_2.m
% A different case with the target function is (x(t)-1/2*t)^2

function ff = project1_4_2(t,it,z,yin,hs,um,xm,lm,ps)
nn=1/hs;
fr=nn*mod(t,hs);
umx=zeros(nn,1);
umx(2) = um(1);
for ii=3:nn
umx(ii)=umx(ii-1) + um(ii-1);
end
xmt=(1-fr)*xm(it)+fr*xm(it+1);
qt=nn*um(floor(it) ,1)*(t-hs*(it-1))+umx(floor(it),1);% time t
ll=(xmt-(1/2)*qt)^2;
ff=nn*ll*um(floor(it),1); %(x(t)-1/2*t)^2

Project1_4_3.m
% A different case with target is | ( ) 0 4 ( ^ )
xt-.*t2|

function ff = project1_4_3(t,it,z,yin,hs,um,xm,lm,ps)
nn=1/hs;
fr=nn*mod(t,hs);
umx=zeros(nn,1);
umx(2) = um(1);
for ii=3:nn
umx(ii)=umx(ii-1) + um(ii-1);
end
xmt=(1-fr)*xm(it)+fr*xm(it+1);
qt=nn*um(floor(it),1)*(t-hs*(it-1))+umx(floor(it),1);
ll=abs(xmt-0.4*(qt^2));
ff=nn*ll*um(floor(it),1); % |x(t)-0.4*(t^2)|

project1_4_4.m
% a different case with target is |x(t)-0.8*(t^2)|
APPENDIX A: CSTVA Program List 149

function ff = project1_4_4(t,it,z,yin,hs,um,xm,lm,ps)
nn=1/hs;
fr=nn*mod(t,hs);
umx=zeros (nn, 1);
umx(2) = um(1);
for ii=3:nn
umx(ii)=umx(ii-1) + um(ii-1);
end
xmt=(1-fr)*xm(it)+fr*xm(it+1);
qt=nn*um(floor(it),1)*(t-hs*(it-1))+umx(floor(it),1);
ll=abs (xmt-0.8*(qt^2));
ff=nn*ll*um(floor(it),1); % |x(t)-0.8*(t^2)|

2. Program B: Financial Oscillator Model in Chapter 3
Project2_1.m
%Program for project2, oscillator problem in chapter 3

function project2_1=project2_1(nb,ns) % nb is big interval of time
t and ns is small interval
par = [2,1,nb,ns];
xinit= [3,5];
%xinit= [6,5];
%par=[1,1,nb,ns];
%xinit=0.0;

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;
%um0=[ 0.00168225289917 0.12189477200848 0.64070279483764
0.23572018024164];
%um0=[0.39023521914983 0.39023521914983 0.10976478085245
0.10976478085245];
%um0=[00.1390,0.5714,0.2896]вЂ™;

ul=zeros(nb,1);
uu=ones(nb,1);
um=constr(вЂ˜project2_2вЂ™,um0,options,ul.uu,[],xinit,par)
[f,g,xm] = project2_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);
150 OPTIMAL CONTROL MODELS IN FINANCE

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/8 :1];
plot(t,-5*t+5, вЂ˜+r:вЂ™)
%plot(t,0.4*t.^2,вЂ˜+r-вЂ™)
%plot(t,t,вЂ˜+rвЂ™)

xlabeL(вЂ˜Time TвЂ™)
ylabel(вЂ˜State function xm and given fit function -5*t+tвЂ™)
figure;
plot(xm(:,1),xm(:,2),вЂ˜*b-вЂ™)
xlabel(вЂ˜state function xm1вЂ™)
ylabel(вЂ˜state function xm2вЂ™)
%title(вЂ˜plot of switching time equal twoвЂ™)

%Project2_2.m integral
function [f,g,xm] = project2_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=вЂ˜project2_3вЂ™;
xm=nqq(px,nx,nu,nn,xm,ma,t,it,hs,sm,xm,lm,ns);
zz=zeros(nn+1,nx);
zz(1,:) = 0;
APPENDIX A: CSTVA Program List 151

ma=1;
t=0;
it=1;
hs=1/nn;
px=вЂ™project2_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;

%Project2_3.m compute differential equation

function ff = project2_3(t,it,z,yin,hs,sm,xm,lm,ns)
nn=1/hs;
T=5;
B=0.0;
B=0.1;
B=0.2;
nb=nn/ns;
rr=nn/(2*ns);
%for i=1:rr,
% ut(2*i-1)=2;
% ut(2*i)=-2;
%end
for i=1:rr,
ut(2*i-1)=2;
ut(2*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)*nn*sm(1,floor(it));
%ff(2) = (-T*z(1)+T*ut(1,floor((it-1)/ns)+1)-T^2*B*z(2))
*nn*sm(1,floor(it));
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;
%ff=ut(1,(floor((it-1)/ns) + 1))*nn*sm(1,floor(it));
%ff=ut(1,(floor((it-1)/ns) +1))*pt;
152 OPTIMAL CONTROL MODELS IN FINANCE

%project2_4.m

function ff = project2_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);
%qt=nb*ts((floor((it-1)/ns) +1),1)*(t-hs*ns*((
floor((it-1)/ns)+1)-1));
%qt=qt+umx((floor((it-1)/ns)+1) ,1);
ll=abs (xmt-((-5)*qt+5));% |x1(t)-((-5*t)+5)|
%ll= (xmt-((-5)*qt+5))^2;
%ll=abs (xmt-0.4*(qt^2));
%ll=(xmt-qt)^2;
ff=ll*nn*sm(floor(it),1);
%ff=ll*pt;

%Project2_4_test.m
function ff = project2_4_test(t,it,z,yin,hs,sm,x,lm,ps)
nn=1/hs;
fr=nn*mod(t,hs);
xmt=(1-fr)*x(it)+fr*x(it+1);
umx=zeros(nn,1);
umx(2) = sm(1);
for ii=3:nn
umx(ii)=umx(ii-1) + sm(ii-1);
end
u=zeros(nn/ps,1);
ts=zeros(nn/ps,1);

if ps > 1
for ii= 1:nn/ps
u(ii*ps-(ps-1))=sm(ii*ps-(ps-1));
for jj=ii*ps-(ps-2):ii*ps
u(jj)=u(jj-1)+sm(jj);
end
ts(ii)=u(jj);
end
else
for ii = 1: nn/ps
ts(ii)=sm(ii)
end
APPENDIX A: CSTVA Program List 153

end

pt=(nn/ps)*ts((floor((it-1)/ps)+1),1);
qt=nn*sm(1,floor(it))*(t-hs*(it-1))+umx(floor(it),1);
%ll=abs (xmt-((-5)*qt+5));% |x1(t)-((-5*t)+5)|
ll=(xmt-((-5)*qt+5))^2;
ff=nn*ll*sm(1,floor(it))*pt;

3. Program C: Optimal Financing Model in Chapter 4
% An application of a model of investment of an utility.
%Model 1_1.m

function model1_1=model1_1(nb,ns,parameters) % parameters
here is used to define all the parameters in the model
%parameters = [p k r d c T]; % parameters here is used
to define all the parameters in the model
%parameters=[ 0.1 0.15 0.2 0.1 1 1];
par = [2,2,nb,ns,parameters];
%xinit= [P0,E0];
%xinit=[1 1];
%xinit = [0.5,0.5];
%xinit=[1.5 1.5];
xinit=[3 2];

nb=par(3); % big intervals
ns=par(4); % small intervals
 << Пред. стр. страница 10(всего 16)ОГЛАВЛЕНИЕ След. стр. >>