2

I am completely new to Matlab. I am trying to simulate a Wiener and Poisson combined process.

Why do I get Subscripted assignment dimension mismatch?

I am trying to simulate

Z(t)=lambda*W^2(t)-N(t)

Where W is a wiener process and N is a poisson process.

The code I am using is below:

T=500
dt=1
K=T/dt
W(1)=0
lambda=3
t=0:dt:T
for k=1:K
r=randn
W(k+1)=W(k)+sqrt(dt)*r
N=poissrnd(lambda*dt,1,k)
Z(k)=lambda*W.^2-N
end
plot(t,Z)
Peter O.
  • 32,158
  • 14
  • 82
  • 96
syys
  • 63
  • 7
  • `W` is a vector, hence `W.^2` is a vector, hence `lambda*W.^2-N` is a vector. But you are trying to put it into one location of `Z`, that is `Z(k)`. – Phil Goddard Feb 18 '17 at 22:34
  • Thanks Phil. Didnt notice that when i wrote this! Cheers :) – syys Feb 19 '17 at 23:06

2 Answers2

0

you forget index

Z(k)=lambda*W.^2-N

it must be

Z(k)=lambda*W(k).^2-N(k)
Abo Lregal
  • 75
  • 8
0

It is true that some indexing is missing, but I think you would benefit from rewriting your code in a more 'Matlab way'. The following code is using the fact that Matlab basic variables are matrices, and compute the results in a vectorized way. Try to understand this kind of writing, as this is the way to exploit Matlab more efficiently, along with writing shorter and readable code:

T = 500;
dt = 1;
K = T/dt;
lambda = 3;
t = 1:dt:T;
sqdtr = sqrt(dt)*randn(K-1,1); % define sqrt(dt)*r as a vector
N = poissrnd(lambda*dt,K,1); % define N as a vector
W = cumsum([0; sqdtr],1); % cumulative sum instead of the loop
Z = lambda*W.^2-N; % summing the processes element-wiesly
plot(t,Z)

Example for a result:

wiener

EBH
  • 10,350
  • 3
  • 34
  • 59