实验三 Matlab程序设计 下载本文

内容发布更新时间 : 2024/6/2 6:19:24星期一 下面是文章的全部内容请认真阅读。

实验三 Matlab程序设计

一、实验目的:掌握MATLAB程序编辑、运行及调试方法,M文件建立方法,函数文件

建立方法,程序设计方法。

二、实验内容:

1、启动MATLAB后,点击File|New|M-File,启动MATLAB的程序编辑及调试器

(Editor/Debugger),编辑以下程序,点击File|Save保存程序,注意文件名最好用英文字符。点击Debug|Run运行程序,在命令窗口查看运行结果,程序如有错误则改正。 注:数论中一个有趣的题目:任意一个正整数,若为偶数,则用2除之,若为奇数,则与3相乘再加上1。重复此过程,最终得到的结果为1。如: 2?1

3?10?5?16?8?4?2?1 6?3?10?5?16?8?4?2?1

运行下面的程序,按程序提示输入n=1,2,3,5,7等数来验证这一结论。

%classic \ while 1

n=input('Enter n,negative quits:'); if n<=0 break end a=n; while n>1

if rem(n,2)==0 n=n/2; else

n=3*n+1; end a=[a,n]; end a end

运行结果如下:

Enter n,negative quits:1 a = 1

Enter n,negative quits:2 a =

2 1

Enter n,negative quits:3 a =

3 10 5 16 8 4 2 1 Enter n,negative quits:5 a =

5 16 8 4 2 1 Enter n,negative quits:7

a =

Columns 1 through 13

7 22 11 34 17 52 26 13 40 20 10 5 16

Columns 14 through 17 8 4 2 1

2.编写如下M 文件,试调整参数a 的大小,观察并记录y1、y2 的波形特征。

a=3结果 10.80.60.40.20-0.2-0.4-0.6-0.8-10246810121410.80.60.40.20-0.2-0.4-0.6-0.8-102468101214a=5结果 然后,保存内存工作区变量a、t、y1、y2 到example1.mat 文件;关闭Matlab,再重新启动;观察内存工作区;重新根据.mat 文件恢复原来的工作区变量。在命

令窗口中查看exp 函数的帮助;运行helpwin 查看超文本格式的帮助文件,试翻译并记录下信号处理工具箱(Signal Processing Toolbox)中的函数分类(Functions -- Categorical List)。 Digital Filters 数字滤波器 Analog Filters 模拟滤波器 Linear Systems 线性系统 Windows 视窗 Transforms 变换 Cepstral Analysis 倒谱分析 Statistical Signal Processing 统计信号处理 Parametric Modeling 参数化建模 Linear Prediction 线性预测 Multirate Signal Processing 多采样率信号处理 Waveform Generation 波形生成 GUIs 图形用户接口 i2?10000的最小m值。 3、编程求满足?i?1mSpecialized Operations 特殊操作 minvalue.mat

i=1;y=2^i;

while y<=1e4 i=i+1; y=y+2^i; end %m最小值 m=i

结果mmin=13

4、编写一个函数,计算下面函数的值,给出标量x的值,调用该函数后,返回y的值。

function [y]=myfun1(x)

?sinx,?y(x)??x,??x?6,?function [y]=myfun1(x) if x<=0 y=sin(x); elseif x<=3 y=x; else y=-x+6; end

x?00?x?3x?3

选择一些数据测试你编写的函数。 Myfun1.mat

测试结果 >> myfun1(-1) ans =

-0.841470984807897 >> myfun1(2) ans = 2

>> myfun1(4) ans = 2

5、编写一个函数求向量x中元素的平均值、最大值、最小值、均方根值。

function [m_x,max_x,min_x,rms_x]=myfun2(x)方均根值(Root

Mean Square)的计算公式为: