bm.m
来自「Brownian Motion, Option.」· M 代码 · 共 43 行
M
43 行
% This program simulates Brownian motion and Geometric Brownian motion. fprintf('\n This program simulates:')fprintf('\n 1. Brownian motion \n 2. Geometric Brownian motion \n')type = input('\n Choose one of the above options (enter 1 or 2): ');mu = input('\n Enter parameter mu: '); sigma = input('\n Enter paremeter sigma: ');S0 = input('\n Enter value at time 0: ');T = input('\n Enter the time horizon T: ');n = input('\n Enter the number of points in the discretization: ');delta = T/n;S = S0;alpha = mu - sigma^2/2;switch type case 1 for i=1:n S = [S; S(i) + mu*delta + sigma*sqrt(delta)*randn]; end plot(delta*[0:1:n], S,'b') title(['Brownian motion, \mu = ',num2str(mu),',\sigma = ',num2str(sigma)]) xlabel('t') case 2 for i=1:n S = [S; S(i)*exp(alpha*delta + sigma*sqrt(delta)*randn)]; end plot(delta*[0:1:n], S, 'g') title(['Geometric Brownian motion, \mu = ',num2str(mu),',\sigma = ',num2str(sigma)]) xlabel('t') otherwise fprintf('\n Error: That is not a valid option \n') end
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?