How to get mean value of x(i) in a loop?!

3 views (last 30 days)
Hey guys,
I've written a code like this. Everything is fine except I don't know how to calculate the mean of x(i) values at the end (I get errors when I put it in different places of my code!). Could any of you help me please? Here is the code (I want to calculate 1000 numbers, let's call them x(1), x(2), ... and finally take the average):
clc;
clear;
syms fS fR k Alpha uR uS y
N = 1000;
mu = 16;
sigma = 25;
uR = 20;
uS = 10;
Alpha = 12.153;
k = 6.010;
VR = 0.1;
VS = 0.25;
mu_R = uR * gamma (1 + 1/Alpha);
mu_S = uS * gamma (1 - 1/k);
SD_R = mu_R * VR;
SD_S = mu_S * VS;
fS == ((k/uS)*(uS/y)^(k+1))*exp(-(uS/y)^k);
fR == (Alpha/uR)*((y/uR)^(Alpha-1))*exp(-(uS/y)^k);
for i=1:N
R(i) = (mu_R + SD_R*rand());
S(i) = (mu_S + SD_S*rand());
hR(i) = normpdf(R(i), mu, sigma);
hS(i) = normpdf(S(i), mu, sigma);
h(i) = hR(i)*hS(i);
fS(i) = 0.601*exp(-1.0*(10.0/S(i))^(601/100))*(10.0/S(i))^(701/100);
fR(i) = 0.60765*exp(-1.0*(10.0/R(i))^(601/100))*(0.05*R(i))^(11153/1000);
fRS(i) = fR(i)*fS(i);
if (R(i)<S(i))
I(i)=1;
x(i) = (I(i)/h(i))*fRS(i);
end
end
Pf = mean(x)
Thank you!

Accepted Answer

Walter Roberson
Walter Roberson on 16 Oct 2015
Your statements
fS == ((k/uS)*(uS/y)^(k+1))*exp(-(uS/y)^k);
fR == (Alpha/uR)*((y/uR)^(Alpha-1))*exp(-(uS/y)^k);
with fS and fR and y as symbolic values, create a symbolic comparison, and then throw away the results of that symbolic comparison. Remember, "==" is not an assignment statement.
These two lines are the only place that y occurs.
If you compare the names you declare as syms to your code, then other than y (which is not used in any computation other than to throw the values away), every other syms you declare is later replaced with a numeric value. You might as well not declare any of them to be syms at all (and delete the two useless "==" lines)
Before calculating mean(x) you need to know that x has been assigned at least one value. And you do not know that, because the only place you assign to x is inside an "if" that might possibly never be true. And it certainly might not be true for on the last iteration of the loop (or the one before, or the one before that or ...) so you do not know how long x is going to end up because it will only end up the length of the last "i" for which the condition holds.
  2 Comments
Antonio
Antonio on 16 Oct 2015
Thanks for your answer, does that mean I should use x=0 before the loops?! I did that but my final result i.e. Pf = mean(x) becomes zero!
Walter Roberson
Walter Roberson on 16 Oct 2015
That suggests that there were no cases where your "if" was true.
I do not have the Statistics toolbox so I cannot test your code myself.

Sign in to comment.

More Answers (0)

Categories

Find more on Argument Definitions 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!