Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The triangular probability function is given by f(x) {x/b^2 0<=x<=b f(x) {(2b-x)

ID: 3559663 • Letter: T

Question

The triangular probability function is given by

f(x) {x/b^2 0<=x<=b

f(x) {(2b-x)/(b^2) b<=x<=2b

where b > 0 and f(x) = 0 , otherwise. Note that the mean of this random variable is m

= b and the variance is a^2=(b^2)/6

(a) Write a user-defined function called triangular_func that takes a constant b and a

vector x and returns the triangular function for each value of the elements of x.

(b) Write a main Matlab program that asks the user to enter the value of the constant b

and a very large number N (at least 1,000). Then generate N triangularly distributed

random variables, each having the above triangular probability distribution.

(i) Calculate the true mean m and the true variance with the true theta^2  of the random variable.

(ii) Estimate the sample mean (xbar) and sample variance ( s^2 ) from the N generated

random samples.

(iii) Compare the sample mean with the true mean and the sample variance with

the true variance by calculating the corresponding percent errors as (m-xbar / m)*100% and

(theta^2-s^2)/(theta^2)*100%, respectivly.

(c) Use linspace to generate 100 values of X in the interval (0,b) and, using

triangular_func, generate the corresponding triangular function Y. Plot the triangular

function.

(d) Generate a normalized histogram of the samples (using the Matlab function 'hist' )..

Use the hold command to superimpose the two plots.

Hint: Note that one way to generate a random variable X with the triangular probability

distribution is to sum two independent standard uniform (0,1) random variables and

multiplying the sum by b, i.e., x= b( U1 U2) where "U1 and U2"  are standard uniform

random variables, that can be independently generated from the Matlab function rand.

Explanation / Answer

Part- A

prompt2='Enter value of b: ';
b=input(prompt2);
x=[....]; %define your vector
N=length(x);
z= zeros(N,1);
for i=1:N
if x(i) <0.5
z(i)=sqrt(x(i)*2*b^2);
else
z(i)=2*b-sqrt((1-x(i))*2*b^2);
end
end

Part- B

prompt1 = 'How many elements you want: ';
N = input(prompt1);
prompt2='Enter value of b: ';
b=input(prompt2);

u= rand(N,1);
x= zeros(N,1);
for i=1:N
if u(i) <0.5
x(i)=sqrt(u(i)*2*b^2);
else
x(i)=2*b-sqrt((1-u(i))*2*b^2);
end
end
m=b;
sigma=b^2/6;
x_bar=mean(x);
s=var(x);
fprintf('i) True mean= %0.2f ',m);
fprintf('i) True variance= %0.2f ',sigma);
fprintf('ii) Sample mean= %0.2f ',x_bar);
fprintf('ii) Sample variance= %0.2f ',s);
error_mean=abs(m-x_bar)/m*100;
error_var=abs(sigma-s)/sigma*100;
fprintf('iii) Percentage error in mean= %0.2f ',error_mean);
fprintf('iii) Percentage error in variance= %0.2f ',error_var);

hist(x);

Part- C and D (I'm not sure about this part)

prompt2='Enter value of b: ';
b=input(prompt2);
N = 100;
u= linspace(0,b,N);
x= zeros(N,1);
for i=1:N
if u(i) <0.5
x(i)=sqrt(u(i)*2*b^2);
else
x(i)=2*b-sqrt((1-u(i))*2*b^2);
end
end
t=1:N;
plot(t,x);
hold on;
hist(x);
hold off;