ImageVerifierCode 换一换
格式:DOCX , 页数:25 ,大小:39.55KB ,
资源ID:5364128      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5364128.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Matlab程序设计基础.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Matlab程序设计基础.docx

1、Matlab程序设计基础数据类型取值范围转换函数Signed 8-bit integer-27 to 27-1int8Signed 16-bit integer-215 to 215-1int16Signed 32-bit integer-231 to 231-1int32Signed 64-bit integer-263 to 263-1int64Unsigned 8-bit integer0 to 28-1uint8Unsigned 16-bit integer0 to 216-1uint16Unsigned 32-bit integer0 to 232-1uint32Unsigned 6

2、4-bit integer0 to 264-1uint64表1.整型数据取值范围Matlab 默认变量常量名常量值i,j虚数单位,定义为pi圆周率eps浮点运算的相对精度realmin最小的浮点数realmax最大的浮点数Inf无穷大NaNNot-a-Number,表示不确定值2.2.21,设定字符串用表2 字符串常用函数及其功能表函数功能函数功能size查看字符数组的维数abs查找一个字符的ASSII码char把数字按照ASCII码转化为字符串strcat字符串连接strcat(水平连接)strvcat(垂直连接)strcmp比较字符串strrep替换字符串strcmpi忽略大小写比较字符串

3、upper转换为大写strncmp比较字符串的前n个字符lower转换为小写findstr在一个字符串中查找另一个字符串strtok返回字符串中第一个分隔符(空格,回车和Tab键)前的部分strjust对齐字符数组,包括左对齐,右对齐和居中blanks产生空字符串stematch查找匹配的字符串deblank删除字符串的空格MATLAB 程序基本语句程序分支控制语言1. if/else语句if语句的语法形式如下所示: if expression statements1else expression statements2end当 expression的结果为真时,执行statement1中的语

4、句,否则执行statement2中的语句。当有多个选择时,可以使用if/elseif语句。if/elseif语句的语法如下所示: if expression statements1;elseif expressionstatements2;else expression statements3;end例如:function math(x)if x0 y=x3else y=3*x2end2. switch/caseswitch/case语句的语法形式如下所示:switch switch-expressioncase case-expression1,statements1;case case-e

5、xpression2,statements2;case case-expression3,statements3;otherwisestatements;end例如:%文件名为 cj.mfunction result=cj(x)%将成绩除以10后取整n=fix(x/10);switch n case 8,9,10 disp(优秀) case 7 disp(良好) case 6 disp(及格) otherwise disp(不及格)end程序循环控制语句使用循环控制语句,可以重复执行代码块。用for语句执行指定次数;while语句适合于循环一直执行,直到满足条件为止;continue和brea

6、k语句对退出循环给予更多的控制。1. for循环for循环允许一组命令以固定的和预定的次数重复执行,for循环的一般形式如下:for index=expression(循环变量=初值:步长:终值)statements;end例如:使用for循环计算1+2+3+100的值。程序如下:sum=0;for i=1:1:100sum=sum+i;endsum2. while循环while循环以不定的次数来求一组命令值。while循环的一般形式如下: while expression statements;end例如:1.输入项数n,求自然数的前n项和;n=input(n=) sum=0;k=1; wh

7、ile k=nsum=sum+k;k=k+1;endsum2.求不超过1000的偶数之和与奇数之和。 oushuhe=0;jishuhe=0;i=1;while ia(j) a(i)=a(i)+a(j); a(j)=a(i)-a(j); a(i)=a(i)-a(j); endendenddisp(a);3. continue 语句continue命令经常与for或while语句一起使用,作用是结束本次循环,即跳过循环体中尚未执行的语句,接着下一次是否执行循环的判断。4. break语句break命令经常与for或while语句一起使用,作用是终止本次循环,跳出内层循环。程序终止控制语句 一般程

8、序代码都是按照执行完后正常退出的,但是,当遇到某些特殊情况,需要程序立即退出时,可以使用return语句提前终止程序运行。Return语句常用在MATLAB函数式M文件中。例如:return语句使用实例。n=-2;%n小于0时退出程序if n0disp(negtive number!)return;enddisp(codon number)程序异常处理语句MATLAB中提供了一种可以进行错误处理的trycatch语句,其语法格式如下:try 表达式1,表达式2,.,表达式ncatch 表达式1,表达式2,.,表达式nend当try与catch之间的所有表达式在执行时没有错误发生时,则不执行ca

