# Thread: Stochastic DE's;Eulers Method; MATLAB

1. ## Stochastic DE's;Eulers Method; MATLAB

I have attached a question below that involves MATLAB. I have done most of it although I am unsure of what to do with the underlined question in the attachment.

here is my matlab code for anyone with matlab:

function y = stochde1
% This section generates White Noise
randn('state',0);
T = 1; N = 2^(18); h = T/N;
w=randn(N,1);
W = [0 ; cumsum(w)*sqrt(h) ];
t=(0:N)'*h;

% Develops solution to the CIR model for this particular W
% using Euler's Method at N = 2^18

kappa = (1/5); rbar = 6.8; beta =(1/5); p=(1/2);
r = nan +zeros(N+1, 1);
r(1) = 7.8;
for i=1:N
r(i + 1) = r(i) - kappa*(r(i)-rbar)*h + r(i)^p*beta*(W(i+1)-W(i));
end
rw = r;
plot( t,W,'-r', t, rw,'b'), title('White Noise Path(Red), the Interest Rate Path It Generates using N=18(blue), and the Interest Rate Path It Generates using N=4(green) ')
hold on;
clear N T h w W t
%This section develops solution to the CIR model for this particular W
% using Euler's method at N=4

randn('state',0);
T = 1; N = 4; h = T/N;
w=randn(N,1);
W = [0 ; cumsum(w)*sqrt(h) ];
t=(0:N)'*h;
r = nan +zeros(N+1, 1);
r(1) = 7.8;
for i=1:N
r(i + 1) = r(i) - kappa*(r(i)-rbar)*h + r(i)^p*beta*(W(i+1)-W(i));
end
rw = r;
plot( t, rw,'g')

% To calculate the error r^w_n - r^w

% Summarises Results in a Excel Spreadsheet
A = [t(1) t(2) t(3) t(4) t(5);W(1) W(2) W(3) W(4) W(5)];
B= {'Value of t'; 'Value of W(t)';}
wk1write('Question 1 (a).wk1', A, 0,0);
xlswrite('Question 1 (a)', M, 'B2:F3');
xlswrite('Question 1 (a)', B, 'A2:A3');

This will create a spread sheet and a plot of the W(0), W(1/2), .....ect values in the underlined question. The spread sheet is also attached so you can see the values.

Can anyone help me work out the error here?

2. I thought Id add the values for $r^w$ for N=4

$r^w$ =

7.8
7.62919122274664
7.12768075689546
7.14475758313631
7.20441468871549

Also is the error $r_n^W - r^W$ the global error? If so would I just accumulate the local errors (i.e. at W(0), W(1/2), ect)?