Matlab基础命令

 

一、MATLAB的启动

 

二、MATLAB的退出

(1)方式1:通过菜单栏

(2)方式2:在MATLAB当前工作窗口输入如下命令:

>> exit

 

三、MATLAB的帮助系统

 

四、矩阵的建立

(1)方式1

>> A=[12 34 56;46 7 46;54 2 68]

 

A =

 

    12    34    56

    46     7    46

    54     2    68

 

 

(2)方式2

>> B=[34,16, 6;51,7,46; 4,2,8]

 

B =

 

    34    16     6

    51     7    46

     4     2     8

(3)方式3

>> C=[12 34 56

      46 71 46

      54 22 68]

 

C =

 

    12    34    56

    46    71    46

    54    22    68

 

五、矩阵的拆分

>> A=[12 34 56;46 7 46;54 2 68];

>> A(2,1)

 

ans =

 

46

 

 

>> A(8)

 

ans =

 

46

 

 

>> A(2:3,1:3)

 

ans =

 

    46     7    46

54     2    68

 

>> A(1:3,3)

 

ans =

 

    56

    46

68

 

 

>> A(2:3)

 

ans =

 

46    54

 

 

>> A(:,3)

 

ans =

 

    56

    46

    68

 

六、特殊矩阵

>> ones(2)

 

ans =

 

     1     1

     1     1

 

>> ones(2,3)

 

ans =

 

     1     1     1

     1     1     1

 

>> eye(2)

 

ans =

 

     1     0

     0     1

 

>> eye(2,3)

 

ans =

 

     1     0     0

     0     1     0

 

>> zeros(2)

 

ans =

 

     0     0

     0     0

 

>> zeros(2,3)

 

ans =

 

     0     0     0

     0     0     0

 

>> magic(3)  %魔方矩阵

 

ans =

 

     8     1     6

     3     5     7

     4     9     2

 

 

>> rand(3)  %随机矩阵

 

ans =

 

    0.1538    0.4886    0.9254

    0.9618    0.4071    0.0056

0.8763    0.1266    0.1864

 

 

>> hilb(3)  %希尔布特矩阵

 

ans =

 

1         1/2         1/3

1/2         1/3         1/4

1/3         1/4         1/5

  

 

>> vander(1:3)  %范德蒙矩阵

 

ans =

 

     1     1     1

     4     2     1

     9     3     1

 

 

 

七、矩阵的数学运算

(1)原矩阵

>> A=[12,34,3;6,43,2;55,3,92]

 

A =

 

    12    34     3

     6    43     2

55    3     92

 

 

(2)矩阵的转置

>> A'

 

ans =

 

    12     6    55

    34    43     3

     3     2    92

 

 

(3)矩阵的逆

>> A^-1

 

ans =

 

    0.1559   -0.1231   -0.0024

   -0.0174    0.0371   -0.0002

   -0.0927    0.0724    0.0123

 

 

>> inv(A)

 

ans =

 

    0.1559   -0.1231   -0.0024

   -0.0174    0.0371   -0.0002

   -0.0927    0.0724    0.0123

 

 

 

 

 

 

>> A+B

 

ans =

 

    35    37    48

    12   121     8

56     6   120

 

 

>> A*B

 

ans =

 

      483        2697         828

      398        3378         584

     1375         675        5069

 

 

>> A/B

 

ans =

 

    0.4354    0.4456   -0.6881

    0.1267    0.5561   -0.2514

    2.4259   -0.0315   -0.6063

 

 

>> A\B

 

ans =

 

    2.8453   -9.1435    6.2109

   -0.1791    2.8383   -0.5694

   -1.6843    5.4063   -3.3901

 

 

>> A.*B

 

ans =

 

      276         102         135

       36        3354          12

       55           9        2576

 

(4)矩阵的行列式

>> det(A)

 

ans =

 

   2.5331e+04

 

 

(5)矩阵的秩

>> rank(A)

 

ans =

 

     3

 

 

(6)矩阵的特征值、特征向量

>> [V,D] = eig(A)

 

V =

 

   -0.0542   -0.8399   -0.5090

   -0.0445    0.1068   -0.5676

   -0.9975    0.5321    0.6471

 

D =

 

   95.1229         0         0

         0    5.7764         0

         0         0   46.1007

 

 

(7)矩阵间的运算

>> A=[12,34,3;6,43,2;55,3,92];

>> B=[23,3,45;6,78,6;1,3,28];

>> A-B

 

ans =

 

    -11   31   -42

     0   -35    -4

54    0    64

 