9、tch与end之间的语句;反之则执行。也可以用lasterr检查最后发生的错误信息。A=1 2 3;4 5 6B=4 7;8 6try A,A+A,A+B,A*Bcatch disp(there is error)end disp(lasterr)MATLAB函数 help elfun Elementary math functions.(小学数学函数) Trigonometric.(三角) sin -正弦sind -正弦值参数以度为单位 sinh - 双曲正弦值 asin - 反正弦. asind - 正弦反以度为单位的结果asinh -反双曲正弦值cos - 余弦 cosd - 余弦值的参

10、数以度为单位 cosh - 双曲余弦值 acos - 反余弦值.acosd - 余弦反以度为单位的结果 acosh - 双曲余弦值 tan - 正切. tand - 参数以度为单位. tanh - 双曲正切值. atan - 反切线atand - 切线反以度为单位的结果atan2 - 四象限反正切值atanh -反双曲正切值. sec - 正割secd - 正割的参数以度为单位sech - 双曲正割. asec -反正割。 asecd - 结果以度为单位的反割技术。 asech - 反双曲正割。csc - 余割。 cscd - 余割的参数以度为单位。 csch - 双曲余割 acsc - 反余

11、割。 acscd -反余割以度为单位的结果acsch -反双曲余割。cot - 余切。 cotd -余切的参数以度为单位。 coth - 双曲线余切 . acot - 反余切。 acotd -反余切以度为单位的结果 acoth -反双曲余切。 hypot -平方和的平方根。 Exponential.(指数) exp - 指数. expm1 - 准确计算exp(x)-1 log - 自然对数 . log1p - 准确计算 log(1+x). log10 - 对数共同 (基地 10) log2 - 基 2 对数和解剖浮点数. pow2 - 基地2力量和标度浮动小数点数字 .realpow - 电源

12、上复杂的结果,将错误。reallog - 真正的numbero自然对数 . realsqrt - 大于或等于零的平方根. sqrt -平方根. nthroot - 实数的真正的 n 次根. nextpow2 - 其次更大的功率的2。 Complex.( 复杂 ) abs - 绝对值 . angle -相角.complex - 修建从真正和虚构部分的复杂数据。 conj -共轭复数。 imag - 复杂的假想部分. real - 复杂的实质部分。 unwrap - 解开相角. isreal -真正的数组,则为 true。 cplxpair - 对数字为共轭复数对进行排序。 Rounding an

13、d remainder.(舍入,其余部分。) fix -圆向零。 floor - 圆往减去无限。 ceil - 轮对正无穷大。 round -舍入为最接近的整数。 mod - 模量 (分立后的签名其余部分)。 rem - 分立后的其余部分。sign -符号取值函数help specfun Specialized math functions.(专门的数学函数。) Specialized math functions. airy - Airy functions. besselj - Bessel function of the first kind. bessely - Bessel func

14、tion of the second kind. besselh - Bessel functions of the third kind (Hankel function). besseli - Modified Bessel function of the first kind. besselk - Modified Bessel function of the second kind. beta - Beta function. betainc - Incomplete beta function. betaln - Logarithm of beta function. ellipj

15、- Jacobi elliptic functions. ellipke - Complete elliptic integral. erf - Error function. erfc - Complementary error function. erfcx - Scaled complementary error function. erfinv - Inverse error function. expint - Exponential integral function. gamma - Gamma function. gammainc - Incomplete gamma func

16、tion. gammaln - Logarithm of gamma function. psi - Psi (polygamma) function. legendre - Associated Legendre function. cross - Vector cross product. dot - Vector dot product. Number theoretic functions.(数字理论作用。 ) factor - Prime factors. isprime - True for prime numbers. primes - Generate list of prim

17、e numbers. gcd - Greatest common divisor. lcm - Least common multiple. rat - Rational approximation. rats - Rational output. perms - All possible permutations. nchoosek - All combinations of N elements taken K at a time. factorial - Factorial function. Coordinate transforms.(坐标转换。) cart2sph - Transf

18、orm Cartesian to spherical coordinates. cart2pol - Transform Cartesian to polar coordinates. pol2cart - Transform polar to Cartesian coordinates. sph2cart - Transform spherical to Cartesian coordinates. hsv2rgb - Convert hue-saturation-value colors to red-green-blue. rgb2hsv - Convert red-green-blue

