Clear Filters
Clear Filters

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 view (last 30 days)
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

Answers (1)

Walter Roberson
Walter Roberson on 14 Feb 2017
Edited: Walter Roberson on 14 Feb 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

Categories

Find more on Loops and Conditional Statements 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!