MATLAB Answers

Why do I get an error in this function and how to solve it?

1 view (last 30 days)
Haotian Wang
Haotian Wang on 29 Apr 2021
Answered: the cyclist on 29 Apr 2021
function r=OU_SAMPLE (n,dt,beta,theta,sig,r0);
t(1)=0;
x(1)=theta-r0;
r(1)=r0;
for i=1:n-1;
t(i+1)=i*dt;
v=(exp(2*beta*t(i+1))-exp(2*beta*t(i)))*(2*beta)^(-1);
x(i+1)=x(i)-sig*normrnd(0,sqrt(v));
r(i+1)=theta-exp(-beta*t(i+1))*x(i+1);
end;
end
function r=OU_SAMPLE (n,dt,beta,theta,sig,r0);
Error: Function definition not supported in this context. Create functions in code file.

Accepted Answer

the cyclist
the cyclist on 29 Apr 2021
You cannot define a function at the command line. You need to place the lines
function r=OU_SAMPLE (n,dt,beta,theta,sig,r0);
t(1)=0;
x(1)=theta-r0;
r(1)=r0;
for i=1:n-1;
t(i+1)=i*dt;
v=(exp(2*beta*t(i+1))-exp(2*beta*t(i)))*(2*beta)^(-1);
x(i+1)=x(i)-sig*normrnd(0,sqrt(v));
r(i+1)=theta-exp(-beta*t(i+1))*x(i+1);
end;
end
inside of a file (that conventionally should be named OU_SAMPLE.m).
Then you can call that function using
OU_SAMPLE() % with the appropriate arguments

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by