MATLAB学习笔记(一)
Exercise
Solution
1) cos(((1+2+3+4)^3/5)^0.5)
2)sin((pi)^0.5)+log(tan(1))
3)2^(3.5*1.7)
4)exp(sin(10))
Special Variables and Constants
列出关键字:>>iskeyword
数字输出格式转换
>>format long
例如:pi
1)short: 3.1416 2)long: 3.141592653589793 3)shortE:3.1416e+00
4)longE:3.141592653589793e+00 5)rat:355/113
Exercise
solution
>> format rat
>> 3/13+4/14+5/15ans =
232/273
>> format long
>> 3/13+4/14+5/15ans =
0.849816849816850
矩阵
试算出A(8)、 A([1 3 5])、A([1 3;1 3])、A(3,2)、 A([1 3],[1 3])的结果。
>> A=[1 21 6;5 17 9;31 2 7]
A =
1 21 6
5 17 9
31 2 7>> A(8)
ans =
9
>> A([1 3 5])ans =
1 31 17
>> A([1 3;1 3])ans =
1 31
1 31>> A(3,2)
ans =
2
>> A([1 3],[1 3])
ans =
1 6
31 7
A =1 21 6
5 17 9
31 2 7>> A(1,2)=76
A =
1 76 6
5 17 9
31 2 7>> A(3,2)=0
A =
1 76 6
5 17 9
31 0 7>> A(1,2)=0
A =
1 0 6
5 17 9
31 0 7>> A(2,2)=0
A =
1 0 6
5 0 9
31 0 7>> A(1,3)=0
A =
1 0 0
5 0 9
31 0 7>> A(2,3)=0
A =
1 0 0
5 0 0
31 0 7
测试 1)B = 1:5;2) B=1:2:5;3)B=[1:5;2:3:15;-2:0.5:0];4) str = 'a':2:'z'
>> B = 1:5
B =
1 2 3 4 5
>> B=1:2:5
B =
1 3 5
>> B=[1:5;2:3:15;-2:0.5:0]
B =
1.000000000000000 2.000000000000000 3.000000000000000 4.000000000000000 5.000000000000000
2.000000000000000 5.000000000000000 8.000000000000000 11.000000000000000 14.000000000000000
-2.000000000000000 -1.500000000000000 -1.000000000000000 -0.500000000000000 0>> str = 'a':2:'z'
str =
acegikmoqsuwy
A =
1 0 0
5 0 0
31 0 7>> A(3,:)
ans =
31 0 7
>> A(3,:)=[]
A =
1 0 0
5 0 0
1)
>> x1=A+a
x1 =
3 4 5
6 7 6
11 10 92)
>> x2=A/a
x2 =
0.500000000000000 1.000000000000000 1.500000000000000
2.000000000000000 2.500000000000000 2.000000000000000
4.500000000000000 4.000000000000000 3.500000000000000
3)>> x3=A./a
x3 =
0.500000000000000 1.000000000000000 1.500000000000000
2.000000000000000 2.500000000000000 2.000000000000000
4.500000000000000 4.000000000000000 3.500000000000000
4)>> x4=A^a
x4 =
36 36 32
60 65 60
104 114 108
5)>> x5=A.^a
x5 =
1 4 9
16 25 16
81 64 49
6)>> C=A'
C =
1 4 9
2 5 8
3 4 77)
>> y1=A+B
y1 =
4 5 6
6 9 13
10 11 8
8)>> y2=A*B
y2 =
10 20 24
26 44 61
50 80 1069)
>> y3=A.*B
y3 =
3 6 9
8 20 36
9 24 710)
>> y4=A/B
y4 =
0.071428571428571 0.285714285714286 0.214285714285714
1.166666666666667 0 0.500000000000000
3.261904761904762 -0.285714285714286 -0.214285714285714
11)>> y5=A./B
y5 =
0.333333333333333 0.666666666666667 1.000000000000000
2.000000000000000 1.250000000000000 0.444444444444444
9.000000000000000 2.666666666666667 7.000000000000000
一些特殊的矩阵
eye(n) 、zero(n1,n2)、ones(n1,n2)、diag()
>> eye(3)
ans =
1 0 0
0 1 0
0 0 1>> zeros(3,3)
ans =
0 0 0
0 0 0
0 0 0>> ones(4,4)
ans =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1>> v=[1 2 3 4];
>> diag(v)ans =
1 0 0 0
0 2 0 0
0 0 3 0
0 0 0 4