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
Jan
2022년 2월 25일
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에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!