用包含随机变量的子图绘制两个图

时间:2014-07-08 13:56:09

标签: matlab random

我试图用随机变量绘制维纳过程的两个图(见下面的代码),如何让matlab绘制一个图,然后用不同的随机数重新计算和重新绘制,因为目前这产生两个相同的的曲线图。

T=1;
n=100;
dt=T/n;
random=randn(1,n); 
dW=sqrt(dt)*random;
W=cumsum(dW);
t=0:dt:T;
W=[0,W];
figure;
subplot(2,1,1);
plot(t,W); xlabel('Time'); ylabel('W(t)'); title('Simple Wiener process a)'); grid    minor;  
subplot(2,1,2);
plot(t,W); xlabel('Time'); ylabel('W(t)'); title('Simple Wiener process b)'); grid minor; 

所以我想在这个过程的同一个图上绘制两个图。

2 个答案:

答案 0 :(得分:1)

如何绘制一个图表,重新计算,然后使用不同的随机数重新绘制? ; P

T=1;
n=100;
dt=T/n;
random=randn(1,n); 
dW=sqrt(dt)*random;
W=cumsum(dW);
t=0:dt:T;
W=[0,W];
figure;
subplot(2,1,1);
plot(t,W); xlabel('Time'); ylabel('W(t)'); title('Simple Wiener process a)'); grid    minor; 
random=randn(1,n); 
dW=sqrt(dt)*random;
W=cumsum(dW);
W=[0,W]; 
subplot(2,1,2);
plot(t,W); xlabel('Time'); ylabel('W(t)'); title('Simple Wiener process b)'); grid minor; 

答案 1 :(得分:0)

一旦您将randn的输出分配给它不会发生变化的变量,您需要拨打randn两次。因此,基本上最简单的方法是在第一次plot之后重写第4 - 8行中的所有代码。

然而,在您的情况下,更快的方法就是创建一个包含2行的随机向量:

n=100;
dt=T/n;
random=randn(2,n); 
dW=sqrt(dt)*random;
W=cumsum(dW);
t=0:dt:T;
W=[[0;0],W];
figure;
subplot(2,1,1);
plot(t,W(1,:)); xlabel('Time'); ylabel('W(t)'); title('Simple Wiener process a)'); grid    minor;  
subplot(2,1,2);
plot(t,W(2,:)); xlabel('Time'); ylabel('W(t)'); title('Simple Wiener process b)'); grid minor;