基本运算:
一,矩阵的生成
clc ;
clear all;
close all;
1.直接输入
A = [ 1 ,2 ,3,4;2,3,4,5;3,4,5,6]
A =
1 2 3 4
2 3 4 5
3 4 5 6
1)矩阵元素可以是表达式
B = [sin(pi/2),4*8]
B =
1 32
2)矩阵元素可以是复数
C = [3+2i,2+6i,4-3i]
C =
3.0000 + 2.0000i 2.0000 + 6.0000i 4.0000 - 3.0000i
2.使用函数建立矩阵
1)使用reshpe函数
a=1:9;
b = reshape(a,3,3)
c = reshape(a,3,3)'
b =
1 4 7
2 5 8
3 6 9
c =
1 2 3
4 5 6
7 8 9
2)使用diag建立对角矩阵
A = rand(4,4)
B = diag(A)
A =
0.4299 0.3968 0.2160 0.6713
0.8878 0.8085 0.7904 0.4386
0.3912 0.7551 0.9493 0.8335
0.7691 0.3774 0.3276 0.7689
B =
0.4299
0.8085
0.9493
0.7689
C =
0.4299 0 0 0
0 0.8085 0 0
0 0 0.9493 0
0 0 0 0.7689
3.使用M文件建立矩阵
二.算术运算
1.矩阵的加减运算.
1)加减运算
A=[1,2;3,4];
B=[5,6;7,8];
x1=A+B
x2=A-B
x1 =
6 8
10 12
x2 =
-4 -4
-4 -4
2)乘法
A=[2 5 7 ;1 3 6 ;3 2 5 ];
B=[1 3 5 ; 2 4 6 ;5 6 7 ];
c1=A*B
c1 =
47 68 89
37 51 65
32 47 62
c2 =
20 24 50
26 34 68
37 57 106
3)除法
c1 =
0.7692 1.5385 2.3077
3.6923 5.7179 7.7436
-2.3077 -5.1709 -8.0342
c2 =
-9.9786 1.2222 4.9188
-11.3034 1.4444 5.9530
-12.6282 1.6667 6.9872
d1 =
0.7692 1.5385 2.3077
3.6923 5.7179 7.7436
-2.3077 -5.1709 -8.0342
d2 =
-9.9786 1.2222 4.9188
-11.3034 1.4444 5.9530
-12.6282 1.6667 6.9872
4)矩阵的乘方
result =
405 630 518
2268 3123 3022
884 1180 1125
5)点运算
A =
1 3 5
2 4 6
3 6 9
B =
9 10 7
6 9 4
2 5 8
C =
9 30 35
12 36 24
6 30 72
C1 =
0.1111 0.3000 0.7143
0.3333 0.4444 1.5000
1.5000 1.2000 1.1250
C2 =
0.1111 0.3000 0.7143
0.3333 0.4444 1.5000
1.5000 1.2000 1.1250
x =
2 2 2
y =
1 2 3
z =
2 4 8
【例1-3】当x=1,3,5,7,9时,分别求y=x^2cos(x)的值.
x=1:2:9
y=x.^2.*cos(x)
x =
1 3 5 7 9
y =
0.5403 -8.9099 7.0916 36.9412 -73.8016
三.关系运算
关系运算符有 == ~= < <= > >=
a =
1 2
3 4
b =
1 3
2 4
c1 =
0 0
1 0
c2 =
1 0
0 1
c3 =
0 1
1 0
c4 =
0 1
0 0
c5 =
1 1
0 1
c6 =
1 0
1 1
d =
1
3
4
四.逻辑运算
a =
17 26 39 3 -32
b =
23 168 58 -3 25
c1 =
1 1 1 0 0
c2 =
0 0 0 1 0
d1 =
0 0 0 0 0
1.all 若向量的所有元素非零,结果为1
resultALL = all([10 20 30 40 ])
resultALL =
1
2.any 若向量中任何一个元素非零,结果为1
resultANY = any([1 2 3 4 5 0])
resultANY =
1
3.exist 判断工作空间是否存在,存在为1,否则非零.
resultEXIST = exist('plot')
resultEXIST =
5
4.find 找出向量或矩阵非零元素的位置
dataforFIND = [2 1 0; 6 0 3 ;5 2 4 ]
resultFIND1 = find(dataforFIND)
resultFIND2 = find(dataforFIND')
dataforFIND =
2 1 0
6 0 3
5 2 4
resultFIND1 =
1
2
3
4
6
8
9
resultFIND2 =
1
2
4
6
7
8
9
order =
1 4 7
2 5 8
3 6 9
5.isempty 判断矩阵是空矩阵 如果为空,则为1,非空0;
resultISEMPTY = isempty([1 2 3 ; 4 5 6 ; 7 8 9 ])
resultISEMPTY =
0
6.isglobal 判断是否为全局变量,是为1,非为0
7.isinf 判断是否为无穷小量,是为1,否为0
8.isnan 若元素是nan,则为1,否为0
9.isfinite 若矩阵大小有限,则取1,否则则为0
10. issparse 判断是否为稀疏矩阵,是为1,否为0
11. isstr 判断变量是字符组,是为1,否则取0
五.基本函数
1.常用数学函数
1)绝对值函数abs(a)
x = [0 -1 -2 4 -4 9]
absOFx = abs(x)
x =
0 -1 -2 4 -4 9
absOFx =
0 1 2 4 4 9
2)相位角函数angle(a) 以弧度表示.
a=[3+4i,5+5i;5+12i 3]
angleOFa=angle(a)*(180/pi)
a =
3.0000 + 4.0000i 5.0000 + 5.0000i
5.0000 +12.0000i 3.0000
angleOFa =
53.1301 45.0000
67.3801 0
3)求复数的实部和虚部real(a),imag(a)
a=[7-8i 10+i;3 7+2i;12-6i 3]
realOFa = real(a)
imagOFa = imag(a)
realANDimag = real(a) + imag(a)*i
a =
7.0000 - 8.0000i 10.0000 + 1.0000i
3.0000 7.0000 + 2.0000i
12.0000 - 6.0000i 3.0000
realOFa =
7 10
3 7
12 3
imagOFa =
-8 1
0 2
-6 0
realANDimag =
7.0000 - 8.0000i 10.0000 + 1.0000i
3.0000 7.0000 + 2.0000i
12.0000 - 6.0000i 3.0000
4)求复数共轭conj(a)
a=[7-8i 10+i;3 7+2i;12-6i 3]
conjOFa = conj(a)
realANDimag = real(a) - imag(a)*j
a =
7.0000 - 8.0000i 10.0000 + 1.0000i
3.0000 7.0000 + 2.0000i
12.0000 - 6.0000i 3.0000
conjOFa =
7.0000 + 8.0000i 10.0000 - 1.0000i
3.0000 7.0000 - 2.0000i
12.0000 + 6.0000i 3.0000
realANDimag =
7.0000 + 8.0000i 10.0000 - 1.0000i
3.0000 7.0000 - 2.0000i
12.0000 + 6.0000i 3.0000
5)数组指数exp(a) 将矩阵元素作为e的幂exp(a)=e.^a
a = [1 -3 3 ;2 -1 6]
resultOFa = exp(a)
a =
1 -3 3
2 -1 6
resultOFa =
2.7183 0.0498 20.0855
7.3891 0.3679 403.4288
6)矩阵指数函数的使用expm(a)=e^a
a = [0 2 0 ;-2 0 3 ; 0 2 -1]
resultOFexpm = expm(a)
resultOFexp = exp(1)^a
a =
0 2 0
-2 0 3
0 2 -1
resultOFexpm =
-1.1709 2.2727 2.4144
-2.2727 1.2435 2.2018
-1.6096 1.4679 2.1191
resultOFexp =
-1.1709 2.2727 + 0.0000i 2.4144 + 0.0000i
-2.2727 - 0.0000i 1.2435 + 0.0000i 2.2018 + 0.0000i
-1.6096 + 0.0000i 1.4679 + 0.0000i 2.1191 + 0.0000i
7)平方根函数的使用 sqrt(a)
a=[2 5 8 9]
resultOFsqrt = sqrt(a)
a =
2 5 8 9
resultOFsqrt =
1.4142 2.2361 2.8284 3.0000
8)对数函数 自然对数log(a),log2(a),log10(a)
a = [12 3 7;-1 4 -2; 21 17 -5]
resultOFlog = log(a)
resultOFlog2 = log2(a)
resultOFlog10 = log10(a)
a =
12 3 7
-1 4 -2
21 17 -5
resultOFlog =
2.4849 1.0986 1.9459
0 + 3.1416i 1.3863 0.6931 + 3.1416i
3.0445 2.8332 1.6094 + 3.1416i
resultOFlog2 =
3.5850 1.5850 2.8074
0 + 4.5324i 2.0000 1.0000 + 4.5324i
4.3923 4.0875 2.3219 + 4.5324i
resultOFlog10 =
1.0792 0.4771 0.8451
0 + 1.3644i 0.6021 0.3010 + 1.3644i
1.3222 1.2304 0.6990 + 1.3644i
9)舎入函数 round(a),floor(a),ceil(a),fix(a)
a = [0.5 0.1 -0.2; -0.8 0.3 1.6]
a =
0.5000 0.1000 -0.2000
-0.8000 0.3000 1.6000
resultOFround =
1 0 0
-1 0 2
resultOFfloor =
0 0 -1
-1 0 1
resultOFceil =
1 1 0
0 1 2
resultOFfix =
0 0 0
0 0 1
10)模除求余数函数的使用mod(x,y),rem(x,y)
a =
5 6 7
-3 -2 -1
18 -16 9
b =
2 3 -2
1 5 6
-3 -1 7
ResultOfAMod3 =
2 0 1
0 1 2
0 2 0
ResultOfAModB =
1 0 -1
0 3 5
0 0 2
ResultOfARemB =
1 0 1
0 -2 -1
0 0 2
2.随机函数的使用
1)均匀分布随机矩阵函数
m = 4
n = 5
x = rand(n)
m =
4
n =
5
x =
0.1673 0.5880 0.8256 0.1117 0.4950
0.8620 0.1548 0.7900 0.1363 0.1476
0.9899 0.1999 0.3185 0.6787 0.0550
0.5144 0.4070 0.5341 0.4952 0.8507
0.8843 0.7487 0.0900 0.1897 0.5606
x =
0.9296 0.8790 0.6126 0.8013 0.5747
0.6967 0.9889 0.9900 0.2278 0.8452
0.5828 0.0005 0.5277 0.4981 0.7386
0.8154 0.8654 0.4795 0.9009 0.5860
x =
0.2467 0.6609 0.7690 0.0170 0.8449
0.6664 0.7298 0.5814 0.1209 0.2094
0.0835 0.8908 0.9283 0.8627 0.5523
0.6260 0.9823 0.5801 0.4843 0.6299
x =
0.0320 0.0495 0.1231
0.6147 0.4896 0.2055
0.3624 0.1925 0.1465
resultofRAND1 =
0.1891
resultofRAND2 =
0.8301
0.6705
0.0845
0.0686
0.0371
0.3854
0.1653
0.3752
0.7297
0.4534
0.8596
0.5685
0.9848
0.3742
0.3715
0.9499
0.9774
0.7428
0.4958
0.4157
0.0777
0.3299
0.9429
0.0906
0.3091
0.5518
0.0350
0.0018
0.9854
0.8229
0.4586
0.9710
0
0
0.0000
2)标准正态分布随机函数
m = 4
n = 5
x = randn(n)
m =
4
n =
5
x =
-1.7254 -0.0022 0.9608 0.3763 -0.5100
0.2882 0.0931 1.7382 -0.2270 -1.3216
-1.5942 -0.3782 -0.4302 -1.1489 -0.6361
0.1102 -1.4827 -1.6273 2.0243 0.3179
0.7871 -0.0438 0.1663 -2.3595 0.1380
x =
-0.7107 -0.4256 1.0635 -0.3712 -0.5568
0.7770 1.0486 1.1569 -0.7578 -0.8951
0.6224 0.6607 0.0530 -0.5640 -0.4093
0.6474 2.5088 -1.2884 0.5551 -0.1609
x =
0.4093 1.3244 -1.3853 -0.8927 -0.2269
-0.9526 -0.2132 0.3105 1.9085 -0.1625
0.3173 -0.1345 -0.2495 0.1222 0.6901
0.0780 -1.1714 0.5037 1.0470 0.5558
x =
-1.1203 -1.4158 -0.3680
-1.5327 0.0596 -1.3610
-1.0979 -0.4113 0.7796
resultofRAND1 =
0.4394
resultofRAND2 =
362436069
521288629
3)正态分布随机函数
x =
1.7996 4.2834 0.0457 2.9273 2.7791 2.7810 0.3694 2.7308 0.8487 -0.0045
x =
-0.6906 4.3206 0.1084 1.6134 -0.7026 1.3356 -5.2271 -0.4305 -1.1896 -0.2684
六,符号运算
1.符号表达式的生成
1)用单引号生成符号表达式 (表达式,方程,微分方程)
f = 'exp(x)'
f = 'a*x^2+bx+c=0'
f = 'D2y - 2Dy - 3y =0'
f =
exp(x)
f =
a*x^2+bx+c=0
f =
D2y - 2Dy - 3y =0
2)使用函数sym来生成符号表达式 ,使用符号数组
A = sym('[a b c ; e f g ]')
f = sym('ax +b =0')
A =
[ a, b, c]
[ e, f, g]
f =
ax + b = 0
3)使用符号表达式来生成, 但是不能生成符号方程.
syms y u;
p = exp(-y/u)
p =
1/exp(y/u)
2.符号表达式的计算
1)提取分子,分母[n,d]=numden(a) n = numden(a)
f = sym ('a*x^2/(b-x)')
[n,d] = numden(f)
f =
(a*x^2)/(b - x)
n =
a*x^2
d =
b - x
2)符号表达式的基本运算
B = sym('x+1')
C = sym('x^2-1')
D = B + C
B =
x + 1
C =
x^2 - 1
D =
x^2 + x
3.符号表达式的高级运算
f =
1/x^3
g =
tan(y)
ans =
1/tan(y)^3
a =
10 11 8 9 20 44 40
ElementBetween10and20 =
10 11 20
附:MATLAB基础知识简介QP
matlab入门基础知识精心整理比较完整
MATLAB优化函数