📄 xgenrn.m
字号:
% xgenrn.m
% Scope: This MATLAB program generates random numbers with normal (Gaussian)
% distribution, with mean and standard deviation specified; plots the
% generated sequence, histogram, and the normalized auto-correlation
% sequence (optional).
% Usage: xgenrn
% Inputs: - number of random numbers to be generated (nstep)
% - mean of random numbers to be generated
% - standard deviation of random numbers to be generated
% - value of the initial seed
% - name of the output file, if the generated data are saved
% Outputs: - random numbers sequence plot
% - histogram plot
% - plot of the normalized auto-correlation sequence (optional)
% Remarks: The generation of the random number sequence is executed in one
% step, i.e. one call to the module genrn.
% External Matlab macros used: genrn, rms, xcorr (from Signal Processing
% Toolbox) - optional
% Last update: 06/28/00
% Copyright (C) 1996-00 by LL Consulting. All Rights Reserved.
clear
yes = 'y';
answer1 = yes;
while (strcmp(answer1,yes) == 1)
clear x
close all
% Specify the input data
disp(' ');
nstep = input('Specify the number of random numbers to be generated --> ');
disp(' ');
xmean = input('Specify mean of random numbers to be generated --> ');
disp(' ');
xstd = input('Specify st. dev. of random numbers to be generated --> ');
disp(' ');
iseed = input('Specify the value of the initial seed, iseed, e.g. 0 --> ');
disp(' ');
% Generate the random numbers with specified mean and standard deviation
x = genrn(nstep,xmean,xstd,iseed);
% Determine mean, standard deviation and root mean square (rms) for the
% generated random numbers
xmean = mean(x);
xstd = std(x);
xrms = rms(x);
temp1 = ['mean = ',num2str(xmean)];
temp2 = ['st.dev. = ',num2str(xstd)];
temp3 = ['rms = ',num2str(xrms)];
g = [temp1,' ; ',temp2,' ; ',temp3];
% Save the generated data into a specified file if desired
answer2 = input('Save the generated data? (y/n)[y] --> ','s');
disp(' ');
if isempty(answer2)
answer2 = 'y';
end
if strcmp(answer2,yes) == 1
f2 = input('Specify the output filename --> ','s');
disp(' ');
for k = 1:nstep
fprintf(f2,'%24.14e\n',x(k));
end
end
% Plot the random number sequence
dd = num2str(nstep);
aa = 'Random number process - with ';
aa = [aa dd ' time sequences'];
ee = 'Random number sequence';
ff = 'Time sequence';
disp('Execute random numbers graph - ');
disp('Select the mouse position to insert statistics.');
disp('Press a key to continue...');
pause
figure(1)
plot(x),...
title(aa), ylabel(ee), xlabel(ff), grid,...
gtext(g); % mouse placement of the text on a graph
disp(' ');
disp('Random numbers graph - check the window associated to this figure.');
disp('Press a key to continue...');
pause
% Plot the histogram
aaa = 'Histogram for the random number process - with ';
aaa = [aaa dd ' time sequences'];
eee = 'Random number value ';
fff = 'Number of random numbers';
figure(2)
hist(x,33),... % number of bins is selected as 33
title(aaa), xlabel(eee), ylabel(fff), grid
disp(' ');
disp('Histogram graph - check the window associated to this figure.');
disp('Press a key to continue...');
pause
% Plot the normalized auto-correlation sequence
disp(' ');
disp('For the auto-correlation sequence - the function XCORR from ');
disp('Signal Processing Toolbox is required');
answer3 = input('Plot the auto-correlation sequence? (y/n)[y] --> ','s');
if isempty(answer3)
answer3 = 'y';
end
if strcmp(answer3,yes) == 1
b = -(nstep-1):1:(nstep-1);
a = xcorr(x','coeff');
bb = 'Normalized auto-correlation sequence';
cc = 'Sequence lag';
figure(3)
plot(b,a'),...
title(aa), ylabel(bb), xlabel(cc), grid
disp(' ');
disp('Auto-correlation graph - check the window for this figure.');
disp('Press a key to continue...');
pause
end
% Select another computation, if desired
disp(' ');
answer1 = input('Do you want another computation ? (y/n)[y] --> ','s');
if isempty(answer1)
answer1 = 'y';
end
end
disp(' ');
disp('End of the program XGENRN');
disp(' ');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -