统计预测 与决策 实验报告_第1页
统计预测 与决策 实验报告_第2页
统计预测 与决策 实验报告_第3页
统计预测 与决策 实验报告_第4页
统计预测 与决策 实验报告_第5页
已阅读5页,还剩40页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验一:了解数学软件MATLAB1.启动按钮2.命令窗口>> a=1 2 3;4 5 6;7 8 9a = 1 2 3 4 5 6 7 8 9>> a=1 2 3;4 5 6;7 8 9;因此,不加分号显示数组,加分号不显示数组功能键功能,Ctrl-P重新调入上命令行,Ctrl-N重新调入下命令行,Ctrl-B光标左移一个字符 ,Ctrl-F光标右移一个字符Home光标移到行首End光标移到行尾Esc清除命令行Del删除光标处字符Backspace删除光标左边字符Ctrl-K删除至行尾3.命令历史窗口(1)将命令历史窗口中的语句复制到命令窗口中;(2)直接双击命令历史窗

2、口中的语句.答:这两种方式都能执行命令历史窗口中的语句4.工作空间窗口清除工作空间的命令是:clc清除命令窗口的命令是:clear在命令窗口中键入: t=0:pi/4:2*pi y=sin(t)答:>> t=0:pi/4:2*pit = Columns 1 through 7 0 0.7854 1.5708 2.3562 3.1416 3.9270 4.7124 Columns 8 through 9 5.4978 6.2832>> y=sin(t)y = Columns 1 through 7 0 0.7071 1.0000 0.7071 0.0000 -0.7071

3、 -1.0000 Columns 8 through 9 -0.7071 -0.0000在命令窗口中键入:who,看运行结果>> whoYour variables are:t y在命令窗口中键入:whos,看运行结果>> whos Name Size Bytes Class t 1x9 72 double array y 1x9 72 double arrayGrand total is 18 elements using 144 bytes在命令窗口中键入:whos y,看运行结果。>> whos y Name Size Bytes Class y 1x

4、9 72 double arrayGrand total is 9 elements using 72 bytes5.帮助浏览器使用帮助浏览器可以搜索和查询所有MathWorks产品的文档和演示。帮助浏览器是集成到MATLAB桌面的一个HTML查看器。分别使用Help函数和doc函数获取format函数的帮助,进而说明format函数的功能。help formatFORMAT Set output format. FORMAT with no inputs sets the ouput format to the default appropriate for the class of the

5、 variable. For float variables, the default is FORMAT SHORT. FORMAT does not affect how MATLAB computations are done. Computations on float variables, namely single or double, are done in appropriate floating point precision, no matter how those variables are displayed. Computations on integer varia

6、bles are done natively in integer. Integer variables are always displayed to the appropriate number of digits for the class, for example, 3 digits to display the INT8 range -128:127. FORMAT SHORT and LONG do not affect the display of integer variables. FORMAT may be used to switch between different

7、output display formats of all float variables as follows: FORMAT SHORT Scaled fixed point format with 5 digits. FORMAT LONG Scaled fixed point format with 15 digits for double and 7 digits for single. FORMAT SHORT E Floating point format with 5 digits. FORMAT LONG E Floating point format with 15 dig

8、its for double and 7 digits for single. FORMAT SHORT G Best of fixed or floating point format with 5 digits. FORMAT LONG G Best of fixed or floating point format with 15 digits for double and 7 digits for single. FORMAT may be used to switch between different output display formats of all numeric va

9、riables as follows: FORMAT HEX Hexadecimal format. FORMAT + The symbols +, - and blank are printed for positive, negative and zero elements. Imaginary parts are ignored. FORMAT BANK Fixed format for dollars and cents. FORMAT RAT Approximation by ratio of small integers. FORMAT may be used to affect

10、the spacing in the display of all variables as follows: FORMAT COMPACT Suppresses extra line-feeds. FORMAT LOOSE Puts the extra line-feeds back in. Example: format short, pi, single(pi) displays both double and single pi with 5 digits as 3.1416 while format long, pi, single(pi) displays pi as 3.1415

