MATLAB Answers

Stochastic delay differantial equations

4 views (last 30 days)
miray boldan
miray boldan on 21 Jun 2021
Answered: Divija Aleti on 24 Jun 2021
ı write this matlab code but it doesnt work.
% Euler-Maruyama method on linear SDE Stock Price.
%SDE is dS= ((mu*S)+(c*S(t-tau)-d) dt + ((a*S)+(b*S(t-tau))dW, S(0) = Szero,
%where mu = -3, c = 2e^-1 , d = 3-2e^-1 , a= 0.1 , b= 0.1 , tau = 1 and Szero = 100.
%Discretized Brownian path over [0,1] has dt = 2^(-8).
%Euler-Maruyama uses timestep R*dt.
randn('state',100)
mu = -3; a = 0.1 ; b = 0.1 ; Szero = 100;
T = 2; N = 2^8; dt = 1/N;
dW = sqrt(dt)*randn(1,N);
W = cumsum(dW);
%Strue = Szero*exp((mu-0.5*sigma^2)*([dt:dt:T])+sigma*W);
%plot([0:dt:T],[Szero,Strue],'m-'), hold on
R =8; Dt = R*dt; L = N/R;
Xem = zeros(1,L);
Xtemp = Szero;
linetypes={'b-','g--*'};
for j = 1:L
Winc = sum(dW(R*(j-1)+1:R*j));
Xtemp = Xtemp + Dt*mu*Xtemp + a*Xtemp*Winc;
Xem(j) = Xtemp;
end
plot([0:Dt:T],[Szero,Xem],'g--*'), hold off
legend(': Exact solution',': Euler-Maruyama')
xlabel('t','FontSize',12)
ylabel('S','FontSize',12,'Rotation',0,'HorizontalAlignment','right')
emerr = abs(Xem(end)-Strue(end))
what is my mistake?

Answers (1)

Divija Aleti
Divija Aleti on 24 Jun 2021
Hi Miray,
The error is occurring in the line where you are using the plot function. This is because the values you have given to the function are of different lengths.
0:Dt:T has 65 elements whereas [Szero,Xem] has 33 elements.
To use the plot function, both these vectors should be of the same length.
I suggest you to review your problem statement, all the variable values and the code you have written to figure out which of the above vectors should be changed.
Hope this helps!
Regards,
Divija

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!