mercredi 24 avril 2019

How do you create a for loop in MATLAB that evaluates a random variable then stores answer in matrix?

My code is not working properly! I am attempting to call on a random exponential random variable with lambda = 10. The amount of times I call it is equal to (a), I can choose whatever I want. If the random variable is within the acceptable interval of 0 and 1, I want to evaluate it in the equation given and store it in h (a zero matrix I created before the for loop). If the random variable is not in the acceptable range, the equation just equals 0. I want to store all values in the h matrix (evaluated and zeros). I think my logic is correct, but after I run it, my h matrix is still all zeros.

function Importance1(a)

%samplesize = a;
%Hx = gx/fx
% equation to use
%hx = @(x) ((abs(cos(pi*x)).^(2.5)).*exp(-10*x))./(exp(-2*abs(x))); 

x = exprnd(10, a, 1); %exponential X ~ (10)
h = zeros(a, 1);
for i=1:1:a
    if x(i) <= 1 && x(i) >= 0
        h(i)=((abs(cos(pi*x(i))).^(2.5)).*exp(-10*x(i)))./(exp(-2*abs(x(i))));
    else 
        h(i) = 0;
    end
end 

mcvalue1 = (1/a)*sum(h); %expected value

%Actual value of hx
hx = @(x) ((abs(cos(pi*x)).^(2.5)).*exp(-10*x))./(exp(-2*abs(x)));
hi = integral(hx, 0,1);

%calculate the relative error; from the known value
error1 =((abs(mcvalue1-hi))/(hi)).*100;




Aucun commentaire:

Enregistrer un commentaire