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

2 vues (au cours des 30 derniers jours)
Haotian Wang
Haotian Wang le 29 Avr 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.

Réponse acceptée

the cyclist
the cyclist le 29 Avr 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

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by