Euler-Maruyama is it right?

Hello!
I have a project to solve an integral with Euler-Maruyama method and plot the approximate and exact solution. This is the code so far. When plotted it does not look correct, what do you think could be the probelm and how to solve it?
Thank you in advance!
(I know how to plot, it just isn't included here)
clear all
Y0=exp(1); %Initial value for exact solution
y0=exp(1); %Initial value for Euler Maruyama
N=10; %Number of realizations
z=randn(N); %Random variable
n=10;
dt=1/(n-1); %Step
t=0:dt:1; %Points
y=y0*zeros (1,N); %Creating vector
f=@(y) -2*exp(-2*y); %function f
g=@(y) 2*exp(-y); %function g
B0=0;%Brownian motion inital
B(1)=B0;
for k=1:N
dB = z-n*sqrt(dt); %Brownian motion
B=B+dB;
for i=1:n-1
y(i+1,:)=y(i,:)+f(y(i,:))*dt+g(y(i,:)).*dB(i,:);
Y(i+1,:)=log(2*dB(i,:)+exp(Y0)); %Exact solution
end
end

1 Kommentar

Jan
Jan am 25 Feb. 2022
We cannot reply reliably to "it does not look correct".
On one hand, this means, that the results differ from your expectation, but you do not mention, what you expect.
On the other hand, your code does not run and does not create a result. This lines fails with an error message:
Y(i+1,:)=log(2*dB(i,:)+exp(Y0));
% Unable to perform assignment because the size of the left side
% is 1-by-1 and the size of the right side is 1-by-10.
Why do you calculate the same dB repeatedly? You could move this before the loop.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Gefragt:

am 25 Feb. 2022

Kommentiert:

Jan
am 25 Feb. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by