11、9265358979 and single(pi) as 3.1415927. format, intmax('uint64'), realmax shows these values as 18446744073709551615 and 1.7977e+308 while format hex, intmax('uint64'), realmax shows them as ffffffffffffffff and 7fefffffffffffff respectively. The HEX display corresponds to the intern

12、al representation of the value and is not the same as the hexadecimal notation in the C programming language. See also disp, display, isnumeric, isfloat, isinteger. Overloaded functions or methods (ones with the same name in other directories) help quantizer/format.m Reference page in Help browser d

13、oc formatdoc format 实验二:数学软件MATLAB的数据类型1. 常数在命令窗口中分别键入:Rho=(sqrt(5)-1)/2>> Rho=(sqrt(5)-1)/2Rho =0.6180A=abs(3+4i)>> A=abs(3+4i)A = 5realmax>> realmaxans = 1.7977e+308huge=exp(log(realmax)>> huge=exp(log(realmax)huge = 1.7977e+308toobig=pi*huge>> toobig=pi*hugetoobig =

14、Inf2.变量函数iskeyword的功能是:MATLAB预留了一些关键字并且不允许重载它们,如果调用过程中使用了它们,将会导致出错信息,使用不带输入变量的iskeyword函数可以列出所有预留的关键字。在命令窗口中键入命令iskeyword并记结果。>> iskeywordans = 'break' 'case' 'catch' 'continue' 'else' 'elseif' 'end' 'for' 'function' '

15、global' 'if' 'otherwise' 'persistent' 'return' 'switch' 'try' 'while'3.输入格式练习使用format命令设置变量的输出格式。答:(1)format:以5位定点格式输出变量值。(2)format long:以15位定点格式输出double型变量值(3)format +:在矩阵中,用符号+、-和空格分别表示正号、负号和零。4. 练习在MATLAB中构造一维数组的方法。>> x=1 2 3 4 5 6

16、x = 1 2 3 4 5 6(1)用增量法构建一维数组>> A=(1:0.2:2)A = 1.0000 1.2000 1.4000 1.6000 1.8000 2.0000>> B=(1:0.2:2.1)B = 1.0000 1.2000 1.4000 1.6000 1.8000 2.0000>> C=(1:5.8)C = 1 2 3 4 5(2)用linspace函数构造一维数组>> x=linspace(0,2*pi,5)x = 0 1.5708 3.1416 4.7124 6.28325. 练习在MATLAB中构造二维数组(矩阵)的方法。

17、(1)简单创建方法>> a=1 2 3 4;2 3 4 5;3 4 5 6a = 1 2 3 4 2 3 4 5 3 4 5 6(2)构造特殊数组>> A=ones(4,5)A = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1>> B=zeros(6,1)B = 0 0 0 0 0 0>> C=zeros(3,5,'uint32')C = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0>> A1=eye(4)A1 = 1 0 0 0 0 1 0 0 0 0 1 0 0 0

18、0 1>> B1=eye(4,5)B1 = 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0>> C1=eye(6,4)C1 = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0>> a=1 2 3 4 5 6a = 1 2 3 4 5 6>> A2=diag(a)A2 = 1 0 0 0 0 0 0 2 0 0 0 0 0 0 3 0 0 0 0 0 0 4 0 0 0 0 0 0 5 0 0 0 0 0 0 6>> B2=diag(a,-1)B2 = 0

19、0 0 0 0 0 0 1 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 6 0>> C2=diag(a,1)C2 = 0 1 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0>> A3=rand(4)A3 = 0.9501 0.8913 0.8214 0.9218 0.2311 0.7621 0.4447 0.7382 0.

20、6068 0.4565 0.6154 0.1763 0.4860 0.0185 0.7919 0.4057>> A3=rand(4)A3 = 0.9355 0.0579 0.1389 0.2722 0.9169 0.3529 0.2028 0.1988 0.4103 0.8132 0.1987 0.0153 0.8936 0.0099 0.6038 0.7468>> A3=rand(4)A3 = 0.4451 0.8462 0.8381 0.8318 0.9318 0.5252 0.0196 0.5028 0.4660 0.2026 0.6813 0.7095 0.41

21、86 0.6721 0.3795 0.4289每执行一次都有不同的结果,此函数创建元素服从均匀分布的随机数数组>> B3=randn(4)*20B3 = -8.6513 -22.9294 6.5458 -11.7663 -33.3117 23.8183 3.4928 43.6637 2.5066 23.7833 -3.7342 -2.7279 5.7535 -0.7527 14.5158 2.2786>> B3=randn(4)*20B3 = 21.3354 5.8882 -13.8355 -28.8193 1.1856 -26.7236 17.1599 11.4230

22、 -1.9130 14.2865 25.0800 -7.9977 -16.6470 32.4712 -31.8746 13.7999>> B3=randn(4)*20B3 = 16.3124 23.8168 -32.0817 -16.1018 14.2382 -24.0491 5.1461 10.5749 25.8050 -0.3958 -21.1295 4.3864 13.3720 -3.1343 28.3028 -18.4380(3)聚合矩阵>> A=ones(3,4)*6A = 6 6 6 6 6 6 6 6 6 6 6 6>> B=eye(3,4)B

23、 = 1 0 0 0 0 1 0 0 0 0 1 0>> C1=A B%在水平方向上聚合A和B(或用,隔开)C1 = 6 6 6 6 1 0 0 0 6 6 6 6 0 1 0 0 6 6 6 6 0 0 1 0>> C2=A;B%在垂直方向上聚合A和BC2 = 6 6 6 6 6 6 6 6 6 6 6 6 1 0 0 0 0 1 0 0 0 0 1 0>> A=1 2 3;4 5 6A = 1 2 3 4 5 6>> B=ones(2,3)B = 1 1 1 1 1 1>> C1=cat(1,A,B)%在垂直方向聚合A和BC1 =

24、 1 2 3 4 5 6 1 1 1 1 1 1>> C2=cat(2,A,B)%在水平方向上聚合A和BC2 = 1 2 3 1 1 1 4 5 6 1 1 1>> C3=cat(3,A,B)C3(:,:,1) = 1 2 3 4 5 6C3(:,:,2) = 1 1 1 1 1 1>> C4=vertcat(A,B)C4 = 1 2 3 4 5 6 1 1 1 1 1 1>> C5=horzcat(A,B)C5 = 1 2 3 1 1 1 4 5 6 1 1 1>> D=repmat(A,2,3)%将矩阵a垂直向上复制2次,水平方向

25、上复制3次。D = 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6 4 5 6 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6 4 5 6>> C=eye(3)*8C = 8 0 0 0 8 0 0 0 8>> E=blkdiag(A,B,C)%创建块对角矩阵E = 1 2 3 0 0 0 0 0 0 4 5 6 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0 0 86练习在MATLAB中如何获取矩阵

26、的元素。>> A=magic(4)%生成一个4阶幻方阵A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1>> b1=A(4,3)%获取矩阵的第4行第3列元素b1 = 15>> b2=A(12)b2 = 15>> b3=A(1,end)%获取矩阵A的第1行最后一个元素b3 = 13>> b4=A(end)%获取矩阵A的最后一行的最后一列元素b4 = 1>> b5=A(end,3)%获取矩阵A的第3列的最后一行元素b5 = 15>> Linearindex=sub2ind(size

27、(A),4,2)%获取线性索引值Linearindex = 8>> row,col=ind2sub(size(A),12)%获取行列索引值row = 4col = 3>> B1=A(2,:)%获取矩阵A的第2行元素B1 = 5 11 10 8>> B2=A(:,3)%获取矩阵A的第2列元素B2 = 3 10 6 15>> B3=A(:)B3 = 16 5 9 4 2 11 7 14 3 10 6 15 13 8 12 1>> C1=A(2:4,:)%获取矩阵A的第2行至第4行元素C1 = 5 11 10 8 9 7 6 12 4 14

28、 15 1>> C2=A(2:4,1 2 4)%获取位于矩阵A的第24行与第1,2,4列的元素C2 = 5 11 8 9 7 12 4 14 17练习在MATLAB中如何获取与矩阵有关的信息。>> A=1 2;3 4;5 6;>> B=2 2;2 2;1 1;>> C=cat(3,A,B)C(:,:,1) = 1 2 3 4 5 6C(:,:,2) = 2 2 2 2 1 1>> M=length(C)M = 3>> n=ndims(C)n = 3>> N=numel(C)N = 12>> size

29、(C)ans = 3 2 28练习在MATLAB中如何创建字符串。通过把字符串放到单引号中来指定字符数据。>> A='China'>> whos A Name Size Bytes Class A 1x5 10 char arrayGrand total is 5 elements using 10 bytes>> ischar(A)%测试A是否为字符串数据类型ans = 19. 练习在MATLAB中如何创建二维字符串。(1)要求每行具有相同的长度>> name='LI YI''HU XU'name

30、 =LI YIHU XU(2)两个字符串长度不同时,将短的字符串后面用空格补齐,使长度相同。>> name='LIU YING''HU XU 'name =LIU YINGHU XU (3)用char函数创建字符串时,自动以最长的输入字符串的长度为标准,进行空格补齐。>> name=char('LIU YING','HU XU')name =LIU YINGHU XU(4)用deblank函数删除后面的空格>> trimname=deblank(name(2,:)trimname =HU XU1

31、0. 练习在MATLAB中数据类型的转换。(1)用char函数把字符串单元数组转化为标准字符串数组A = 'Sunjunfang' 'Newstudent' 'Beijing'>> whos A Name Size Bytes Class A 1x3 234 cell arrayGrand total is 30 elements using 234 bytes>> B=char(A)%字符串单元数组转化为标准字符串数组B =SunjunfangNewstudentBeijing >> whos B Name

32、Size Bytes Class B 3x10 60 char arrayGrand total is 30 elements using 60 bytes(2)用str2double函数把一个字符串单元数组转化为字符串表示的双精度值。>> A='3.7294e-3''-583.75''13.796'%字符串单元数组A = '3.7294e-3' '-583.75' '13.796'>> B=str2double(A)B = 0.0037 -583.7500 13.7960&

33、gt;> whos A B Name Size Bytes Class A 3x1 224 cell array B 3x1 24 double arrayGrand total is 28 elements using 248 bytes(3)用in2str函数将整型数据类型转化为字符串型数据。>> x=2010;>> y=int2str(x)y =2010>> whos x y Name Size Bytes Class x 1x1 8 double array y 1x4 8 char arrayGrand total is 5 elements

34、 using 16 bytes(4)用num2str函数对输出字符串的格式提供了更多控制,该函数的第2个变量是可选的,它设置输出字符串的位数,或指定一个实际格式。>> p=num2str(pi,6)p =3.14159>> p=num2str(pi,16)p =3.141592653589793>> whos p Name Size Bytes Class p 1x17 34 char arrayGrand total is 17 elements using 34 bytes>> p=num2str(0.00001234567890123456

35、789,6)p =1.23457e-005>> whos p Name Size Bytes Class p 1x12 24 char arrayGrand total is 12 elements using 24 bytes(5)用mat2ste函数将数组转化为字符串>> A=11 22 33;44 55 66;>> B=mat2str(A)B =11 22 33;44 55 66>> class(B)ans =char11.比较字符串在命令窗口中键入: >> str1='hello'>> str2=&

36、#39;help'>> strcmp(str1,str2)运行结果:ans = 0解释运行结果:因为str1和str2不相等,所以用strcmp函数时返回0(false)。在命令窗口中键入:strncmp(str1,str2,3)运行结果: ans = 1解释运行结果:用strncmp函数可以比较字符串的前n个字符。因为str1和str2的前三个字符相同,所以用strncmp函数时返回1(true)。在命令窗口中键入:strncmp(str1,str2,4)运行结果: ans = 0解释运行结果:用strncmp函数可以比较字符串的前n个字符。因为str1和str2的前三个

37、字符相同,但第四个字符不同,所以用strncmp函数时返回0(false)。在命令窗口中键入:>> A='hello''pen''rule'>> B='help''pen''pencilbox'>> strcmp(A,B)运行结果: ans = 0 1 0解释运行结果:对于用字符串构成的单元数组,这些函数一个单元一个单元的进行比较。因为第二个单元的字符串相同,第一个单元的字符串和第三个单元的字符串不相同,所以结果分别是0(false),1(true),0(fals

38、e)。在命令窗口中键入: strncmp(A,B,3)运行结果:ans = 1 1 0解释运行结果:对于用字符串构成的单元数组,这些函数一个单元一个单元的进行比较前3位。因为第一个单元的字符串和第二个单元的字符串前3位相同,第三个单元的字符串前3位不相同,所以运行结果分别为:1(true),1(true),0(false)。12.练习使用MATLAB的关系运算符(,=,)比较字符串。>> A='face'B='cake'>> A=Bans = 0 1 0 1>> A>=Bans = 1 1 0 1>> A&g

39、t;Bans = 1 0 0 013.练习聚合字符串字符串通常可以由更小的元素组合而成。两个通用的聚合方法是使用MATLAB聚合运算符( )和sprintf函数。>> A=;>> for n=1:6 An='a',int2str(n);end>> AA = 'a1' 'a2' 'a3' 'a4' 'a5' 'a6'>> B=;>> for m=1:8 Bm=sprintf('B%d',m);end>&g

40、t; BB = 'B1' 'B2' 'B3' 'B4' 'B5' 'B6' 'B7' 'B8'>> i=2;j=5;k=8;>> A=sprintf('i=%d,j=%d,k=%d.',i,j,k)A =i=2,j=5,k=8.用strcat函数也可以14.练习在MATLAB中如何生成多维数组。用cat函数生成多维数组>> A=cat(3,1 2;3 4,1 3;5 7)A(:,:,1) = 1 2 3 4A(:,:

41、,2) = 1 3 5 7>> B=cat(3,5 6;7 8,2 4;6 8);>> D=cat(4,A,B,cat(3,1 9;9 1,1 8;8 1)D(:,:,1,1) = 1 2 3 4D(:,:,2,1) = 1 3 5 7D(:,:,1,2) = 5 6 7 8D(:,:,2,2) = 2 4 6 8D(:,:,1,3) = 1 9 9 1D(:,:,2,3) = 1 8 8 115.练习在MATLAB中如何创建结构数组。(1)使用赋值语句>> ='Wang Meng'>> student.I

42、D=08;>> student.test=79 75 73 80;88 89 80 95student = name: 'Wang Meng' ID: 8test: 2x4 double>> student(2).name='Zhang Lei'>> student(2).ID=10;>> student(2).test=68 77 68;69 65 71;82 61 98student = 1x2 struct array with fields: name IDtest(2)使用struct函数>>

43、 A=struct('type','big','litter','color','red','x',3 4)A = 1x2 struct array with fields: type color x16.练习在结构数组中如何获取数据。>> A1=79 75 73 80;88 89 80 95;>> A2=68 77 68;69 65 71;82 61 98;>> student=struct('name','Wang Meng',

44、'Zhang Lei','ID',8,10,'test',A1,A2)student = 1x2 struct array with fields: name ID test>> B=student(1).nameB =Wang Meng>> C=student(2).IDC = 10>> D=student(2).testD = 68 77 68 69 65 71 82 61 98>> E1=E1 = 'Wang Meng' 'Zhang Lei&#

45、39;>> E2=student.IDE2 = 8 10>> E3=student(1:2).testE3 = 2x4 double 3x3 double>> f=student(2).test(:,2,3)f = 77 68 65 71 61 9817.练习使用size函数获取结构数组或任何结构字段的大小。18.练习在MATLAB中如何创建单元数组。1、通过赋值语句(1)单元索引>> A= ;>> A(1,1)=1 2 3;4 5 6;7 8 9;>> A(1,2)='Liu zhong'>> A(2,1)=3+7i;>> A(2,2)=-pi:pi/10:pi;>> AA = 3x3 double 'Liu zhong' 3.0000+ 7.0000i 1x21 double(2)内容索引>> A= ;>&g

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论