error_message.m
来自「matlab源代码,适用于开发研究,带来很好的学习效果.」· M 代码 · 共 41 行
M
41 行
% Script file error_message.m
%
% Purpose:
% To calculate mean and the standard deviation of
% an input data set containing and arbitrary number
% of input values.
%
% Define variables:
% n The number of input samples
% std_dev The standard devation of the input samples
% sum1 The sum of the input values
% sum2 The sum of the squares of the input values
% x input data value
% xvar The average of the input samples
% Initalize variables
sum1=0;sum2=0;
% Get the number of points to input
n=input('Enter the number of points: ');
% Check to see if we have enough input data.
if n<2
warning('Not enough input data');
else
% construct if loop
for ii=1:n
x=input('Enter value: ');
sum1=sum1+x;
sum2=sum2+x^2;
end
%Calculate the mean and standard deviation
xvar=sum1/n;
std_dev=sqrt((n*sum2-sum1^2)/(n*(n-1)));
%Print the result
fprintf('The mean of this data set is: %f\n',xvar);
fprintf('The standard deviation is: %f\n',std_dev);
fprintf('The number of data is: %d\n',n);
end
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?