Lecture 13:Matlab简介(二) 张伟平 Monday 7th December,2009
Lecture 13: Matlab {0() ‹ï² Monday 7th December, 2009
Contents 1 Programming ) 1.1Mfle...................。......... 1.1.1 Program Control Statements............. 2 1.1.2M-File Functions........·.···.····· 13 l.2 anonymous functions··+.······.·········· 18 2 Computational statistics with Matlab 21 2.1 Functions on Probability and Statistics.·..········ 21 2.1.1 Probability distribution ............... 21 2.1.2 Descriptive statistics..................23 2.1.3 Statistical plotting................... 24 21.4 Linear model...·.······· 24 2.l.5 Multivariate Statistics··.··.·····.···· 26 2.2 Monte Carlo with Matlab...·....·..··.····· 28 2.2.1 Monte Carlo Assessment of Hypothesis Testing··· 28 2.2.2 MCMC with matlab.················· 31 Previous Next First Last Back Forward 2
Contents 1 Programming 1 1.1 M file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1.1 Program Control Statements . . . . . . . . . . . . . 2 1.1.2 M-File Functions . . . . . . . . . . . . . . . . . . . . 13 1.2 anonymous functions . . . . . . . . . . . . . . . . . . . . . . 18 2 Computational statistics with Matlab 21 2.1 Functions on Probability and Statistics . . . . . . . . . . . . 21 2.1.1 Probability distribution . . . . . . . . . . . . . . . . 21 2.1.2 Descriptive statistics . . . . . . . . . . . . . . . . . . 23 2.1.3 Statistical plotting . . . . . . . . . . . . . . . . . . . 24 2.1.4 Linear model . . . . . . . . . . . . . . . . . . . . . . 24 2.1.5 Multivariate Statistics . . . . . . . . . . . . . . . . . 26 2.2 Monte Carlo with Matlab . . . . . . . . . . . . . . . . . . . 28 2.2.1 Monte Carlo Assessment of Hypothesis Testing . . . 28 2.2.2 MCMC with matlab . . . . . . . . . . . . . . . . . . 31 Previous Next First Last Back Forward 2
3 Symbolic computation with matlab 37 3.1 Creating Symbolic Variables and Expressions········ 37 3.2 Calculus..···.·.·.··..·....·..·.···39 4 Optimization 42 4.1 Unconstrained Minimization Example.....·...·.·42 4.2 Nonlinear Inequality Constrained Example..........43 Previous Next First Last Back Forward 1
3 Symbolic computation with matlab 37 3.1 Creating Symbolic Variables and Expressions . . . . . . . . 37 3.2 Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4 Optimization 42 4.1 Unconstrained Minimization Example . . . . . . . . . . . . 42 4.2 Nonlinear Inequality Constrained Example . . . . . . . . . . 43 Previous Next First Last Back Forward 1
Chapter 1 Programming Matlab有两种命令执行方式:一种就是在命令窗口进行交互式命令执行.用 户输入一个命令,Matlab解释执行一个命令.这种方式在有些场合(比如需要 循环)是不方便的:另一种执行方式就是M文件执行方式.用户将所有命令放在 一个文件里(称为M文件,以.m为扩展名),matlab执行该文件里所有命令直至 结束 1.1 M file 用matlab语言编写的程序称为M文件.它是由若千个matlab命令构成的,用以 实现某些操作或者某个算法.M文件有两类,一类称为命令文件(Script file), 类称为函数文件(Function file).它们的扩展名都为.m.区别在于 Previous Next First Last Back Forward 1
Chapter 1 Programming Matlab k¸´·-â1ê™: ò´“¥3·-Iù?1p™·-â1. ^ r—\òá·-, Matlab)º â1òá·-. ˘´ê™3k |‹('XIá ÃÇ)¥ÿêB; ,ò´â1ꙓ¥M©áâ1ê™. ^rÚ§k·-ò3 òá©áp(°èM©á, ±.mè*–¶), matlabâ1T©áp§k·-Üñ (Â. 1.1 M file ^matlabäÛ?ßS°èM©á. ߥdeZámatlab·-§, ^± ¢y, ˆä½ˆ, áé{. M©ák¸a, òa°è·-©á(Script file), òa°èºÍ©á(Function file). ßÇ*–¶—è.m. ´O3u Previous Next First Last Back Forward 1
一命令文件没有输入参数,也不返回输出参数:函数文件可以有输入参数, 也可以有返回输出参数: 命令文件可以在命令窗口里输入该文件名称,即可运行里面的命令:函 数文件则不行,须以函数调用的方式来调用此函数 命令文件对工作空间里的变量直接操作,结果也完全返回到工作空间里。 而函数文件里定义的变量为局部变量,函数执行完毕后就被清除 1.1.1 Program Control Statements 程序的控制分为下面几种: 音顺序结构 顺序结构是最简单的程序结构,程序中的语句将按照顺序依次执行,直至最 后一个语句.一般涉及到数据的输入,数据的计算,数据的输出 input输入语句语法结构 TCode Previous Next First Last Back Forward 2
➢ ·-©ávk—\ÎÍ, èÿࣗ—ÎÍ; ºÍ©áå±k—\ÎÍ, èå±kࣗ—ÎÍ. ➢ ·-©áå±3·-Iùp—\T©á¶°, =å$1p°·-; º Í ©áKÿ1, L±ºÍN^ê™5N^dºÍ. ➢ ·-©áÈÛäòmpC˛Üˆä, (Jèà£Ûäòmp. ºÍ©áp½¬C˛è¤‹C˛, ºÍâ1.“òÿ. 1.1.1 Program Control Statements ßSõõ©èe°A´µ ✿ ^S( ^S(¥Å{¸ßS(, ßS•äÈÚUÏ^Sùgâ1, ÜñÅ òáäÈ. òÑ 9Í‚—\, Í‚Oé, Í‚——. input —\äÈ ä{( ↑Code Previous Next First Last Back Forward 2
input(提示信息,选项) Code 其中提示信息为一个字符串,用于提示用户输入什么样的数据,其中可以加多 个换行符n.例如 TExample A=input('输入A矩阵:\n') Example 执行该语句后,首先在屏幕上显示'输入A矩阵',然后等用户从键盘上输入该 矩阵.nput语句不加选项时表示输入数值,而当选项为's'时,则允许用户输 入一个字符串.例如 TExample nm=input('what''s your name?','s') Previous Next First Last Back Forward 3
input(J´&E,¿ë) ↓Code Ÿ•J´&EèòáiŒG, ^uJ´^r—\üoÍ‚, Ÿ•å±\ı áÜ1Œ\n. ~X ↑Example A=input(’—\A› :\n’) ↓Example â1TäÈ, ƒk3¶4˛w´’—\A› ’, ,^rlÖ˛—\T › . input äÈ ÿ\¿ëûL´—\Íä, ¿ëè’s’û, K#N^r— \òáiŒG. ~X ↑Example nm=input(’what’’s your name?’,’s’) Previous Next First Last Back Forward 3
Example disp输出语句disp语句用于向命令窗口显示指定信息.例如 TExample A='statistics'; disp(A) A=[1,2,3;2,4,5]; disp(A) Example pause暂停语句 语法 pause #程序暂停,等待用户按任何键以继续 TCode pause(n)#暂停n秒 pause on#使得下续的pausel或pause(n)执行 pause of1#使得下续的pause或pause(m)不执行 Previous Next First Last Back Forward 4
↓Example disp ——äÈ dispäÈ^uï·-Iùw´ç½&E. ~X ↑Example A=’statistics’; disp(A) A=[1,2,3;2,4,5]; disp(A) ↓Example pause 6 äÈ ä{ ↑Code pause #ßS6 , ñ^rU?¤Ö±UY pause(n) # 6 n¶ pause on # ¶eYpause½pause(n)â1 pause off #¶eYpause½pause(n)ÿâ1 Previous Next First Last Back Forward 4
Code 章条件控制一if,switch if,else,and elseif语句 语法结构 TCode if logical_expression statements elseif logical_expression statements else statements end Code 例如 Example Previous Next First Last Back Forward 5
↓Code ✿ ^áõõ— if, switch if, else, and elseif äÈ ä{( ↑Code if logical_expression statements elseif logical_expression statements else statements end ↓Code ~X ↑Example Previous Next First Last Back Forward 5
1frem(a,2)==0 disp('a is even') b=a/2; end Example 以及 fE以ampl if n <0 If n negative,display error message. disp('Input must be positive'); elseif rem(n,2)==0 If n positive and even,divide by 2. A=n/2; else A=(n+1)/2; If n positive and odd,increment and divide. end Example switch语句 Previous Next First Last Back Forward 6
if rem(a, 2) == 0 disp(’a is even’) b = a/2; end ↓Example ±9 ↑Example if n < 0 % If n negative, display error message. disp(’Input must be positive’); elseif rem(n,2) == 0 % If n positive and even, divide by 2. A = n/2; else A = (n+1)/2; % If n positive and odd, increment and divide. end ↓Example switch äÈ Previous Next First Last Back Forward 6
switch语句根据表达式值的不同执行不同的语句.语法结构 switch expression (scalar or string) case valuel statements Executes if expression is valuel case value2 statements X Executes if expression is value2 otherwise statements Executes if expression does not match any case end Code 例如 Example switch var Previous Next First Last Back Forward 7
switch äÈä‚Là™äÿ”â1ÿ”äÈ. ä{( ↑Code switch expression (scalar or string) case value1 statements % Executes if expression is value1 case value2 statements % Executes if expression is value2 . . . otherwise statements % Executes if expression does not % match any case end ↓Code ~X ↑Example switch var Previous Next First Last Back Forward 7