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 个评论

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.

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

提问:

2022-2-25

评论:

Jan
2022-2-25

Community Treasure Hunt

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

Start Hunting!

Translated by