Problem with dimension of a vector

3 views (last 30 days)
FrancescoMi
FrancescoMi on 15 Oct 2015
Edited: Thorsten on 15 Oct 2015
Hi, my code is the this
rho = 0.03;
c = 0.05;
eta = 0.2;
mu = 1.2;
sigma = 0.8;
alpha = -rho/eta;
pzero=(eta*mu+rho*c)/(rho+eta);
Passo = 0.01;
pMax = 15;
N = 10;
dt = 1e-2;
time = 0:dt:4;
W = zeros(1,length(time));
x = zeros(N,length(time));
rng(1);
pStar = zeros(1,N);
tauStar = zeros(1,N);
front=fzero(@(p)(p-c)*(sqrt(2*eta)/sigma).*quad(@(t) t.^(-alpha).*exp(-(t.^2)/2+((p-mu)/sigma)*sqrt(2*eta).*t),0,15)/gamma(-alpha)-quad(@(t) t.^(-alpha-1).*exp(-(t.^2)/2+((p-mu)/sigma)*sqrt(2*eta).*t),0,15)/gamma(-alpha),pzero+0.11);
cont=1;
for p = 0:Passo:pMax
if p < front
v(cont)=(front-c)*(quad(@(t) t.^(-alpha-1).*exp(-(t.^2)/2+((p-mu)/sigma)*sqrt(2*eta).*t),0,15)/gamma(-alpha))/(quad(@(t) t.^(-alpha-1).*exp(-(t.^2)/2+((front-mu)/sigma)*sqrt(2*eta).*t),0,15)/gamma(-alpha));
else
v(cont)=p-c;
end
asc(cont)=p;
for j = 1:N
for i = 1:length(time)-1
W(i+1) = W(i)+sqrt(dt)*exp(eta*time(i+1))*randn;
end
ex = exp(-eta*time);
x(j,:) = p*eta+mu*(1-ex)+sigma*ex.*W;
end
for j = 1:N
for i = 1:length(time)-1
if x(j,i) >= front
pStar(1,j) = x(j,i);
tauStar(1,j) = time(1,i);
funVal(cont,j) = exp(-rho*tauStar(1,j))*(pStar(1,j)-c);
break
end
end
end
meanFunVal(cont) = sum(funVal(cont,:))/N;
cont=cont+1;
end
The error is:
Attempted to access funVal(7,:); index out of bounds because size(funVal)=[6,8].
Error in pstar (line 56)
meanFunVal(cont) = sum(funVal(cont,:))/N;
But if I don't write meanFunVal(cont) = sum(funVal(cont,:))/N; everything is ok. How is possible?? Is a simple vector and a simple sum. Can you help me?

Answers (1)

Thorsten
Thorsten on 15 Oct 2015
Edited: Thorsten on 15 Oct 2015
For cont == 7, the expression x(j,i) >= front is always false so
funVal(cont,j) = exp(-rho*tauStar(1,j))*(pStar(1,j)-c);
is never executed, i.e., funVal(7,:) does not exist.
Not that many entries in funVal are never set, i.e, they are set to zero. Taking the mean averages across all values, including the zeros. I'm not sure if this is what you want.
If you don't want to average over the values not set, but only over the non-zero values, you can pre-allocate funVal at the start of the loop
funVal = nan(1501,10);
and after the for loop, compute the mean as
meanFunVal = nanmean(funVal');

Categories

Find more on Financial Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!