function intgxtdw % Fig. 4.2 Book code example for int[g(x(t),t)dw] on [t0,t] (3/2007) % by RNG Simulation: % Generation is by summing g(X(i),t(i))dW(i) of even spacing dt for % i=0:n, but converted to from index base zero to base one: % matlab[G(X(i),T(i))DW(i);i=1:N+1] = math[g(X(i),t(i))dW(i);i=0:n]. % Chain Rule Form: Int[gdW](t) = G(W,t)-G(0,0) - Int[(g_t+0.5*g_w)(w,t)dt]; % G_w(w,t) = g(w,t), G_{ww}(w,t) = g_w(w,t). % Sample Test Code for various g's, here for g(x,t) = exp(x) and x = w. clc % clear variables, but must come before globals, % else clears globals too. clf % clear figures fprintf('\nfunction intgxtdw OutPut:') nfig = 0; % figure counter. TF= 2.0; T0 = 0; N = (TF-T0)*10000; NI = N+1; dt = (TF-T0)/NI; % Set initial time grid: Fixed Delta{t}, Scaled to [T0,TF] with N. sqrtdt = sqrt(dt); % Set standard Wiener increment time scale. % Begin Sample Path Calculation: kstate = 1; randn('state',kstate); % set randn state for repeatability. dW = sqrtdt*randn(1,NI); % Generate normal random vector of N+1 % samples for dW(t). t = zeros(1,NI+1); t(1) = T0; % set T initially. W = zeros(1,NI+1); % Set W(1) in place of W(0) = 0 wpo for base 1 vector. X = zeros(1,NI+1); % Set integral sum initially. gv = zeros(1,NI+1); gv(1) = g(X(1),t(1)); % Set integrand initially. sdw = zeros(1,NI+1); sdt = zeros(1,NI+1); % Set integral sum initially. ev = zeros(1,NI+1); % Set Error initially. for i = 1:NI % Simulated Sample paths by Increment Accumulation: t(i+1) = i*dt; W(i+1) = W(i) + dW(i); X(i+1) = W(i+1); % Set State for this g Example. gv(i+1) = g(X(i+1),t(i+1)); sdw(i+1) = sdw(i) + gv(i)*dW(i);% integrand g in subfunction. sdt(i+1) = sdt(i) - gthgw(X(i+1),t(i+1))*dt;% gthgw in subfunction. ev(i+1) = sdw(i+1) - exact(X(i+1),t(i+1)) - sdt(i+1); % CAUTION: For given g only! end t(NI+1) = TF; % Correct for final cumulative time rounding errors. % Begin Plot: nfig = nfig + 1; fprintf('\n\nFigure(%i): int[g](t) versus t Simulations\n',nfig) figure(nfig) scrsize = get(0,'ScreenSize'); % figure spacing for target screen ss = [5.0,4.0,3.5]; % figure spacing factors plot(t,sdw,'k-',t,W,'k-.',t,ev,'k--','LineWidth',2); title('\int g(X,t)dW(t) for g = exp(X), X = W'... ,'FontWeight','Bold','Fontsize',44); ylabel('\int g(X,t)dW(t), X = W(t) and Error(t)'... ,'FontWeight','Bold','Fontsize',44); xlabel('t, Time'... ,'FontWeight','Bold','Fontsize',44); hlegend=legend('\int g(X,t)dW(t)','X = W(t)','Error(t)'... ,'Location','SouthWest'); set(hlegend,'Fontsize',36,'FontWeight','Bold');; set(gca,'Fontsize',36,'FontWeight','Bold','linewidth',3); set(gcf,'Color','White','Position' ... ,[scrsize(3)/ss(nfig) 60 scrsize(3)*0.60 scrsize(4)*0.80]); %[l,b,w,h] % End Main %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function gv = g(x,t) % ignore caution that t might not be used here. % Sample g(X(t),t) only, e.g., %1% gv = exp(x-t/2); % x = w. %2% gv = exp(x); % x = w. %3% gv = x; % x = w. gv = exp(x); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function exactv = exact(x,t) % Sample g(X(t),t) exact integrals only, e.g., %1% exactv = exp(x-t/2) - 1; % i.e., G(w,t)-G(0,0), x=w, G(w,t)=exp(w-t/2). %2% exactv = exp(x) - 1; % i.e., G(w,t)-G(0,0), x=w, G(w,t)=exp(w). %3% exactv = 0.5*(x^2-t); % i.e., G(w,t)-G(0,0), x=w, G(w,t)=0.5*(w^2-t). exactv = exp(x) - 1; % i.e., G(w,t)-G(0,0), x=w, G(w,t)=exp(w). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function gthgwv = gthgw(x,t) % Reg. Correction Int. of (G_t+0.5*G_{ww})(X(t),t), G_w = g. %1% gthgwv = 0; % i.e., g=exp(x-t/2)=G, G_t=-0.5*G, G_{ww}=G. %2% gthgwv = 0.5*exp(x); % i.e., G=g=exp(w), G_t=0, G_{ww}=g_w=exp(w). %3% gthgw 0; % i.e., g=x=w, G=0.5*(w^2-t), G_t=-0.5, G_{ww}=g_w=1; gthgwv = 0.5*exp(x); % i.e., G=g=exp(w), G_t=0, G_{ww}=g_w=exp(w). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % End Code