云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 4 第四章

4 第四章

  • 62 次阅读
  • 3 次下载
  • 2025/12/2 22:25:35

366。

3.2001年不是闰年。它的1月1号对应的day of year是30。这个程序5次运行后的结果分别为 >> doy This program calculates the day of year given the current date. Enter current month (1-12):1 Enter current day(1-31):1 Enter current year(yyyy): 1999 The date 1/ 1/1999 is day of year 1. >> doy This program calculates the day of year given the current date. Enter current month (1-12):12 Enter current day(1-31):31 Enter current year(yyyy): 1999 The date 12/31/1999 is day of year 365. >> doy This program calculates the day of year given the current date. Enter current month (1-12):1 Enter current day(1-31):1 Enter current year(yyyy): 2000 The date 1/ 1/2000 is day of year 1. >> doy This program calculates the day of year given the current date. Enter current month (1-12):12 Enter current day(1-31):31 Enter current year(yyyy): 2000 The date 12/31/2000 is day of year 366. >> doy This program calculates the day of year given the current date. Enter current month (1-12):3 Enter current day(1-31):1 Enter current year(yyyy): 2001 The date 3/ 1/2001 is day of year 60. 通过5次不同情况的检测,这个程序给出了正确的结果。 例4.4

统计分析

执行如下算法:

输入一系列的测量数,计算它们的平均数和标准差。这些数可以是正数,负数或0。 答案:

这个程序必须能够读取大量数据,并能够计算出这些测量值的平均数和标准差。这些测量值可以是正数,负数或0。

8

因为我们再也不能用一个数来表示数据中止的标识了,我们要求用户给出输入值的个数,然后用for循环读取所有数值。

下面的就是这个修定版本的程序。它允许各种输入值,请你自己验证下面5个输入值的平均数和标准差:3,-1,0,1,-2。 % Script file: stats_3.m % % Purpose: % To calculate mean and the standard deviation of % an input data set, where each input value can be % positive, negative, or zero. % % Record of revisions: % Date Programmer Description of change % ==== ========== ==================== % 12/08/97 S. J. Chapman Original code % % Define variables: % ii Loop index % 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. sum_x = 0; sum_x2 = 0; % Get the number of points to input. n = input('Enter number of points: '); % Check to see if we have enough input data. if n < 2 % Insufficient data disp ('At least 2 values must be entered.'); else % we will have enough data, so let's get it. % Loop to read input values. for ii = 1:n % Read in next value x = input('Enter value: '); % Accumulate sums. sum_x = sum_x + x; sum_x2 = sum_x2 + x^2; end % Now calculate statistics. 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 4.2.1 运算的细节

既然我们已经看了许多for循环的例子。在用for循环时,我们必须检查许多重要的细节。

1.没有必要缩进for循环的循环体。即使所有语句都左对齐,matlab程序也会识别出这个循环。但缩进循环体能增强代码的可读性,所以建议大家缩进循环体。

9

好的编程习惯

对于for循环体总是要缩进两个或更多空格,以增强程序的可读性。

2.在for循环中,我们不能随意修改循环指数。循环指数常被用作计算器,

如果修改了它们将会导致一些奇怪而难以发现的错误。下面的一个例子将初始化一个函数的数组。但是语句“ii=5”的突然出现,导致只有a(5)得到了初始化,它得到了本应赋给a(1),a(2)等等的值。 for ii = 1:10 ... ii = 5 ; %Error! ... a(ii) = end 好的编程习惯

在循环体中绝不修改循环指数的值。

3.我们在第二章已经学过,用赋值的方法可以扩展一个已知的数组。例如,语句 arr = 1:4; 定义了一个数组[1 2 3 4]。如果执行语句 arr(8) = 6; 将会产生一个八元素数组[1 2 3 4 0 0 0 6]。不幸的是,每一次扩展数组,都要经过以下步骤:第一步,创建一个新数组。第二步,把旧数组的元素复制到新数组当中。第三步,把扩展的元素写入新数组。第四步,删除旧数组。对于大数组来说这些步骤是相当耗时的。