19、 colors to hue-saturation-value.help elmat Elementary matrices and matrix manipulation.(初等矩阵和矩阵操作。) Elementary matrices.(初等矩阵。) zeros - Zeros array. ones - Ones array. eye - Identity matrix. repmat - Replicate and tile array. linspace - Linearly spaced vector. logspace - Logarithmically spaced vecto

20、r. freqspace - Frequency spacing for frequency response. meshgrid - X and Y arrays for 3-D plots. accumarray - Construct an array with accumulation. : - Regularly spaced vector and index into matrix. Basic array information.(基本阵列信息。) size - Size of array. length - Length of vector. ndims - Number of

21、 dimensions. numel - Number of elements. disp - Display matrix or text. isempty - True for empty array. isequal - True if arrays are numerically equal. isequalwithequalnans - True if arrays are numerically equal. Matrix manipulation.( 矩阵操作。 ) cat - Concatenate arrays. reshape - Reshape array. diag -

22、 Diagonal matrices and diagonals of matrix. blkdiag - Block diagonal concatenation. tril - Extract lower triangular part. triu - Extract upper triangular part. fliplr - Flip matrix in left/right direction. flipud - Flip matrix in up/down direction. flipdim - Flip matrix along specified dimension. ro

23、t90 - Rotate matrix 90 degrees. : - Regularly spaced vector and index into matrix. find - Find indices of nonzero elements. end - Last index. sub2ind - Linear index from multiple subscripts. ind2sub - Multiple subscripts from linear index. bsxfun - Binary singleton expansion function. Multi-dimensio

24、nal array functions.(多维数组的函数。) ndgrid - Generate arrays for N-D functions and interpolation. permute - Permute array dimensions. ipermute - Inverse permute array dimensions. shiftdim - Shift dimensions. circshift - Shift array circularly. squeeze - Remove singleton dimensions. Array utility function

25、s.(数组实用程序函数。) isscalar - True for scalar. isvector - True for vector. Special variables and constants.(特殊变量和常数。) ans - Most recent answer. eps - Floating point relative accuracy. realmax - Largest positive floating point number. realmin - Smallest positive floating point number. pi - 3.1415926535897

26、. i - Imaginary unit. inf - Infinity. nan - Not-a-Number. isnan - True for Not-a-Number. isinf - True for infinite elements. isfinite - True for finite elements. j - Imaginary unit. why - Succinct answer. Specialized matrices.(专门的矩阵。) compan - Companion matrix. gallery - Higham test matrices. hadama

27、rd - Hadamard matrix. hankel - Hankel matrix. hilb - Hilbert matrix. invhilb - Inverse Hilbert matrix. magic - Magic square. pascal - Pascal matrix. rosser - Classic symmetric eigenvalue test problem. toeplitz - Toeplitz matrix. vander - Vandermonde matrix. wilkinson - Wilkinsons eigenvalue test mat

28、rix.子函数 例如: 子函数的函数实例:functionavg,med=calculate(u);%主函数newstats调用两个子函数求输入矢量的平均值和中值dui%获得参数长度n=length(u);%调用子函数averageavg=average(u,n);%调用子函数medianmed=median(u,n)%定义函数average,计算平均值function a=average(v,n);a=sum(v)/n;%定义函数median,计算中值function m=median(v,n);%对矢量进行排序sort(x): 对向量x的元素进行排序(sorting) rem 相除后求余

29、w=sort(v);if rem(n,2)=1 m=w(n+1)/2);else m=(w(n/2)+w(n/2+1)/2;end 嵌套函数在一个函数内部,可以定义一个或多个函数,这种定义在其他函数内部的函数被称为嵌套函数。嵌套可以多层发生,即一个函数内部可以嵌套多个函数,在嵌套函数内部又可以再嵌套其它函数。嵌套函数的语法形式如下:function a=A(m1,m2) function b=B(m3)end end基本绘图方法二维图形函数与调用方法1. 常用绘图函数函数功能plot绘制二维图形,x轴与y轴为线性坐标semilogx绘制二维图形,x轴为对数坐标,y轴为线性坐标smilogy绘制二维图形,y轴为对数坐标,x轴为线性坐标loglog绘制二维图形,x轴,y轴均为对数坐标plotyy 绘制二维图形,在图形的左右各有一个y轴polar绘

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1