>> A./B

 

ans =

 

    0.5217   11.3333    0.0667

    1.0000    0.5513    0.3333

   55.0000    1.0000    3.2857

 

 

>> A.^-1

 

ans =

 

    0.0833    0.0294    0.3333

    0.1667    0.0233    0.5000

0.0182    0.3333    0.0109

 

 

(8)线性方程组求解

>> A=[2 -1 3;4 2 5;2 1 2]

 

A =

 

     2    -1     3

     4     2     5

     2     1     2

 

>> b=[1;4;5]

 

b =

 

     1

     4

     5

 

>> x=A\b

 

x =

 

     9

    -1

    -6

 

八、结构体

>>date=struct('year','2019年','month','4月','day','23日')

 

date =

 

  包含以下字段的 struct:

 

     year: '2019年'

   month: '4月'

     day: '23日'

 

>> date.year

 

ans =

 

'2019年'

 

>> date.month

 

ans =

 

    '4月'

 

>> date.day

 

ans =

 

    '23日'

>>Information=struct('name','张三','class','信息171','num','201711010101')

 

Information =

 

  包含以下字段的 struct:

 

     name: '张三'

     class: '信息171'

      num: '201711010101'

 

>> Information.name

 

ans =

 

    '张三'

 

>> Information.class

 

ans =

 

    '信息171'

 

>> Information.num

 

ans =

 

    '201711010101'

九、元胞数组

>> a=100;

>> b='cmg';

>> c=12.5;

>> d=[1 2 3;4 5 6];

>> e={a,b;c,d}

 

e =

 

  2×2 cell 数组

 

{[    100]}    {'cmg'     }

{[12.5000]}    {2×3 double}

 

>> f={e,d}

 

f =

 

  1×2 cell 数组

 

{2×2 cell}    {2×3 double}

 

 

十、常用数学函数

(1)绝对值函数

>> abs(2)

 

ans =

 

     2

 

>> abs(-4)

 

ans =

 

     4

 

 

(2)开平方函数

>> sqrt(2.56)

 

ans =

 

1.6000

 

 

(3)指数函数

>> exp(2)

 

ans =

 

7.3891

 

 

(4)对数函数

>> log(2)

 

ans =

 

0.6931

 

>> log10(100)

 

ans =

 

     2

 

(6)复数相关函数

>> z=5+6j

 

z =

 

   5.0000 + 6.0000i

 

>> real(z)

 

ans =

 

     5

 

>> imag(z)

 

ans =

 

     6

 

>> angle(z)

 

ans =

 

    0.8761

 

>> conj(z)

 

ans =

 

   5.0000 - 6.0000i

 

(5)三角函数

>> sin(pi/4)

 

ans =

 

0.7071

 

 

>> cos(pi/4)

 

ans =

 

    0.7071

 

 

>> tan(pi/4)

 

ans =

 

1.0000

 

 

>> sec(pi/4)

 

ans =

 

    1.4142

 

 

>> csc(pi/4)

 

ans =

 

    1.4142

 

 

>> cot(pi/4)

 

ans =

 

    1.0000

 

 

(7) 向量相关函数

>> a=[18 22 3 666 9];

>> max(a)

 

ans =

 

   666

 

>> min(a)

 

ans =

 

     3

 

>> mean(a)

 

ans =

 

  143.6000

 

>> sum(a)

 

ans =

 

   718

 

>> sort(a)

 

ans =

 

     3     9    18    22   666

 

 

十一、画图函数应用

(1)plot函数的应用

x=0:0.1:2*pi;

plot(x,sin(x), 'r+',x,cos(x), 'Go')

x=0:0.2:2*pi;

y1=sin(x);y2=cos(x);

plot(x,y1,'r',x,y2,'g')

xlabel('t=0 to 2*pi')

ylabel('value of sin(t) and cos(t)')

title('示意图')

legend('sin(t)','cos(t)')

(2)subplot函数的应用

x=0:0.2:2*pi;

subplot(2, 2, 1);plot(x,sin(x))

subplot(2, 2, 2);plot(x,cos(x))

subplot(2, 2, 3);plot(x,sin(2*x))

subplot(2, 2, 4);plot(x,cos(2*x))

(3) mesh函数的应用

x=linspace(-2,2,25);           %在x轴[ -2,2]之间取25个点

y=linspace(-2,2,25);           %在y轴[ -2,2]之间取25个点

[xx,yy]=meshgrid(x,y);       %xx,yy是25*25的矩阵

zz=xx.*exp(-xx.^2-yy.^2);    %计算函数值,zz是25*25的矩阵

