MatLab的Galton钉板问题训练报告
- 格式:docx
- 大小:105.12 KB
- 文档页数:8
matlab实验报告总结电气工程学院自动化102班 2012年12月21日实验一 MATLAB环境的熟悉与基本运算一、实验目的1.熟悉MATLAB开发环境2.掌握矩阵、变量、表达式的各种基本运算二、实验基本知识1.熟悉MATLAB环境MATLAB桌面和命令窗口、命令历史窗口、帮助信息浏览器、工作空间浏览器、文件和搜索路径浏览器。
2.掌握MATLAB常用命令变量与运算符变量命名规则如下:变量名可以由英语字母、数字和下划线组成变量名应以英文字母开头长度不大于31个区分大小写MATLAB中设置了一些特殊的变量与常量,列于下表。
MATLAB运算符,通过下面几个表来说明MATLAB的各种常用运算符表2 MATLAB算术运算符表3 MATLAB关系运算符表4 MATLAB逻辑运算符表5 MATLAB特殊运算的一维、二维数组的寻访表6 子数组访问与赋值常用的相关指令格式的基本运算表7 两种运算指令形式和实质内涵的异同表的常用函数表8 标准数组生成函数表9 数组操作函数三、实验内容1、新建一个文件夹2、启动,将该文件夹添加到MATLAB路径管理器中。
3、保存,关闭对话框4、学习使用help命令,例如在命令窗口输入help eye,然后根据帮助说明,学习使用指令eye5、学习使用clc、clear,观察command window、command history和workspace等窗口的变化结果。
6、初步程序的编写练习,新建M-file,保存,学习使用MATLAB的基本运算符、数组寻访指令、标准数组生成函数和数组操作函数。
注意:每一次M-file的修改后,都要存盘。
练习A:help rand,然后随机生成一个2×6的数组,观察command window、command history和workspace等窗口的变化结果。
学习使用clc、clear,了解其功能和作用。
答:clc是清除命令窗体内容 clear是清除工作区间输入C=1:2:20,则C表示什么?其中i=1,2,3,?,10。
目录一、基础题 (2)二、绘图题 (3)三、句柄图形和图形用户界面制作 (9)四、Simulink仿真题 (13)五、专题实验总结 (17)六、参考文献 (17)一.基础题实验目的:1、掌握数组的创建和寻访;2、掌握MA TLAB 数组的运算;3、熟悉MA TLAB 关系操作和逻辑操作;4、掌握函数的调用。
实验要求:1、熟练掌握数组运算;2、熟悉MA TLAB 的各种数据类型及函数调用;3、初步了解M 文件及其编写、调试与运行。
实验内容:某公司投资2000万元建成一条生产线。
投产后,在时刻t 的追加成本和追加收益分别为G(t)= (百万元/年), H(t)= (百万元/年)。
试确定该生产线在合适何时停产可获最大利润?最大利润是多少?解:构造函数f(t)=H(t)-G(t)=13-t-3t 2/3=0 ;令t 1/3=x,则f(t)=-t 3-3t 2+13 可得矩阵P=[-1,-3,0,13]求最佳生产时间的源程序如下:p=[-1,-3,0,13]; x=roots(p); t=x.^3运行结果如下: t =3.6768 +21.4316i 3.6768 -21.4316i4.6465考虑到实际情况,显然两个虚数根应该舍掉。
所以将t=4.6425带入,求积分。
代码:t=4.6465; x=0:0.01:t;y=13-x-3*x.^(2/3); trapz(x,y)运行结果: ans =26.32083/225tt ++3/218t-结论:比较以上三组数据,可知最佳生产时间t=4.6465年,可获得的最大收益为 26.3208(百万元/年)。
减去20(百万元)投资,可得最终利润为6.3208(百万元)。
二.作图题实验目的:1、进一步熟悉M 文件调试过程;2、熟练掌握MA TLAB 二维、三维图形的绘制;3、掌握图形的修饰; 实验要求:1、进一步熟悉和掌握MA TLAB 的编程及调试;2、掌握二维、三维图形的绘制;3、掌握图形交互指令的使用;实验内容:1、二维绘图: (1)函数)sin(2x ey x-=,求:a. 绘制[0 ,8]区间内的图形,加注x,y 轴及图形名称;b. 最大值和最小值;c. 零值;求解:a、命令:ezplot('2*exp(-x)*sin(x)',[0,8]) %绘图title(f) , xlabel('x') ,ylabel('y') %加轴名及图名图像:b、先求最小值:从图像上可以看出,在[3,4]之间有最小值,所以命令:[xmin,fmin]=fminbnd(f,3,4)Hold onPlot(xmin,fmin)运行结果:xmin =3.9270 fmin =-0.0279增加一句标注: text(xmin,fmin,'(3.9270,-0.0279)')求最小值求最大值分析:要求最大值,即为求y=-f(x)的最小值,从图像上可以看出,在[0,2]之间有最大值命令:f=inline('-2*exp(-x)*sin(x)') ezplot(f,[0,8][xmin,fmin]=fminbnd(f,0,2) hold onplot(xmin,fmin,'*') Hold onPlot(xmin,fmin)运行结果: xmin = 0.7854 fmin = -0.6448增加标注:text(xmin,fmin,'(0.7854,-0.6448)')(如上图)综上可知:fmax= 0.6448 fmin= -0.0279c 、为了便于观察,首先增加一条直线y=0 命令:hold onezplot('0',[0,8])图像:0123456780.10.20.30.40.50.60.7x2 exp(-x) sin(x)由图像可以看出,在x=0,x=3,x=6附近有零点,所以命令及运行结果如下:fzero(f,0) %求在x=0附近的零点 ans = 0fzero(f,3) %求在x=3附近的零点 ans =3.1416fzero(f,6) %求在x=6附近的零点 ans =6.2832所以零点有三个分别为:x 1=0 x 2=3.1416 x 3=6.2832(2)在同一个画面上建立几个坐标系, 用subplot(m,n,p)命令;把一个画面分成m×n 个图形区域, p 代表当前的区域号,在每个区域中分别画一个图(函数为:;cos sin 2;cos ;sin x x u x z x y ===xxv cos sin =); 命令:>> subplot(2,2,1)>> ezplot('sin(x)',[-2*pi,2*pi]) >> subplot(2,2,2)>> ezplot('cos(x)',[-2*pi,2*pi]) >> subplot(2,2,3)>> ezplot('2*sin(x)*cos(x)',[-2*pi,2*pi]) >> subplot(2,2,4)>> ezplot('sin(x)/cos(x)',[-2*pi,2*pi])图像:2、三维绘图:(1)绘制[-8 8]区间内函数2222)sin(),(yx y x y x f ++=的三维网格曲面图,给该图加上标题;(2)绘制[-8 8]区间内单叶双曲面 125169222=-+z y x 的三维网格曲面图,给该图加上标题;(3) 求函数 f(x,y) = 3x 2+10y 2+3xy-3x +2y 在原点附近的一个极小值点和极小值;作函数 f(x,y)在|x|<2, |y|<1内的图;解:(1)命令:ezsurf('sin(sqrt(x^2+y^2))/sqrt(x^2+y^2)',[-8,8])title('f=sin(sqrt(x^2+y^2))/sqrt(x^2+y^2)')图像:(2)命令:xa=-8:0.2:8; ya=xa;[x,y]=meshgrid(xa,ya); a=3;b=4;c=5;z1=sqrt(c.^2*(x.^2/(a.^2)+y.^2/(b.^2)-1)); z2=-sqrt(c.^2*(x.^2/(a.^2)+y.^2/(b.^2)-1)); surf(x,y,real(z1)) hold on ;surf(x,y,real(z2)) shading interp图像:(3)求极值点命令:>> f=inline('3*x(1)^2+10*x(2)^2+3*x(1)*x(2)-3*x(1)+2*x(2)')f =Inline function:f(x) = 3*x(1)^2+10*x(2)^2+3*x(1)*x(2)-3*x(1)+2*x(2)>> [x,fval]=fminsearch(f,[0,0])运行结果:x = 0.5946 -0.1892fval = -1.0811所以原点附近的极小值点为(0.5946,-0.1892),极小值为:-1.0811 绘图命令:>> x=-2:0.1:2;>> y=-1:0.1:1;>> [xx,yy]=meshgrid(x,y);>> zz=3.*xx.^2+10.*yy.^2+3.*xx.*yy-3.*xx+2.*yy>> surf(xx,yy,zz)图像:三、句柄图形和图形用户界面制作实验目的:1、熟悉句柄图形体系的对象树结构;2、熟练掌握句柄图形体系的对象属性的创建、设置、查询;3、熟练掌握句柄的获取;4、熟练掌握图形用户界面(GUI)的制作;实验要求:1、会获取和显示图形对象的句柄;2、会设置菜单和子菜单;3、会设置用户控件;实验内容:利用底层绘图指令绘制一条余弦曲线。
信号与系统MATLAB第一次实验报告一、实验目的1.熟悉MATLAB软件并会简单的使用运算和简单二维图的绘制。
2.学会运用MATLAB表示常用连续时间信号的方法3.观察并熟悉一些信号的波形和特性。
4.学会运用MATLAB进行连续信号时移、反折和尺度变换。
5.学会运用MATLAB进行连续时间微分、积分运算。
6.学会运用MATLAB进行连续信号相加、相乘运算。
7.学会运用MATLAB进行连续信号的奇偶分解。
二、实验任务将实验书中的例题和解析看懂,并在MATLAB软件中练习例题,最终将作业完成。
三、实验内容1.MATLAB软件基本运算入门。
1). MATLAB软件的数值计算:算数运算向量运算:1.向量元素要用”[ ]”括起来,元素之间可用空格、逗号分隔生成行向量,用分号分隔生成列向量。
2.x=x0:step:xn.其中x0位初始值,step表示步长或者增量,xn 为结束值。
矩阵运算:1.矩阵”[ ]”括起来;矩阵每一行的各个元素必须用”,”或者空格分开;矩阵的不同行之间必须用分号”;”或者ENTER分开。
2.矩阵的加法或者减法运算是将矩阵的对应元素分别进行加法或者减法的运算。
3.常用的点运算包括”.*”、”./”、”.\”、”.^”等等。
举例:计算一个函数并绘制出在对应区间上对应的值。
2).MATLAB软件的符号运算:定义符号变量的语句格式为”syms 变量名”2.MATLAB软件简单二维图形绘制1).函数y=f(x)关于变量x的曲线绘制用语:>>plot(x,y)2).输出多个图像表顺序:例如m和n表示在一个窗口中显示m行n列个图像,p表示第p个区域,表达为subplot(mnp)或者subplot(m,n,p)3).表示输出表格横轴纵轴表达范围:axis([xmax,xmin,ymax,ymin])4).标上横轴纵轴的字母:xlabel(‘x’),ylabel(‘y’)5).命名图像就在subplot写在同一行或者在下一个subplot前:title(‘……’)6).输出:grid on举例1:举例2:3.matlab程序流程控制1).for循环:for循环变量=初值:增量:终值循环体End2).while循环结构:while 逻辑表达式循环体End3).If分支:(单分支表达式)if 逻辑表达式程序模块End(多分支结构的语法格式)if 逻辑表达式1程序模块1Else if 逻辑表达式2程序模块2…else 程序模块nEnd4).switch分支结构Switch 表达式Case 常量1程序模块1Case 常量2程序模块2……Otherwise 程序模块nEnd4.典型信号的MATLAB表示1).实指数信号:y=k*exp(a*t)举例:2).正弦信号:y=k*sin(w*t+phi)3).复指数信号:举例:4).抽样信号5).矩形脉冲信号:y=square(t,DUTY) (width默认为1)6).三角波脉冲信号:y=tripuls(t,width,skew)(skew的取值在-1~+1之间,若skew取值为0则对称)周期三角波信号或锯齿波:Y=sawtooth(t,width)5.单位阶跃信号的MATLAB表示6.信号的时移、反折和尺度变换:Xl=fliplr(x)实现信号的反折7.连续时间信号的微分和积分运算1).连续时间信号的微分运算:语句格式:d iff(function,’variable’,n)Function:需要进行求导运算的函数,variable:求导运算的独立变量,n:求导阶数2).连续时间信号的积分运算:语句格式:int(function,’variable’,a,b)Function:被积函数variable:积分变量a:积分下限b:积分上限(a&b默认是不定积分)8.信号的相加与相乘运算9.信号的奇偶分解四、小结这一次实验让我能够教熟悉的使用这个软件,并且能够输入简单的语句并输出相应的结果和波形图,也在一定程度上巩固了c语言的一些语法。
--实习报告班级:******姓名:**学号:***********指导老师:***日期:2011/11/24目录一.MATLAB简介 (3)二.上机实习题目 (3)1. 第2章第1题 (3)2. 第3章第2题 (3)3. 第5章第2题 (3)4. 第4章第1题 (4)三.题目实现过程 (4)1. 牛顿插值以及三次样条插值(第一个实验题) (4)a.牛顿插值 (4)b.三次样条插值 (4)c.牛顿插值截图 (5)d.三次样条插值截图 (6)2. 3次,4次多项式的曲线拟合 (7)a.先输入表格中对应的数据, (7)次拟合 (7)c. 3次拟合截图 (8)d. 4次拟合 (9)e. 4次拟合截图 (9)3. 高斯消去法解线性方程组 (10)a. 高斯消去法源代码 (10)b. 第一个小题 (11)c. 第二个小题 (12)4. 雅克比迭代法与SOR方法 (13)a. 雅克比迭代法的计算公式 (13)b. 超松弛迭代法的计算公式 (13)c.得到希尔矩阵 (13)d.得到b矩阵 (13)e.雅克比迭代法实现的函数 (13)迭代法实现的函数 (14)g.对于雅克比迭代法,通过执行以下代码 (15)h. 对于SOR迭代法,执行相对应代码 (15)四.心得与感想 (18)一.M ATLAB简介MATLAB是一个软件,用来进行科学计算。
在实际生活或者在科学研究中,大量的计算有时候是不可避免的,在这个时候,我们可以选择很多种方式来解决我们的问题。
但是选择一个好的软件对于我们来说有时候却是非常困难的,尤其是现在各种软件层出不穷,当然里面有好的,可是有大多数的软件却是鱼目混珠,在里面滥竽充数。
如果我们不避开这些,那么我们可能面临的是用很大的工作量来完成一个极其简单的问题,或者说在付出了时间与精力之后却完不成问题。
所以必须选择一个好的计算软件来解决问题。
MATLAB 就是这样一个软件,在你能够熟悉并且流畅运用它之后,那它将是一把能够解决很多问题的钥匙。
v1.0 可编辑可修改实验一 MATLAB 环境的熟悉与基本运算一、实验目的及要求1.熟悉MATLAB 的开发环境; 2.掌握MATLAB 的一些常用命令;3.掌握矩阵、变量、表达式的输入方法及各种基本运算。
二、实验内容1.熟悉MATLAB 的开发环境: ① MATLAB 的各种窗口:命令窗口、命令历史窗口、工作空间窗口、当前路径窗口。
②路径的设置:建立自己的文件夹,加入到MATLAB 路径中,并保存。
设置当前路径,以方便文件管理。
2.学习使用clc 、clear ,了解其功能和作用。
3.矩阵运算:已知:A=[1 2;3 4]; B=[5 5;7 8]; 求:A*B 、A.*B ,并比较结果。
4.使用冒号选出指定元素:已知:A=[1 2 3;4 5 6;7 8 9]; 求:A 中第3列前2个元素;A 中所有列第2,3行的元素; 5.在MATLAB 的命令窗口计算: 1) )2sin(π2) 5.4)4.05589(÷⨯+ 6.关系及逻辑运算1)已知:a=[5:1:15]; b=[1 2 8 8 7 10 12 11 13 14 15],求: y=a==b ,并分析结果 2)已知:X=[0 1;1 0]; Y=[0 0;1 0],求: x&y+x>y ,并分析结果 7.文件操作1)将0到1000的所有整数,写入到D 盘下的文件 2)读入D 盘下的文件,并赋给变量num8.符号运算1)对表达式f=x 3-1 进行因式分解2)对表达式f=(2x 2*(x+3)-10)*t ,分别将自变量x 和t 的同类项合并 3)求3(1)xdz z +⎰三、实验报告要求完成实验内容的3、4、5、6、7、8,写出相应的程序、结果实验二 MATLAB 语言的程序设计一、实验目的1、熟悉 MATLAB 程序编辑与设计环境2、掌握各种编程语句语法规则及程序设计方法3、函数文件的编写和设计4、了解和熟悉变量传递和赋值二、实验内容1.编写程序,计算1+3+5+7+…+(2n+1)的值(用input 语句输入n 值)。
matlab实验报告总结电气工程学院自动化102班 2012年12月21日实验一 MATLAB环境的熟悉与基本运算一、实验目的1.熟悉MATLAB开发环境2.掌握矩阵、变量、表达式的各种基本运算二、实验基本知识1.熟悉MATLAB环境MATLAB桌面和命令窗口、命令历史窗口、帮助信息浏览器、工作空间浏览器、文件和搜索路径浏览器。
2.掌握MATLAB常用命令变量与运算符变量命名规则如下:变量名可以由英语字母、数字和下划线组成变量名应以英文字母开头长度不大于31个区分大小写MATLAB中设置了一些特殊的变量与常量,列于下表。
MATLAB运算符,通过下面几个表来说明MATLAB的各种常用运算符表2 MATLAB算术运算符表3 MATLAB关系运算符表4 MATLAB逻辑运算符表5 MATLAB特殊运算的一维、二维数组的寻访表6 子数组访问与赋值常用的相关指令格式的基本运算表7 两种运算指令形式和实质内涵的异同表的常用函数表8 标准数组生成函数表9 数组操作函数三、实验内容1、新建一个文件夹2、启动,将该文件夹添加到MATLAB路径管理器中。
3、保存,关闭对话框4、学习使用help命令,例如在命令窗口输入help eye,然后根据帮助说明,学习使用指令eye5、学习使用clc、clear,观察command window、command history和workspace等窗口的变化结果。
6、初步程序的编写练习,新建M-file,保存,学习使用MATLAB的基本运算符、数组寻访指令、标准数组生成函数和数组操作函数。
注意:每一次M-file的修改后,都要存盘。
练习A:help rand,然后随机生成一个2×6的数组,观察command window、command history和workspace等窗口的变化结果。
学习使用clc、clear,了解其功能和作用。
答:clc是清除命令窗体内容 clear是清除工作区间输入C=1:2:20,则C表示什么?其中i=1,2,3,?,10。
MATLAB实验报告MATLAB实验报告姓名:专业:学号:实验⼀MATLAB环境的熟悉与基本运算⼀、实验⽬的:1.熟悉MATLAB开发环境2.掌握矩阵、变量、表达式的各种基本运算⼆、实验基本知识:1.熟悉MATLAB环境:MATLAB桌⾯和命令窗⼝、命令历史窗⼝、帮助信息浏览器、⼯作空间浏览器⽂件和搜索路径浏览器。
2.掌握MATLAB常⽤命令3.MATLAB变量与运算符变量命名规则如下:(1)变量名可以由英语字母、数字和下划线组成(2)变量名应以英⽂字母开头(3)长度不⼤于31个(4)区分⼤⼩写MATLAB中设置了⼀些特殊的变量与常量,列于下表。
MATLAB运算符,通过下⾯⼏个表来说明MATLAB的各种常⽤运算符表2MATLAB算术运算符表3MATLAB关系运算符表4MATLAB逻辑运算符表5MATLAB特殊运算4.MATLAB的⼀维、⼆维数组的寻访表6⼦数组访问与赋值常⽤的相关指令格式5.MATLAB的基本运算表7两种运算指令形式和实质内涵的异同表6.MATLAB的常⽤函数表8标准数组⽣成函数表9数组操作函数三、实验内容1、学习安装MATLAB软件。
2、学习使⽤help命令,例如在命令窗⼝输⼊helpeye,然后根据帮助说明,学习使⽤指令eye(其它不会⽤的指令,依照此⽅法类推)3、学习使⽤clc、clear,观察commandwindow、commandhistory和workspace等窗⼝的变化结果。
4、初步程序的编写练习,新建M-file,保存(⾃⼰设定⽂件名,例如exerc1、exerc2、exerc3……),学习使⽤MATLAB的基本运算符、数组寻访指令、标准数组⽣成函数和数组操作函数。
注意:每⼀次M-file的修改后,都要存盘。
四、实验结果练习A:(1)helprand,然后随机⽣成⼀个2×6的数组,观察commandwindow、commandhistory和workspace等窗⼝的变化结果。
MATLAB实验报告实验⼀ MATLAB 环境的熟悉与基本运算⼀、实验⽬的及要求1.熟悉MATLAB 的开发环境;2.掌握MATLAB 的⼀些常⽤命令;3.掌握矩阵、变量、表达式的输⼊⽅法及各种基本运算。
⼆、实验内容1、熟悉MATLAB 的开发环境: ① MATLAB 的各种窗⼝:命令窗⼝、命令历史窗⼝、⼯作空间窗⼝、当前路径窗⼝。
②路径的设置:建⽴⾃⼰的⽂件夹,加⼊到MATLAB 路径中,并保存。
? 设置当前路径,以⽅便⽂件管理。
2、学习使⽤clc 、clear,了解其功能与作⽤。
3、矩阵运算:已知:A=[1 2;3 4]; B=[5 5;7 8]; 求:A*B 、A 、*B,并⽐较结果。
4、使⽤冒号选出指定元素:已知:A=[1 2 3;4 5 6;7 8 9]; 求:A 中第3列前2个元素;A 中所有列第2,3⾏的元素; 5、在MATLAB 的命令窗⼝计算: 1))2sin(π2) 5.4)4.05589(÷?+ 6、关系及逻辑运算1)已知:a=[5:1:15]; b=[1 2 8 8 7 10 12 11 13 14 15],求: y=a==b ,并分析结果 2)已知:X=[0 1;1 0]; Y=[0 0;1 0],求: x&y+x>y ,并分析结果 7、⽂件操作1)将0到1000的所有整数,写⼊到D 盘下的data 、txt ⽂件 2)读⼊D 盘下的data 、txt ⽂件,并赋给变量num 8、符号运算1)对表达式f=x 3-1 进⾏因式分解2)对表达式f=(2x 2*(x+3)-10)*t ,分别将⾃变量x 与t 的同类项合并 3)求3(1)xdz z +?三、实验报告要求完成实验内容的3、4、5、6、7、8,写出相应的程序、结果实验⼆ MATLAB 语⾔的程序设计⼀、实验⽬的1、熟悉 MATLAB 程序编辑与设计环境2、掌握各种编程语句语法规则及程序设计⽅法3、函数⽂件的编写与设计4、了解与熟悉变量传递与赋值⼆、实验内容1.编写程序,计算1+3+5+7+…+(2n+1)的值(⽤input 语句输⼊n 值)。
MATLAB实验报告(8个实验)四川师范大学MATLAB语言实验报告1系级班年月日实验名称:Intro, Expressions, Commands姓名学号指导教师成绩1ObjectiveThe objective of this lab is to familiarize you with the MATLAB program development environment and to develop your first programs in this environment.2Using MATLAB2.1Starting MATLABLogon to your computer and start MATLAB by double-clicking on the icon on the desktop or by using the Start Programs menu. MATLAB Desktop window will appear on the screen.The desktop consists of several sub-windows. The most important ones are:●Command Window (on the right side of the Desktop) is used to do calculations,enter variables and run built-in and your own functions.●Workspace (on the upper left side) consists of the set of variables (arrays) createdduring the current MATLAB session and stored in memory.●Command History (on the lower left side) logs commands entered in theCommand Window. You can use this window to view previously run statements, and copy and execute selected statements.You can switch between the Launch Pad window and the Workspace window using the menu tabs under the sub-windowon the upper left side. Similarly, you can switch between the Command History and Current Directory windows using the menu tabs under the sub-window on the lower left side.2.2Executing CommandsYou can type MATLAB commands at the command prompt “>>” on the Command Window.For example, you can type the formula cos(π/6)2sin(3π/8) as >>(cos(pi/6) ^ 2) * (sin(3 * pi/8))Try this command. After you finish typing, press enter. The command will be interpreted and the result will be displayed on the Command Window.Try the following by observing how the Workspace window changes:>> a = 2; (M ake note of the us age of “;”)>> b = 3;>> c = a ^ 4 ? b ? 5 + pi ^3You can see the variables a, b and c with their types and sizes on the Workspacewindow, and can see the commands on the Command History window.Spend a few minutes to practice defining array variables (i.e. vectors and matrices)usingthe square bracket (“[ ]”) and colon (“:”) operators, and zeros() and ones() functions.>> ar =[ 1 2 3 4 5 ];>> br =[ 1 2 3 ;4 5 6 ];>> cr = [1 : 3 : 15];Set dr to ?rst 3 elements of ar.dr=ar(1:3);Set er to second row of br.er=br(2,:);Set ar to [dr er]. Find the number of elements of ar.ar=[dr er]; length(ar)2.3 Getting HelpThere are several ways to get help on commands and functions in MATLAB. First ofall you can use the Help menu. You can also use the “?” button. Try to findinformation on the plot function from the help index. Also try to get information onthe same function using the help command (i.e. type help plot). Finally, experimentwith the lookfor command. This command looks for other commands related to agiven keyword.2.4 Some Useful CommandsTry the following commands and observe their results:Which : Version and location infoClear : Clears the workspaceClc : Clears the command windowwho, whos : Lists content of the workspace3 ExercisesPlease solve the following problems in MATLAB. Do not forget to keep a diary ofyour commands and their outputs.(1) De?ne the variables x y and z as 7.6, 5.5 and 8.1, respective ly, and evaluate:578.422.52??? ??-x y xz(2) Compute the slope of the line that passes through thepoints (1,-2) and(5,8).(3) Quiz 1.1: 5(4)1.6 Exercises: 1.1, 1.4(5)2.15 Exercises: 2.6, 2.9, 2.114Quitting MATLABTyping quit on the command window will close the program. Do not forget to send your diary file and M-file to your TA.Do not forget to delete your ?les from the hard disk of the PC you used in the lab at the end of the lab session.四川师范大学MATLAB语言实验报告2系级班年月日实验名称:Programming, Relational and Logical Expressions 姓名学号指导教师成绩1ObjectiveThe objective of this lab is to familiarize you with the MATLAB script files (M-files), subarrays, relational and logical operators.2Script FilesScript files are collections of MATLAB statements that are stored in a file. Instead of typing commands directly in the Command Window, a series of commands may be placed into a file and the entire file may be executed by typing its name in the Command Window. Such files are called script files that are also known as M-files because they have an extension of .m. When a script file is executed, the result is the same as it would be if all of the commands had been typed directly into the Command Window. All commands and script files executed in the Command Window share a common workspace, so they can all share variables in the workspace. Note that if two script files are executed successively, the second script file can use the variables created by the first script file. In this way, script files can communicate with other script files through the data left behindin the workspace. An Edit Window is used to create new M-files or to modify existing ones. The Edit Window is a programming text editor, with the features of MATLAB language highlighted in different colors. You can create a new M-file with the File/New/M-file selection and you can open an existing M-file with the File/Open selection from the desktop menu of MATLAB.(1)Create a new working directory under the current directory and change the currentdirectory to …TA?s suggest?.3SubarraysIt is possible to select and use subsets of MATLAB arrays. To select a subset of an array, just include a list of the elements to be selected in the parentheses after the array name. MATLAB has a special function named end that is used to create arraysubscripts. The end function always returns the highest value taken on by a givensubscript. It is also possible to use subarrays on the left-hand side of an assignmentstatement to change only some of the values in an array. If values are assigned to asubarray, only those values are changed but if values are assigned to an array, theentire contents of the array are replaced by the new values.(1) Define the following 5 x 5 array arr1 in MATLAB.----=2274235421209518171651413215111012844563311arr(2) Write a MATLAB statement to select a subset of arr1 and return the subarraycontaining the values as shown.=22745456311arrarr11=arr1([1,5],[2 4 5]);(3) Write two MATLAB statements to select the last row and last column of arr1,separately.arr12=arr1(5,:);或arr12=arr1(end,:); arr13=arr1(:,end);或arr13=arr1(:,5);(4) Write MATLAB statements to obtain the following array from arr1.-=2257462335432112arrarr2=arr1([1 5],:)';4 Relational and Logical OperatorsRelational and logical operators are the two types of operators that produce true/falseresults in MATLAB programs. MATLAB interprets a zero value as false and anynonzero value as true. Relational operators ( ==, =,>,>=,<,<=) are operators with twooperands that produce either a true (1) or a false (0) result, depending on the values ofthe operands. Relational operators can be used to compare a scalar value with an array.They can also be used to compare two arrays or two strings only if they have the samesize. Be careful not to confuse the equivalence relational operator ( == ) with theassignment operator ( = ). Logic operators ( &, |, xor, ~ ) are operators with one ortwo operands that yield a logical result such as 0 or 1. There are three binary logicoperators: AND (& ), OR ( |), and exclusive OR ( xor ); and oneunary operator: NOT( ~). In the hierarchy of operations, logic operators are evaluated after allarithmetic and relational operators have been evaluated. The operator is evaluatedbefore other logic operators.(1) Define the following 4 x 5 array arr4 in MATLAB.------=212343212343212543214arr(2) Write an expression using arr4 and a relational operator to produce the followingresult.=110001110011110111115arrarr5=arr4>0;(3) Write an expression using arr4 and a relational operator to produce the followingresult.=010000010000010000016arrarr6=arr4==1;(4) Write a MATLAB program which will generate an (n-1)x(n-1) matrix from agiven nxn matrix which will be equal to given matrix with first row and firstcolumn deleted.arr44=rand(5); arr444=arr35(2:end,2:end);(5) Generalize your program above so that the program should ask the row andcolumn numbers to be deleted and then generate new (n-1)x(n-1) matrix.n=input('input n:');matrixn=rand(n)delrow=input('input row numbers to be deleted:');delcolumn=input('input column numbers to be deleted:');matrixn_1=matrixn([1:delrow-1 delrow+1:end], [1:delcolumn-1 delcolumn+1:end])(6) Quiz 3.1 (P88)5 Quitting MATLABTyping quit on the command window will close the program. Do not forget to sendyour diary file and M-file to your TA.Do not forget to delete your files from the hard disk of the PC you used in the lab atthe end of the lab session.四川师范大学MATLAB 语言实验报告3系级班年月日实验名称:Branches and Loops, Logical Arrays.姓名学号指导教师成绩 1 ObjectiveThe objective of this lab is to familiarize you with the MATLAB Branches and Loops,Logical Arrays.2 ExercisesDo not forget to add sufficient documentation and proper indentation to all programsyou write.(1) Write a program that calculates follow equation with for and while loop, and writea program without loop.63263022212+++==∑= i i K% for loopk1=0;for ii=1:64k1=k1+2^(ii-1);end% while loopk2=0;n=0;while n>=0&n<64k2=k2+2^n;n=n+1;end% without loopa=0:63;b=2.^a;K3=sum(b);(2) Write a program that accepts a vector of integers as input and counts the numberof integers that are multiples of 3 in that vector. You can assume that the inputcontains only integer values. An example run of your program can be as follows:Enter a vector of integers: [ 1 3 2 8 0 5 6 ]The number of multiples of 3 is 2(3) The root mean square is a way for calculating a mean fora set of numbers. The rmsaverage of a series of numbers is given as:∑==N i i xN rmsaverage 121Write a program that will accept an arbitrary number of input values and calculatethe rmsaverage of the numbers. The program should ask the user for the numberof values to be entered. Test your program with 4 and 10 set of numbers.% The root mean square is a way for calculating a mean for a set of numbers% Initializesum_x2=0;% Get the number of points to input.n=input('Enter number of points:');% Loop to read input valuesfor ii=1:n% Read in next valuex=input('Enter value:');% Calculate square sumssum_x2=sum_x2+x^2;end% Now calculate root mean squareroot_ms=sqrt(sum_x2/n);% Tell userfprintf('The number of data points is: %d\n',n);fprintf('The root mean square of this data set is: %f\n',root_ms);(4) 3.8 exercises:3.5(5) 4.7Exercises: 4.1 4.23 Quitting MATLABTyping quit on the command window will close the program. Do not forget to sendyour M-file to your TA.Do not forget to delete your files from the hard disk of the PC you used in the lab at the end of the lab session.四川师范大学MATLAB语言实验报告4系级班年月日实验名称:MATLAB/SIMULINK package姓名学号指导教师成绩1Objective●To learn how to use MATLAB/SIMULINK package●To learn how to estimate performance parameters from time-domain data2SIMULINK BasicBasic steps(1)Click on the MATLAB button to start MATLAB.(2)Once MATLAB has started up, type simulink (SMALL LETTERS!) at theMATLAB prompt (>>) followed by a carriage return (press the return key). A SIMULINK window should appear shortly, with the following icons: Sources, Sinks, Discrete, Linear, Connections, Extras.(3)Next, go to the File menu in SIMULINK window and choose New in order tobegin building the block diagram representation of the system of interest.(4)Open one or more of the block libraries and drag the chosen blocks into the active.(5)After the blocks are placed, draw lines to connect their input and output ports bymoving the mouse over a port and drag using the left button. To make a line witha right angle in it, release the button where you want thecorner, then click on theend of the line and drag to create next segment. To add a second line that runs off of an existing line click the right mouse on an existing line and drag it.(6)Save the system by selecting Save from the File menu.(7)Open the blocks by double-clicking and change some of their internal parameters.(8)Adjust some simulation parameters by selecting Parameters from the Simulationmenu. The most common parameter to change is Stop Time that defines the length of time the simulation will run.(9)Run the simulation by selecting Start from the Simulation menu. You can stop asimulation before completing by selecting Stop from the Simulation menu. (10)View the behavior of the system by attaching Scope blocks to the variables ofinterest, or by using To Workspace blocks to send data to the MATLAB workspace where you can plot the results using standard MATLAB commands.3Exercises(1)Your TA has shown you how to observe and print signals from the scope. Try thisout by printing out the input signal, which should be a -1V to 1V square wave with frequency 0.1 Hz. Note the peak-to-peak voltage difference of this signal.Note to write key blocks parameters.(2) Write a Simulink model to calculate the following differential equation,0)1(222=+--x dt dx x dt x d μInitialized 1)0(=x ,0)0(=dt dx 。
matlab实验报告答案篇一:参考答案Matlab实验报告实验一Matlab基础知识一、实验目的:1. 熟悉启动和退出Matlab的方法。
2. 熟悉Matlab命令窗口的组成。
3. 掌握建立矩阵的方法。
4. 掌握Matlab各种表达式的书写规则以及常用函数的使用。
二、实验内容:1. 求[100,999]之间能被21整除的数的个数。
(rem)2. 建立一个字符串向量,删除其中的大写字母。
(find)3. 输入矩阵,并找出其中大于或等于5的元素。
(find)4. 不采用循环的形式求出和式i?1三、实验步骤:? 求[100,199]之间能被21整除的数的个数。
(rem)1. 开始→程序→Matlab2. 输入命令:?m=100:999;?p=rem(m,21);?q=sum(p==0)ans=43? 建立一个字符串向量,删除其中的大写字母。
(find)1. 输入命令:?263i的数值解。
(sum)?k=input( ’,’s’);Eie48458DHUEI4778?f=find(k =’A’ k =’Z’);f=9 10 11 12 13?k(f)=[ ]K=eie484584778? 输入矩阵,并找出其中大于或等于5的元素。
(find)1. 输入命令:?h=[4 8 10;3 空间。
2. 熟悉M文件与M函数的编写与应用。
3. 熟悉Matlab的控制语句。
4. 掌握if,switch,for等语句的使用。
二、实验内容:1. 根据y=1+1/3+1/5+??+1/(2n-1),编程求:y 5时最大n值以及对应的y值。
2. 编程完成,对输入的函数的百分制成绩进行等绩转换,90~100为优,80~89为良,70~79为中,60~69为及格。
2x2?10?5,并分别求3. 编写M函数文件表示函数x=12和56时的函数值。
x2?x?6;x?0且x?3y?x2?5x?6;0?x?5且x?3及x?22x?x?1;其它,并求4. 编程求分段函数输入x=[-5.0,-3.0,1.0,2.0,2.5,3.0,3.5]时的输出y。
Matlab工程实训报告
题目 Galton钉板模型
学院名称 信息科学与工程学院
专业班级
姓名
学号
日期 2013-7-4
一、 问题描述
Galton 钉板模型
Galton 钉板模型是由英国生物统计学家Galton 设计的。其原理是:
在一块板上钉有n 排钉子,设n=5,在钉子的下方有6 个格子,分别
编号为0、1、2、3、4、5,自上方扔进小球任其自由下落,在下落过
程中让小球碰到钉子时,从左边落下和从右边落下的机会相等。碰到
下一排钉子时又是如此。最后落入底板中的某一格子。
二、 训练目的
·熟悉随机函数的使用
·了解基本动画的设计
三、 训练内容
编程实现该模型的动画,输入扔球次数m。提示:注意确定钉子的位置,
将钉子的横、纵坐标保存在两个矩阵中,确定小球碰到钉子的可能性,
小球碰到钉子后往左或往右各占50%,可设定当生成的随机数<0.5 往
左,否则往右。
训练要求:
计算落入第i 个格子中的球数及概率
模拟小球堆积的情况
编制实训报告。
四、 设计思路
【Step1】:动画模拟Galton钉板试验
1) 确定钉子的位置。将钉子的横、纵坐标存储在一个矩阵中;
2) 模拟了小球从顶端随机地落入某一格子的过程。设向右的概率为p,
向左的概率为q=1-p;将[0,1]分成两段,区间[0,p]和(p,1]。利用rand[]
产生一个介于0和1之间的随机数u,如果随机数up[0,],让小球落向左
边,否则落向右边;将这一过程重复n次,并用直线连接小球落下时所经过
的点。
3) 模拟小球堆积的形状。输入扔球次数m,计算落在第i个格子的小
球数im在总球数m中所占的比例,这样当模拟结束时,就得到了频率
i
m
i
m
fin,0,1,2,...,
,用频率反映小球堆积的形状。
4) 利用movie完成动画。
5)程序代码:
clear;
clc;
clf;
m=input('请输入小球的个数:');
n=5;
y0=2;
ballnum=zeros(1,n+1); %生成1*(n+1)全零阵
p=input('请输入概率P的值(0.5最佳):'); %设置向左的概率
q=1-p;
for i=n+1:-1:1 %创建钉子的坐标
x(i,1)=0.5*(n-i+1);
y(i,1)=(n-i+1)+y0;
for j=2:i
x(i,j)=x(i,1)+(j-1)*1;
y(i,j)=y(i,1);
end
end
mm=moviein(m); %开始模拟小球下落
for i=1:m
s=rand(1,n); %产生n个随机数
xi=x(1,1);yi=y(1,1);k=1;l=1; %小球碰到第一个钉子
for j=1:n
plot(x(1:n,:),y(1:n,:),'o',x(n+1,:),y(n+1,:),'.-'),
%plot是绘制二维图形的最基本函数,它是针对向量或矩阵的列来绘制曲线的
%画钉子的位置
axis([-2 n+2 0 y0+n+1]),hold on%创建坐标系图形对象
k=k+1; %小球下落一格
if s(j)>p
l=l+0; %碰到小球左移的情况
else
l=l+1; %碰到小球右移的情况
end
xt=x(k,l);yt=y(k,l); %记录小球下落的坐标
h=plot([xi,xt],[yi,yt]);axis([-2 n+2 0 y0+n+1]) %画运
动轨迹
xi=xt;yi=yt;
end
ballnum(l)=ballnum(l)+1; %计算小球个数
ballnum1=3*ballnum./m;
bar([0:n],ballnum1),axis([-2 n+2 0 y0+n+1]) %画各格子
的频率
mm(i)=getframe; %存储动画数据
hold off
end
movie(mm,1) %播放动画一次
模拟如图:
-2-101234567
0
1
2
3
4
5
6
7
8
-2-101234567
0
1
2
3
4
5
6
7
8
该图分别为模拟100个小球和200个小球的情况,小球在碰到钉子后向左向右的
概率均为0.5.可以看出,小球落入2、3号格子的机率最大,类似成一个正态分
布。
【Step2】:用二项分布描述Galton钉板模型
1)小球自上方落下,经过n个钉子。每经过一个钉子时只有两种可
能结果:向左或向右,这是一个具有两个结果(成功和失败)的随机试验E,
将向右视为成功,其概率为p,向左视为失败,其概率为1-p。小球碰到一
个钉子下落一格,相当于进行了一次试验E,自顶端落下,碰到n个钉子,
最终落到某个格子的过程,恰好相当于将试验E重复了n次,因此一次投球
过程就是一个n重贝努利试验。n重贝努利试验的成功次数X正好就是小球
向右移动的次数,是一个随机变量,根据概率论的结果,它服从二项分布,
即X~B(n,p),其取值与模拟模型的对应关系为:
表22-2 格子编号与随机变量取值对应表
小球落入的格子编号 0 1 2 ... n-1 n
随机变量X的取值 0 1 2 … n-1 n
利用概率论知识,二项随机变量X的分布列为:
iiniinpPXiCppin()(1),0,1,2,...,
上述动画模拟中:p=0.5。
有了上面的理论分析之后,我们可以比较n次投球小球堆积的频率图和X~
B(n,p)的分布图之间的差异。
2)程序代码:
clear;
clf;
clc;
p=input('请输入p的值(0.5最佳):');
m=input('请输入小球的总数:');
n=5;
rand('seed',3);
R=binornd(n,p,1,m); %模拟服从二项分布的随机数,相当于模拟投球n次
for i=1:n+1 %开始计数
k=[];
k=find(R==(i-1));
h(i)=length(k)/m; %计算落在编号为(I-1)的格子的小球频率
end
x=[0:1:n];
subplot(1,2,1);
axis([-1 6 0 1]); %画频率图
bar(x,h);
xlabel('(1)投球小球堆积的频率图');
f=binopdf(x,n,p);
subplot(1,2,2);
axis([-1 6 0 1]);
bar(x,f);
xlabel('(2)理论分布B(5,0.5)的');
模拟如图:
01234500.050.10.150.20.250.30.35(1)投球小球堆积的频率图012345
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
(2)理论分布B(5,0.5)的分布图
01234500.050.10.150.20.250.30.35(1)投球小球堆积的频率图012345
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
(2)理论分布B(5,0.5)的分布图
以上两幅图分别模拟的是100个小球和200个小球的情况,左边的为实际模拟出
来的频率统计图,右边的为理论上小球的分布图。从图1和图2的实际情况可以
看出,小球的分布情况基本上符合动画模拟出来的结果,即2、3号格子落入小
球的概率最大。而相对于理论上的分布图,实际统计出来的结果也和理论大致一
致。
五、 小结
首次接触到工程计算软件Matlab,在一开始设计该实验的时候,感觉
不知道从何下手,但慢慢地就把Matlab的基础功能有所了解。在设计
过程中,最为困难的就是代码设计部分,因为不熟悉该语言,部分代
码不得不借助上网查找,比如step1里面的代码的那几个for循环。
还有第二个代码中的产生随机数那里。在设计完代码最初几次运行会
有问题,经过不断的调试,才走到了现在的这个程度。希望通过以后
多用Matlab这个软件,能在学习上有多帮助。