Matlab Satellite and Wireless Communication Lab Index Matlab
Matlab 基础 —— 信号与系统补充课程 Satellite and Wireless Communication Lab
Index • • Matlab简介 使用帮助 基本的操作 矩阵运算 绘图 M文件 程序流程控制 需要注意的问题 2021/2/21 Satellite and Wireless Communication Lab 2
Matlab简介 • MATLAB具有强大的矩阵运算能力 MATLAB = Matrix Laboratory = 矩阵实验 室,顾名思义MATLAB可以使矩阵运算变 得非常简单。 2021/2/21 Satellite and Wireless Communication Lab 3
Matlab外观 • 接口是以 Java 撰写,因此第一次启动时间 较久。外观如下: 2021/2/21 Satellite and Wireless Communication Lab 8
使用帮助 (Cont. ) • 常用help命令 help matfun :矩阵函数-数值线性代数 help general:通用命令 help graphics:通用图形函数 help elfun:基本的数学函数 help elmat:基本矩阵和矩阵操作 help datafun:数据分析和傅立叶变换函数 help ops:操作符和特殊字符 2021/2/21 Satellite and Wireless Communication Lab 10
基本的操作 (Cont. ) • 数值显示格式 – Format命令 如果结果为整数,则显示没有小数;如果结果不 是整数,则输出形式有: format (short):短格式(5位定点数)99. 1253 format long:长格式(15位定点数) 99. 12345678900000 format short e:短格式e方式 9. 9123 e+001 format long e:长格式e方式 9. 912345678900000 e+001 format bank: 2位十进制(小数部分) 99. 12 format hex:十六进制格式 2021/2/21 Satellite and Wireless Communication Lab 19
基本的操作 (Cont. ) • 数值显示格式 – Format命令 如果结果为整数,则显示没有小数;如果结果不 是整数,则输出形式有: format (short):短格式(5位定点数)99. 1253 format long:长格式(15位定点数) 99. 12345678900000 format short e:短格式e方式 9. 9123 e+001 format long e:长格式e方式 9. 912345678900000 e+001 format bank: 2位十进制(小数部分) 99. 12 format hex:十六进制格式 2021/2/21 Satellite and Wireless Communication Lab 20
矩阵运算 (Cont. ) • 一些常用的特殊矩阵—举例 》eye(2, 3) ans= 100 010 》eye(2) ans= 10 01 》zeros(2, 3) ans= 000 》zeros(2) ans= 00 00 》ones(2, 3) ans= 111 2021/2/21 》ones(2) ans= 11 11 》V=[5 7 2]; A=diag(V) A= 500 070 002 如果已知A为方阵,则 V=diag(A)可以提取A的 对角元素构成向量V。 Satellite and Wireless Communication Lab 29
矩阵运算 (Cont. ) • 矩阵下标 – 注意:MATLAB中矩阵的下标从1开始,而不是从0 开始! 2021/2/21 Satellite and Wireless Communication Lab 34
矩阵运算 (Cont. ) • 了解以下几个矩阵操作函数 – – Flipud Fliplr Rot 90 flipdim 2021/2/21 Satellite and Wireless Communication Lab 38
绘图 • MATLAB提供了丰富的绘图功能 • help graph 2 d可得到所有画二维图形的命令 • help graph 3 d可得到所有画三维图形的命令 2021/2/21 Satellite and Wireless Communication Lab 39
绘图 (Cont. ) 例 t=[0: pi/20: 9*pi]; figure(1) plot(t, sin(t), 'r: *') grid on grid off figure(2) plot(t, cos(t)) grid on grid off 2021/2/21 Satellite and Wireless Communication Lab 42
绘图 (Cont. ) • 设定轴的范围 – axis([xmin xmax ymin ymax]) – axis(‘equal’):将x坐标轴和y坐标轴的单位刻度大小调 整为一样。 例 t=[0: 0. 01: 2*pi]; x=sin(t); y=cos(t); plot(x, y) axis([-1. 5 1. 5]) 2021/2/21 Satellite and Wireless Communication Lab 43
绘图 (Cont. ) • subplot(m, n, k):分割图形显示窗口 – m: 上下分割个数,n: 左右分割个数,k: 子图编号 例 t=[0: pi/20: 5*pi]; figure(1) subplot(121) plot(t, sin(t)) grid on subplot(122) plot(t, -sin(t)) grid on 2021/2/21 Satellite and Wireless Communication Lab 44
绘图 (Cont. ) • 可用于数值统计分析或离散数据处理 – bax(x, y); – hist(y, x); – stairs(x, y); – stem(x, y); 2021/2/21 Satellite and Wireless Communication Lab 46
绘图 (Cont. ) subplot(221) subplot(222) x=[1 2 3 4 5 6 7 8 9 10]; x 1=randn(1, 1000); y=[5 6 3 4 8 1 10 3 5 6]; y 1=-3: 0. 1: 3; bar(x, y) hist(x 1, y 1) 2021/2/21 Satellite and Wireless Communication Lab 47
绘图 (Cont. ) subplot(223) subplot(224) x 2=0: 0. 1: 10; x 3=0: 0. 1: 10; y 2=1. /(x 2. ^3 -2. *x 2+4); y 3=1. /(x 2. ^3 -2. *x 2+4); stairs(x 2, y 2) stem(x 3, y 3) 2021/2/21 Satellite and Wireless Communication Lab 48
M文件 • 在目前目录下有一个M档案“script 01. m”,可用 type 指令显示其内容: >> cd ’d: matlab. BookMATLAB程序设计:入门篇15 -M档案’ >> type script 01. m ? clear all % 清除所有变量 x = [1 4 -2 3 -1 -5]; for i = 1: length(x), if x(i)>0, fprintf('x(%g) = %g is positiven', i, x(i)); else fprintf('x(%g) = %g is negative or zeron', i, x(i)); end 2021/2/21 Satellite and Wireless Communication Lab 51
M文件 • 执行程序底稿的效应,相当直接在指令窗 口下下达 script 01. m 里的每一列指令 • 所产生的变量也都存放在 MATLAB 的基本 作空间(Base Workspace),可验证如 下: >> whos Name Size Bytes Class ? i 1 x 1 8 double array x 1 x 6 48 double array ? Grand total is 7 elements using 56 bytes 2021/2/21 Satellite and Wireless Communication Lab 52
程序流程控制之范例四 • 若要跳出 for 循环,可用 break 指令。例如,若要找出 最小的 n 值,满足 n! > 10100,可输入如下: • 范例16 -4 : break 01. m for i = 1: 1000 if prod(1: i) > 1 e 100 fprintf('%g! = %e > 1 e 100n', i, prod(1: i)); break; % 跳出 for 循环 end 70! = 1. 197857 e+100 > 1 e 100 2021/2/21 Satellite and Wireless Communication Lab 57
switch-case-otherwise 指令 • MATLAB 在第五版开始支持 switch-case-otherwise 的多向条件指令,其使 用语法如下: switch expression case value(1) statement(1) case value(2) statement(2) case value(n-1) statement(n-1) otherwise statement(n) end • 在上述语法中,expression 为一数值或字符串,当其值和 value(k) 相等时, MATLAB 即执行 statement(k) 并跳出 switch 指令。若 expression 不等于 value(k),k=1, 2, …, n-1,则 MATLAB 会执行 statement(n) 并跳出 switch 指令 2021/2/21 Satellite and Wireless Communication Lab 60
程序流程控制之范例六 • 如果 expression 是字符串,那么若要在 case 之后比对多个字符串,就必 需使用字符串的异值数组(Cell Arrey of Strings): • 范例16 -12 : switch 02. m month = {'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep'}; for i = 1: length(month) switch month{i} case {'Mar', 'Apr', 'May'} season = 'Spring'; case {'Jun', 'Jul', 'Aug'} season = 'Summer'; case {'Sep', 'Oct', 'Nov'} season = 'Autumn'; case {'Dec', 'Jan', 'Feb'} season = 'Winter'; end fprintf('%s is %s. n', month{i}, season); end 2021/2/21 Satellite and Wireless Communication Lab 61
程序流程控制之范例六 • 上述范例output如下: Jan is Winter. Feb is Winter. Mar is Spring. Apr is Spring. May is Spring. Jun is Summer. Jul is Summer. Aug is Summer. Sep is Autumn. • MATLAB 的 switch 指令和 C 语言的 switch 指令略有差别:在 C 语言的 switch 叙述内,每个 case 叙述需加上 break 以跳出该 switch 叙述,而在 MATLAB 则不必多此一举。 • 一般而言,switch–case–otherwise 的执行效率优于 if–then– else 2021/2/21 Satellite and Wireless Communication Lab 62
自己编写函数 • function: 增加一个新函数 • function[x, y, z]=funcname(para 1, para 2…) • 注意funcname的名字要与m文件名一致 2021/2/21 Satellite and Wireless Communication Lab 63
- Slides: 64