i am trying to add the value of a certain function to an array, but there seems to be some error. the code is as follows. could you please help? trying to store the value of Thetahat into A for every x.

1 visualización (últimos 30 días)
function [inseed] = optsim (inseed, nreps)
sum = 0;
sum2 = 0;
for x = 0:5:60
for j= 1:nreps
[inseed, profit] = poisprofit(inseed, x);
sum = sum+profit;
sum2 =sum2 + profit^2;
end
Thetahat = sum/nreps;
sigmahat = sqrt(sum2/nreps-Thetahat^2);
sehatThetahat = sigmahat/sqrt(nreps);
A(x)= profit;
D(x) = x;
end
end

Respuestas (1)

Walter Roberson
Walter Roberson el 14 de Feb. de 2017
Editada: Walter Roberson el 14 de Feb. de 2017
Only positive integers can be used as indices.
You should use a pattern more like
sum1 = 0;
sum2 = 0;
xvals = 0:5:60;
for xidx = 1 : length(xvals)
x = xvals(xidx);
for j= 1:nreps
[inseed, profit] = poisprofit(inseed, x);
sum1 = sum1+profit;
sum2 =sum2 + profit^2;
end
Thetahat = sum1/nreps;
sigmahat = sqrt(sum2/nreps-Thetahat^2);
sehatThetahat = sigmahat/sqrt(nreps);
A(xidx)= profit;
D(xidx) = x;
end
end

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by