mesh(xx,yy,zz);                   %画出立体网状图

colormap('cool');            %以冷色呈现

(4)surf 函数的应用

x=linspace(-2,2,25);             %在x轴[ -2,2]之间取25个点

y=linspace(-2,2,25);                %在y轴[ -2,2]之间取25个点

[xx,yy]=meshgrid(x,y);         %xx,yy 是25*25的矩阵

zz=xx.*exp(-xx.^2-yy.^2);    %计算函数值,zz是25*25的矩阵

surf(xx,yy,zz);                  %画出三维曲面图

colormap('hot');                       %以暖色呈现

(5)plot3函数的应用

t=linspace(0,20*pi,501);   %在0到20*pi之间取501个点

plot3(t.*sin(t),t.*cos(t),t);           %画tsin(t),tcos(t),t的曲线

[x,y]=meshgrid(-2:0.1:2);               %取网格点

z=y.*exp(-x.^2-y.^2);                  %计算函数值

plot3(x,y,z);                           %画三维曲线图

(6)fill函数的应用

t=0:0.02:4*pi;

y=sin(t).*exp(-t/5);

fill(t, y, 'b')          

t=0:0.02:4*pi;

y=sin(t).*exp(-t./5);

fill(t, y, 'b');hold on;    

stem(t, y, 'g');hold off;   

(7)向量场图

[x, y, z]=peaks(20);

[u, v]= gradient(z);

contour(x, y, z, 10);

colormap([1 0 0]); 

hold on;

quiver(x, y, u, v);

axis image

(8)长条图

x=[2 3 4 5 7;1 2 3 2 1];

bar(x);

 

x=[2 3 4 5 7;1 2 3 2 1];

bar3(x,'group');

 

 

(9)扇形图

x=[2 3 4 5];

explode=[ 1 1 0 0];

pie(x, explode)

x=[2 3 4 5];

label={'春','夏','秋','冬'};

pie3(x, label);

 

十二、字符串与数值的转换

(1)将数值转为字符串

>> int2str(12345)

 

ans =

 

    '12345'

 

 

>> num2str(pi)

 

ans =

 

'3.1416'

 

 (1)将字符串转为数值

>> str2num('12345')

 

ans =

 

       12345

 

十三、编写求三位整数中是水仙花数的程序,所谓水仙花数是指一个3位数,其各位数字的立方之和等于该数本身,例如153是一个水仙花数,13+53+33=153。

(1)程序代码

function ShuiXian() 

for i=100:999

    n1=fix(i/100);

    n2=fix((i-n1*100)/10);

    n3=i-n1*100-n2*10;

    if (n1^3+n2^3+n3^3)==i

        disp(i)

    end

end

 

(2)运行结果

>> ShuiXian

   153

 

   370

 

   371

 

   407

十四、编写求三位整数中是完数的程序,一个数如果恰好等于它的因子之和,这个数就称为完数。

(1)程序代码

function Wanshu() 

for s = 100:999

    y=0;

    for i = 1:s/2

        if mod(s,i) == 0

            y = y + i;

        end

    end

    if y == s

        disp(s)

    end

end

 

(2)运行结果

>> Wanshu

   496

 

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Matlab中,有许多基础命令可以使用。其中一些基础命令及其功能描述如下: - builtin:执行Matlab内建的函数。 - global:定义全局变量。 - eval:执行Matlab语句构成的字符串。 - nargchk:函数输入输出参数个数检验。 - feval:执行字符串指定的文件。 - script:Matlab语句及文件信息。 - function:Matlab函数定义关键词。 另外,还有一些其他的基础命令,包括: - matlabrc:启动主程序。 - quit:退出Matlab环境。 - startup:Matlab自启动程序。 还有一些与搜索路径、帮助文档、版本信息、错误信息等相关的命令,包括: - addpath:增加一条搜索路径。 - rmpath:删除一条搜索路径。 - demo:运行Matlab演示程序。 - type:列出.M文件。 - doc:装入超文本文档。 - version:显示Matlab的版本号。 - help:启动联机帮助。 - what:列出当前目录下的有关文件。 - lasterr:显示最后一条信息。 - whatsnew:显示Matlab的新特性。 - lookfor:搜索关键词的帮助。 - which:找出函数与文件所在的目录。 - path:设置或查询Matlab路径。 这些基础命令可以帮助你在Matlab中进行各种操作和任务。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Matlab 常用命令 大全](https://blog.csdn.net/qq_37407054/article/details/90744293)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值