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

Use MATLAB: Write a code that accepts numbers a, b and the vector X as inputs. T

ID: 3349163 • Letter: U

Question

Use MATLAB:

Write a code that accepts numbers a, b and the vector X as inputs. The vector X contains n samples of a standard normal distribution. Generate n samples for a normal distribution with mean b and variance a2. Call the output vector Y. Using given code below check if the sample mean and sample variance of the vector Y are near to b and a2 for n = 10, 100 and 1000.

CODE:

function X = smean_svar(n)

smean = sum(n)/length(n);

disp(smean)

svar = (sum((n-smean).^2))/(length(n)-1);

disp(svar)

end

Explanation / Answer

Hello,
          Please find the answer attached as under. Please give a thumbs up rating if you find the answer useful! Have a rocking day ahead!

******** Function definition ************

function X = smean_svar(n)
smean = sum(n)/length(n);
disp(smean)
svar = (sum((n-smean).^2))/(length(n)-1);
disp(svar)
end

******** Matlab Code ********

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Mean and variance of normally distributed data

%%%%%%%%%%%%%%%%%%%%%%%% Inputs %%%%%%%%
clc;
a = 5;                              % standard deviation
b = 500;                            % mean
n1 = 10; n2 = 100; n3 = 1000;       % number of data points

x1 = randn(n1,1);                  % normal input data with 10 points
x2 = randn(n2,1);                  % normal input data with 100 points
x3 = randn(n3,1);                  % normal input data with 1000 points

%%%%%%%%%%%%%%%%%%%%%% Output %%%%%%%%%%%%%%%%%%%

y1 = a.*x1 + b;           % normal output data with 10 points
y2 = a.*x2 + b;           % normal output data with 100 points
y3 = a.*x3 + b;           % normal output data with 1000 points

fprintf('========== Sample mean and variance for n=10========== ');
smean_svar(y1);
fprintf('========== Sample mean and variance for n=100========== ');
smean_svar(y2);
fprintf('========== Sample mean and variance for n=1000=========== ');
smean_svar(y3);

*********** End of Code ********

Output:

========== Sample mean and variance for n=10==========
500.9687

   29.3732

========== Sample mean and variance for n=100==========
498.9828

   25.3288

========== Sample mean and variance for n=1000===========
499.9238

   24.8444

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote