GEN 500 Engineering System Analysis Spring 18 Final Exam Name: Banner ID: Proble
ID: 2087430 • Letter: G
Question
GEN 500 Engineering System Analysis Spring 18 Final Exam Name: Banner ID: Problem] #1 Write a Matlab script file to complete the following tasks. (1) Use the command "rand" to generate 100 random numbers which are uniformly distributed in interval (0,1) (2) Use "for-end" loop to calculate the sum, the average and the standard deviation of all 100 random numbers generated in (1) Sz x(k) x( 1) + x(2) +X(3) + + x(N) Average: ? SSE N-1 Standard deviation: ? (3) Uses "for-end" loop and "if-end to find the maximal and minimal numbers and their indices for these 100 random numbers generated. (4) Use matlab commands "sum", "mean", "std", "max" and "min" to find the sum, the average, the standard deviation, the maximum and the minimum. (Compare the results with (2) and (3)Explanation / Answer
1 ans:
MATLAB CODING:
% Creating a vector with 100 elements randomly distributed between 0 and 1
rand_num=rand([1 100]);
% ========== Calculation of sum ===========
sum_num=0; % Initializing sum variable to 0
for i=1:100
sum_num=sum_num+rand_num(i);
end
% =========== Calculation of average ===========
average_num=(sum_num/100);
% Calculation of standard deviation
sse=0; % Initializing sse variable to 0
for i=1:100
sse=sse+((rand_num(i)-average_num)^2);
end
std_num = sqrt(sse/99);
% ======== Calculation of maximum and minimum number ========
max_num=rand_num(1);
min_num=rand_num(1);
for i=1:100
if(max_num<rand_num(i))
max_num=rand_num(i) ;
max_index=i;
end
if(min_num>rand_num(i))
min_num=rand_num(i) ;
min_index=i;
end
end
%========= Comparison of the statistical values ============
display('=== Comparison of sum ===');
disp('manual calculation: ');
disp(sum_num);
disp('inbuilt function: ');
disp(sum(rand_num));
display(' ');
display('=== Comparison of average ===');
disp('manual calculation: ');
disp(average_num);
disp('inbuilt function: ');
disp(mean(rand_num));
display(' ');
display('=== Comparison of standard deviation ===');
disp('manual calculation: ');
disp(std_num);
disp('inbuilt function: ');
disp(std(rand_num));
display(' ');
display('=== Comparison of maximum number ===');
disp('manual calculation: ');
disp(max_num);
disp('inbuilt function: ');
disp(max(rand_num));
display(' ');
display('=== Comparison of minimum number ===');
disp('manual calculation: ');
disp(min_num);
disp('inbuilt function: ');
disp(min(rand_num));
OUTPUT:
=== Comparison of sum ===
manual calculation:
50.4444
inbuilt function:
50.4444
=== Comparison of average ===
manual calculation:
0.5044
inbuilt function:
0.5044
=== Comparison of standard deviation ===
manual calculation:
0.2749
inbuilt function:
0.2749
=== Comparison of maximum number ===
manual calculation:
0.9880
inbuilt function:
0.9880
=== Comparison of minimum number ===
manual calculation:
0.0287
inbuilt function:
0.0287
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.