文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > 高等教育 > 工学 > Stephen J.Chapman《MATLAB 编程(第二版) 》第四章
侵权投诉

Stephen J.Chapman《MATLAB 编程(第二版) 》第四章

Stephen J.Chapman《MATLAB 编程(第二版) 》第四章

% an input data set containing an arbitrary number % of input values. %

% Record of revisions:

% Date Programmer Description of change

% ==== ========== ===================== % 12/05/97 S. J. Chapman Original code %

% Define variables:

% n -- The number of input samples

% std_dev -- The standard deviation of the input samples % sum_x -- The sum of the input values

% sum_x2 -- The sum of the squares of the input values % x -- An input data value

% xbar -- The average of the input samples % Initialize sums.

n = 0; sum_x = 0; sum_x2 = 0; % Read in first value

x = input('Enter first value: ');

% While Loop to read input values. while x >= 0 % Accumulate sums. n = n + 1; sum_x = sum_x + x; sum_x2 = sum_x2 + x^2; % Read in next value

x = input('Enter next value: '); end

% Calculate the mean and standard deviation x_bar = sum_x / n;

std_dev = sqrt( (n * sum_x2 - sum_x^2) / (n * (n-1)) ); % Tell user.

fprintf('The mean of this data set is: %f\n', x_bar); fprintf('The standard deviation is: %f\n', std_dev); fprintf('The number of data points is: %f\n', n);

5.检测程序为检测这个程序,我们将手工算出一组简单数据的平均数和标准差,然后与程序产生的结果进行比对。如果我们用三个输入值:3,4和5,那么它的平均数和标准差分别为

1N1

=∑xi=×12=4

Ni=13

N∑xi2 (∑xi)

N

N

2

s=

N(N 1)

=1

我们把这些值输入程序后,产生的结果为

>> stats_1

Enter first value: 3 Enter next value: 4 Enter next value: 5 Enter next value: -1

The mean of this data set is: 4.000000 The standard deviation is: 1.000000 The number of data points is: 3.000000

这个结果说明了程序的正确性。在这个例子中,我们并没有完全遵循设计过程。这个失误导致这个软件有一个致命的缺陷。你能指出来它吗?

上一页第4页

你可能喜欢

热门文档

相关文档

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email