1、MATLAB实验报告 实验一 班级:电 信 0804 姓名:田 佳 奇 学号:1404080620 完成时间:2011-3-20 实验一 MATLAB基本操作、M文件和流程控制语句 一、实验目的 1、熟悉 Matlab 的开发环境、熟悉 Matlab 中的运算符。 2、掌握建立矩阵的方法,掌握相关命令操作。 3、理解命令M 文件和函数M 文件的区别。掌握M 文件的创建和运行。 4、掌握流程控制语句的使用,掌握程序调试的基本方法。 二、实验内容 1、计算以下表达式的值,将结果按不同格式输出。掌握format 命令的使用方法。 (1) >>
2、 y=(1.3^3)*sin(pi/3)*sqrt(26) y = 9.7017 >> format short e >> y y = 9.7017e+000 (2) >> x=[2,1+2i;-0.45,5]; y=(1/2)*log(x+sqrt(1+x^2)) y = 7.1140e-001 -2.5314e-002i 8.9680e-001 +3.6577e-001i 2.1387e-001 +9.3428e-001i 1.1541e+000 -4.4259e-003i >> format short >> y y = 0.71
3、14 - 0.0253i 0.8968 + 0.3658i 0.2139 + 0.9343i 1.1541 - 0.0044i (3) >> x=[-3.0:0.1:3.0]; y=((exp(0.3*x)-exp(-0.3*x))/2).*sin(x+0.3)+log((0.3+x)/2) y = Columns 1 through 4 0.73882 +3.1416i 0.76961 +3.1416i 0.7871 + 3.1416i 0.79127 + 3.1416i Columns 5 through 8 0.78221 +3.14
4、16i 0.76015 +3.1416i 0.72541 +3.1416i 0.6784 +3.1416i Columns 9 through 12 0.61961 +3.1416i 0.54956 +3.1416i 0.46883 +3.1416i 0.37797 +3.1416i Columns 13 through 16 0.27753 +3.1416i 0.16798 +3.1416i 0.049691+3.1416i -0.077109 +3.1416i Columns 17 through 20 -0.21243+3
5、1416i -0.35659+3.1416i -0.51038+3.1416i -0.67524+3.1416i Columns 21 through 24 -0.85364+3.1416i -1.0497+3.1416i -1.2701+3.1416i -1.5271+3.1416i Columns 25 through 28 -1.8436 +3.1416i -2.2727+3.1416i -2.9837 + 3.1416i -37.024 Columns 29 through 32 -3.0017
6、 -2.3085 -1.8971 -1.5978 Columns 33 through 36 -1.3575 -1.1531 -0.97233 -0.80828 Columns 37 through 40 -0.65675 -0.51514 -0.38189 -0.25611 Columns 41 through 44 -0.13736 -0.025542 0.
7、079223 0.17665 Columns 45 through 48 0.26633 0.34781 0.42057 0.48411 Columns 49 through 52 0.53791 0.58152 0.61452 0.63658 Columns 53 through 56 0.64744 0.64696 0.6351 0.61194 C
8、olumns 57 through 60 0.57768 0.53269 0.47744 0.41256 Column 61 0.33885 2、已知 求下列表达式的值: (1) A+6*B和A-B+I; >> a=[12,34,-4;34,7,87;3,65,7]; >> b=[1,3,-1;2,0,3;3,-2,7]; >> a+6*b ans = 18 52 -10 46 7 105 21 53 49 >> I
9、ones(3); >> a-b+I ans = 12 32 -2 33 8 85 1 68 1 (2)A*B和A.*B >> a*b ans = 68 44 62 309 -72 596 154 -5 241 >> a.*b ans = 12 102 4 68 0 261 9 -130 49 (3)A^3和A.^3 >> a^3 ans = 37226 23
10、3824 48604 247370 149188 600766 78688 454142 118820 >> a.^3 ans = 1728 39304 -64 39304 343 658503 27 274625 343 (4)A/B 和B\A >> a\b ans = -0.031311 0.30287 -0.33243 0.
11、044188 -0.032281 0.10625 0.031669 -0.11577 0.15585 >> a/b ans = 16.4 -13.6 7.6 35.8 -76.2 50.2 67 -134 68 (5)[A,B]和[A([1,3],:);B^2] >> [a,b] ans = 12 34 -4 1 3 -1 34
12、 7 87 2 0 3 3 65 7 3 -2 7 >> [a([1,3],:);b^2] ans = 12 34 -4 3 65 7 4 5 1 11 0 19 20 -5 40 3、已知 (1)当时,求y的值; 创建函数的M文件:f31.m 如下: function a=f(n) a=n+10*log(n^2+5) 命令窗口调用: >> y=f31(40)/(f31(30
13、)+f31(20)) y = 0.63895 (2)当时,求y的值; 创建函数的M文件:f32.m 如下: function b=f(n) b=0; while n>=1 b=b+n*(n+1); n=n-1; end 命令窗口调用: >> y=f32(40)/(f32(30)+f32(20)) y = 1.7662 4、请分别用if 和switch 语句实现。 输入一个百分制成绩,要求输出成绩等级A、B、C、D、E。其中90 分~100 分为A,80 分~89 分为B,70 分~79 分为C,60 分~69 分为D,
14、60 分以下为E。 (1) if语句实现: disp('conversion under if statement'); n=input('please enter a percentile scores:'); if n>89 disp('A'); elseif n>79 disp('B'); elseif n>69 disp('C'); elseif n>59 disp('D'); else disp('E'); end (2) switch语句实现: disp('conversion under switch sta
15、tement'); n=input('please enter a percentile scores:'); a=fix(n/10); switch a case 10 disp('A'); case 9 disp('A'); case 8 disp('B'); case 7 disp('C'); case 6 disp('D'); otherwise disp('E'); end 5、已知: 求中: (1)
16、 这100个数中的最大值、最小值,这100个数的总和;
(2) 统计正数、零、负数的个数;
创建M文件:f5.m 代码如下:
sum=2;
f1=1;
f2=0;
f3=1;
max=1;
min=0;
n=4;
positive=2;
negative=0;
zero=1;
while n<=100
f=f3-2*f2+f1;
sum=sum+f;
if f>max
max=f;
elseif f
17、tive=positive+1; elseif f<0 negative=negative+1; else zero=zero+1; end f1=f2; f2=f3; f3=f; n=n+1; end fprintf('the max=%f\nthe min=%f\n',max,min); fprintf('the sum=%f\n',sum); fprintf('the number of positive:%f\n',positive); fprintf('the number of negative:%f\n',negative); fprintf('the number of zero:%f\n',zero); 在命令窗口中进行调用: >> f5 the max=437763282635.000000 the min=-899412113528.000000 the sum=-742745601951.000000 the number of positive:49.000000 the number of negative:49.000000 the number of zero:2.000000






