没有合适的资源?快使用搜索试试~ 我知道了~
MATLAB编程快速入门 MATLAB编程基础入门教程 第4章 循环结构 共35页.pdf
2 下载量 141 浏览量
2022-12-17
22:49:37
上传
评论
收藏 522KB PDF 举报
温馨提示
试读
35页
MATLAB编程快速入门 MATLAB编程基础入门教程 第4章 循环结构 共35页.pdf
资源推荐
资源详情
资源评论
1
目录
第四章 循环结构..............................................................................................................................1
4.1 while 循环 ............................................................................................................................1
例 4.1..................................................................................................................................1
4.2 for 循环 ................................................................................................................................5
例 4.2..................................................................................................................................6
例 4.3..................................................................................................................................6
例 4.4..................................................................................................................................8
4.2.1 运算的细节..............................................................................................................9
例 4.5................................................................................................................................11
4.2.2 break 和 continue 语句 ...........................................................................................12
4.2.3 循环嵌套................................................................................................................13
4.3 逻辑数组与向量化...........................................................................................................14
4.3.1 逻辑数组的重要性................................................................................................15
例 4.6................................................................................................................................15
4.3.2 用 if/else 结构和逻辑数组创建等式 ....................................................................17
测试 4.1............................................................................................................................17
4.4 附加例子...........................................................................................................................18
例 4.7................................................................................................................................18
例 4.8................................................................................................................................23
4.5 总结...................................................................................................................................28
4.5.1 好的编程习惯总结................................................................................................29
4.5.2 matlab 总结 .............................................................................................................29
4.6 练习...................................................................................................................................29
4.1.....................................................................................................................................29
4.2.....................................................................................................................................29
4.3.....................................................................................................................................29
4.4.....................................................................................................................................29
4.5.....................................................................................................................................29
4.6.....................................................................................................................................30
4.7.....................................................................................................................................30
4.9.....................................................................................................................................31
4.10...................................................................................................................................31
4.11...................................................................................................................................31
4.12...................................................................................................................................31
4.13...................................................................................................................................31
4.14...................................................................................................................................31
4.15...................................................................................................................................31
4.16...................................................................................................................................31
4.17...................................................................................................................................32
4.18...................................................................................................................................32
4.19...................................................................................................................................32
4.20...................................................................................................................................32
4.21...................................................................................................................................33
4.22...................................................................................................................................33
4.23...................................................................................................................................33
4.24...................................................................................................................................33
4.25...................................................................................................................................34
4.26...................................................................................................................................34
4.27...................................................................................................................................34
4.28...................................................................................................................................34
1
第四章 循环结构
循环(loop)是一种 matlab 结构,它允许我们多次执行一系列的语句。循环结构有两种
基本形式:while 循环和 for 循环。两者之间的最大不同在于代码的重复是如何控制的。在
while 循环中,代码的重复的次数是不能确定的,只要满足用户定义的条件,重复就进行下
去。相对地,在 for 循环中,代码的重复次数是确定的,在循环开始之前,我们就知道代
码重复的次数了。
4.1 while 循环
只要满足一定的条件,While 循环是一个重复次数不能确定的语句块。它的基本形如
下
while expression
...
... code block
...
end
如果 expression 的值非零(true),程序将执行代码块(code block),然后返回到 while 语
句执行。如果 expression 的值仍然非零,那么程序将会再次执行代码。直到 expression 的值
变为 0,这个重复过程结束。当程序执行到 while 语句且 expression 的值为 0 之后,程序将
会执行 end 后面的第一个语句。
while 循环的伪代码为
while expr
...
...
...
end
我们将用 whlie 循环编写一个统计分析的程序。
例 4.1
统计分析在科学工程计算中,跟大量的数据打交道是非常平常的事,这些数据中的每
一个数据都是对我们关心的一些特殊值的度量。本课程的第一次测验的成绩就是一个简单
的例子。每一个成绩都对某一个学生在本课程中学到多少东西的度量。
许多的时侯,我们并不关心某一个单个数据。我们可以通过总结得到几个重要的数
据,以此告诉我们数据的总体情况。例如,一组数据的平均数(数学期望)和标准差。平均
数的定义如下:
N
i
i
x
N
x
1
1
(4.1)
其中 x
i
代表 n 个样本中的第 i 个样本。如果所有的输入数据都可以在一个数组中得
到,这些数据的平均数就可以通过公式(4.1)直接计算出来,或应用 matlab 的内建函数
mean。
标准差的定义如下:
2
)1(
)(
2
1 1
2
NN
xxN
s
N
i
N
i
ii
(4.2)
标准差则体现随机变量取值与其期望值的偏差。标准差的值较大,则表明该随机变量
的取值与其期望值的偏差较大,反之,则表明此偏差较小。如果所有的输入数据都可以在
一个数组中得到,这些数据的平均数就可以通过公式(4.2)直接计算出来,或应用 matlab 的
内建函数 std。本例的目的是要通过公式 4.1,4.2 计算平均数和标准差,向大家介绍 while
循环的应用。我们要执行的算法是读取一个组数据,计算它们的平均数和标准差,最后输
出结果。
答案:
程序必须能读取一系列的测量值,并能够计算出这些测量值的数学期望和标准差。在
进行计算之前,我们有 while 循环来读取这些测量值。
当所有的测量值输入完毕,我们必须通过一定的方法来告诉程序没有其它的数据输入
了。在这里,我们假设所有测量值均为非负数,我们用一个负数来表示数据输入完毕。当
一个负数输入时,程序将停止读取输入值,并开始计算这些数据的数学期望和方差。
1.陈述问题因为我们假设所有的输入数据为非负数,则合适地问题陈述为:计算一组测
量数的平均数和方差,假设所有测量值为非负数;假设我们事先不知道有多少个测量数。一
个负数的输入值将代表测量值输入的结束。
2.定义输入值和输出值这个程序的输入是未知数目的非负数。输出为这些非负数的平
均数和标准差。顺便还要打印出输入数据的数据,以便于检测程序的正确性。
3.设计算法这个程序可分为以下三大步:
Accumulate the input data
Calculate the mean and standard deviation
Write out the mean, standard deviation, and number of points
每一大步的为读取输入值。为达此目的,我们必须提示用户输入合适的值。当数据输
入完毕,我们将计算出数据的个数,它们的和与平方和。这些步骤的伪代码如下所示
Initialize n, sum_x, and sum_x2 to 0
Prompt user for first number
Read in first x
while x >=0
n←n+1
sum_x←sum_x + x
sum_x2←sum_x2 + x^2
Prompt user for next number
Read in next x
end
注意我们必须在 while 循环开始之前,我们必须读取第一个值,这样在 while 循环第一
次运行中才有了检测值。
下一步,我们要计算出数学期望和标准差。这个步骤的伪代码就是公式(4.1)和(4.2)
的 matlab 版本。
x_bar ← sum_x / n
std_dev ← sqrt((n*num_x2 – sum_x^2) / (n*(n-1)))
最后我们写出输出结果:
Write out the mean value x_bar
Write out the standard deviation std_dev
Write out the number of input data points n
4.将伪代码转化为相应的 matlab 语句最终的 matlab 程序如下
% Script file: stats_1.m
%
% Purpose:
% To calculate mean and the standard deviation of
3
% 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,那么它的平均数和标准差
分别为
412
3
11
1
N
i
i
x
N
x
1
)1(
)(
2
1 1
2
NN
xxN
s
N
i
N
i
ii
我们把这些值输入程序后,产生的结果为
>> 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
我们的错误就在于我们没有检测程序所有可能的输入类型。请重看一遍程序。如果我
们不输入数或者只输入一个数,那么上面的公式就会出现除以 0 的情况。这种除 0 错误将
会在导致在命令窗口内出现 divide-by-zero 的警告,导致输出值为无穷大(NaN)。我们需要
修改这个程序来解决这个问题,告诉用户这个问题是什么,并在适当的时侯停止。这个程
序的修定版本为 stats_2。在运行运算之前,我们必须检查是否有足够多的输入值。如果没
有,程序将打印出错误提示信息,并且跳出。你自己检测一下这个版本的程序。
% Script file: stats_2.m
%
% Purpose:
% To calculate mean and the standard deviation of
% 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
% 1. 12/06/97 S. J. Chapman Correct divide-by-0 error if
% 0 or 1 input values given.
%
% 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
% Check to see if we have enough input data.
if n < 2 % Insufficient information
disp('At least 2 values must be entered!');
else % There is enough information, so
% 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);
end
注意平均数和标准差可以通过 MATLAB 的内建函数 mean 和 std 计算得到,输入数据
存储在一个向量内,并把向量作为函数的参数。在本章章末的练习中,将会要求你用标准
的 matlab 函数创建一个新的版本程序。
剩余34页未读,继续阅读
资源评论
passionSnail
- 粉丝: 403
- 资源: 5624
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功