当一个for循环中存储了一个预先未定义的数组,在第一次循环执行的时侯,循环结构迫使matlab重复执行以上步骤。从另一方面说,如果在循环开始之前数组预先分配了数组的大小,那么复制就不需要了,执行代码的速度也将加快。下面代码片段向大家展示了在循环开始之前如何预先分配数组。

好的编程习惯

在循环执行开始之前,总是要预先分配一个数组,这样能大大增加循环运行的速度。

4.用for循环和向量计算是非常常见的。例如,下面的代码片段用for循环计算1到100之间的所有整数的平方,平方根,立方根。 for ii = 1:100 square(ii) = ii ^2; square_root(ii) = ii ^ (1/2); cube_root(ii) = ii ^ (1/3); end 下面一个代码片段是用向量计算上面的问题。 ii = 1:100; square = ii .^2; square_root = ii .^ (1/2); cube_root(ii) = ii .^ (1/3); 尽管两种算法得到了相同的结果,但两者并不同等价。因为for循环算法比向量算法慢15倍还多。这是由于matlab通过每一次循环时,每行都要翻译执行一次。也相当于matlab翻译执行了300行代码。相反,如果用向量算法,matlab只需要翻译执行4行代码。所以用向量语句它的执行速度非常快。

向量算法的缺点是需要很大的内存,因为一些间接的数组需要创建。这经常是一小点损失,所以要比for循环算法好的多。

在matlab中,用向量算法代替循环的算法的过程称之为向量化(vectorization)。向量化能够改进许多的matlab程序。

10

好的编程习惯

那种既可以用向量可以解决的问题,也可以用循环解决的问题,最好用向量解决,这是因为向量执行的速度快。

例4.5

比较向量算法和循环为了比较循环和向量算法执行若无事所用的时间,用两种方法编程并测试三个运算所花的时间。

1.用for循环计算1到10000的之间每一个整数的平方,而事先不初始化平方数组。 2.用for循环计算1到10000的之间每一个整数的平方,而事先初始化平方数组。 3.用向量算法计算计算1到10000的之间每一个整数的平方。 答案:

这个程序必须用上面提供的三种方示计算出1到10000之间的每一个整数的平方,并测试每一个种算法的时间。测试时间要用到matlab函数tic和toc。tic函数复位内建计时器,而toc函数则从最后一次调用tic以秒开始计时。

因为在许多的计算机中它的时间钟是相当粗略的,所以有必要多运行几次以获得相应的平均数。

下面就是用三种方法编出的matlab程序。 % Script file: timings.m % % Purpose: % This program calculates the time required to % calculate the squares of all integers from 1 to % 10,000 in three different ways: % 1. Using a for loop with an uninitialized output % array. % 2. Using a for loop with an preallocated output % array. % 3. Using vectors. % % Record of revisions: % Date Programmer Description of change % ==== ========== ===================== % 12/08/97 S. J. Chapman Original code % % Define variables: % ii, jj Loop index % average1 Average time for calculation 1 % average2 Average time for calculation 2 % average3 Average time for calculation 3 % maxcount Number of times to loop calculation % square Array of squares % leap_day Extra day for leap year % month Month(mm) % year Year(yyyy) % Perform calculation with an uninitialized array % \% because it is so slow. maxcount = 1; % One repetition tic; % Start timer for jj = 1:maxcount clear square % Clear output array for ii = 1:10000 square(ii) = ii^2; % Calculate square 11

搜索更多关于: 4 第四章 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

366。 3.2001年不是闰年。它的1月1号对应的day of year是30。这个程序5次运行后的结果分别为 >> doy This program calculates the day of year given the current date. Enter current month (1-12):1 Enter current day(1-31):1 Enter current year(yyyy): 1999 The date 1/ 1/1999 is day of year 1. >> doy This program calculates the day of year given the current date. Enter current month (1-12):12 Enter current day(1-31):3

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com