1.21k likes | 1.42k Views
DREAM. IDEA. PLAN. IMPLEMENTATION. Introduction to Matlab. Present to: Amirkabir University of Technology (Tehran Polytechnic) & Semnan University. Dr. Kourosh Kiani Email: kkiani2004@yahoo.com Email : Kourosh.kiani@aut.ac.ir Web: www.kouroshkiani.com. Training.
E N D
DREAM IDEA PLAN IMPLEMENTATION
Introduction to Matlab Present to:Amirkabir University of Technology (Tehran Polytechnic) & Semnan University Dr. Kourosh Kiani Email: kkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.ir Web: www.kouroshkiani.com
>>3^2 - (5 + 4)/2 + 6*3 ans = 22.5000 >>ans^2 + sqrt(ans) ans = 510.9934 >>u = cos(10) u = -0.8391 >>v = sin(10) v = -0.5440 >>uˆ2 + vˆ2 ans = 1
>>solve('x^2 - 2*x - 4 = 0') ans = 1 - 5^(1/2) 5^(1/2) + 1 >>solve('x^2 - 4 = 0') ans = -2 2 >>solve('x^3 - 27 = 0') ans = 3 - 3/2 - 3^(1/2)*3/2*I - 3/2 + 3^(1/2)*3/2*I
>>[x, y] = solve('x^2 - y = 2', 'y - 2*x = 5') x = 2*2^(1/2) + 1 1 - 2*2^(1/2) y = 4*2^(1/2) + 7 7 - 4*2^(1/2) >>Z = [2,4,6,8] Z = 2 4 6 8 >>Y = [4 -3 5 -2 8 1] Y = 4 -3 5 -2 8 1
>>X = 1:9 X = 1 2 3 4 5 6 7 8 9 >>X = 0:2:10 X = 0 2 4 6 8 10 >>X(3) ans = 4
>>X = 0:2:10 X = 0 2 4 6 8 10 >>X’ ans = 0 2 4 6 8 10 >>X.^2 ans = 0 4 16 36 64 100
>>X = 0:2:10 X = 0 2 4 6 8 10 >>Y=-4 Y = -4 >> X.*Y ans = 0 -8 -16 -24 -32 -40
>> A = [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12] A = 1 2 3 4 5 6 7 8 9 10 11 12 >> A = [1 2 3 4; 5 6 7 8; 9 10 11 12] A = 1 2 3 4 5 6 7 8 9 10 11 12 >> [2 3] < [3 2] ans = 1 0
>> x = -2:2; x >= 0 ans = 0 0 1 1 1 >> x(x >= 0) ans = 0 1 2
>> a=magic(4) a = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 >> b=sum(a) b = 34 34 34 34 >> diag(a) ans = 16 11 6 1
>> a=magic(4) a = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 >> a(4,4)=1000 a = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1000
>> A = [2 7 4] A = 2 7 4 >> A = [2; 7; 4] A = 2 7 4
>> A = [2 7 4; 3 8 9] A = 2 7 4 3 8 9 >> B=[A A] B = 2 7 4 2 7 4 3 8 9 3 8 9
>> u = [ 1:3 ]' u = 1 2 3 >> v = [ u u ] v = 1 1 2 2 3 3
>> a=[1 2;3 4] a = 1 2 3 4 >> cat_a=[a, 2*a; 3*a, 4*a; 5*a, 6*a] cat_a = 1 2 2 4 3 4 6 8 3 6 4 8 9 12 12 16 5 10 6 12 15 20 18 24 Use square brackets [ ] 4*a
>> A=magic(4) A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 >> A(4,4) ans = 1 >> A(4,1) ans = 4 >> A(3,3) ans = 6
>> x = [ -2 0 9 1 4 ] x = -2 0 9 1 4 >> x(1) ans = -2 >> x(5) ans = 4 >> x(8) ??? Attempted to access x(8); index out of bounds because numel(x)=5. >> x(-1) ??? Attempted to access x(-1); index must be a positive integer or logical.
>> Z=[1+i 2 1; 2+5i i 2] Z = 1.0000 + 1.0000i 2.0000 1.0000 2.0000 + 5.0000i 0 + 1.0000i 2.0000
>> A=magic(4) A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 >> A(1:2,3:4) ans = 3 13 10 8
>> A=magic(4) A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 >> A([2 3],[1 2]) ans = 5 11 9 7
>> A=magic(4) A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 >> B=A([3 2],[2 1]) B = 7 9 11 5
>> A=magic(4) A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 >> B=[A(3,2),A(3,1);A(2,2),A(2,1)] B = 7 9 11 5
>> A=magic(4) A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 >> A(1,:) ans = 16 2 3 13 >> B=A(1,:) B = 16 2 3 13
>> A=magic(4) A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 >> A(1:2,:) ans = 16 2 3 13 5 11 10 8 >> A([1 2],:) ans = 16 2 3 13 5 11 10 8
>> a = [100 200 300 400 500 600 700] a = 100 200 300 400 500 600 700 >> b = [3 5 6] b = 3 5 6 >> c = a(b) c = 300 500 600
>> A=magic(6) A = 35 1 6 26 19 24 3 32 7 21 23 25 31 9 2 22 27 20 8 28 33 17 10 15 30 5 34 12 14 16 4 36 29 13 18 11 >> B=A(1:2:5,3) B = 6 2 34
>> A=magic(6) A = 35 1 6 26 19 24 3 32 7 21 23 25 31 9 2 22 27 20 8 28 33 17 10 15 30 5 34 12 14 16 4 36 29 13 18 11 >> B = A(:, 2:3) B = 1 6 32 7 9 2 28 33 5 34 36 29
>> x(1:5) ans = -2 0 9 1 4 >> x([2 4]) ans = 0 1
>> X=[2 6 4 9 7] X = 2 6 4 9 7 >> X([5 3 2 4 1]) ans = 7 4 6 9 2
>> x = 1:2:10 x = 1 3 5 7 9 >> y = 0:0.1:0.5 y = 0 0.1000 0.2000 0.3000 0.4000 0.5000 >> C= 10:-1:2 C = 10 9 8 7 6 5 4 3 2
>> A=100:-7:50 A = Columns 1 through 5 100 93 86 79 72 Columns 6 through 8 65 58 51 >> B=0:pi/4:pi B = 0 355/452 355/226 1065/452 355/113
linspace(x1, x2) gives 100 evenly spaced values between x1 and x2 • x = linspace(5,20); • linspace(a,b,n) generate n equally spaced points between a and b • x = linspace(a,b,n) • >> x = linspace(5,20,8) • x = • 5.0000 7.1429 9.2857 11.4286 13.5714 15.7143 17.8571 20.0000
logspace(a,b,n) generates a logarithmically equally spaced row vector x = logspace(a,b,n) logspace(a,b) generates 50 logarithmically equally spaced points x = logspace(a,b) >> logspace(-4,2,7) ans = 0.0001 0.0010 0.0100 0.1000 1.0000 10.0000 100.0000
>> A = [1:3:15; linspace(0,1,5)] A = 1.0000 4.0000 7.0000 10.0000 13.0000 0 0.2500 0.5000 0.7500 1.0000 >> A = [(1:3:15)', linspace(0,1,5)'] A = 1.0000 0 4.0000 0.2500 7.0000 0.5000 10.0000 0.7500 13.0000 1.0000
>> eye(3) ans = 1 0 0 0 1 0 0 0 1 >> eye(4) ans = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
>> zeros(4) ans = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >> zeros(3,5) ans = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >> zeros(4,5) ans = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> ones(4) ans = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 >> ones(3,5) ans = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 >> ones(2,5) ans = 1 1 1 1 1 1 1 1 1 1
rand: uniformly distributed random numbers. >>A = rand(3,5) A = 0.9501 0.4860 0.4565 0.4447 0.9218 0.2311 0.8913 0.0185 0.6154 0.7382 0.6068 0.7621 0.8214 0.7919 0.1763 randn: normally distributed random numbers. >>B = randn(3,5) B = -1.1465 -0.0376 -0.1867 2.1832 1.0668 1.1909 0.3273 0.7258 -0.1364 0.0593 1.1892 0.1746 -0.5883 0.1139 -0.0956
>> a=[1 2; 3 5] a = 1 2 3 5 >> b=[3 4; 7 5] b = 3 4 7 5 >> a*b ans = 17 14 44 37 >> a=[1 2; 3 5] a = 1 2 3 5 >> b=[3 4; 7 5] b = 3 4 7 5 >> a.*b ans = 3 8 21 25
>> A=[2 3 8 1] A = 2 3 8 1 >> B=[1 4 5 2] B = 1 4 5 2 >> C=A.*B C = 2 12 40 2 >> d=A./B d = 2.0000 0.7500 1.6000 0.5000 >> A=[2 3 8 1] A = 2 3 8 1 >> B=[1 4 5 2] B = 1 4 5 2 >> d=A.^3 d = 8 27 512 1 >> E=(3).^B E = 3 81 243 9
>> 3 < 4 ans = 1 >> 6 < 3 ans = 0 >> 5==5 ans = 1 >> 5==7 ans = 0 >> 3 ~= 4 ans = 1 >> 3 ~= 3 ans = 0 >> 6 > 5 ans = 1 >> 6 > 8 ans = 0
>> a=[1 2; 0 -1] a = 1 2 0 -1 >> a>1 ans = 0 1 0 0 >> a<0 ans = 0 0 0 1
>> a=4 a = 4 >> b=3 b = 3 >> c=5 c = 5 >> (a<b) & (b<c) ans = 0 >> a=4 a = 4 >> b=3 b = 3 >> ~(a==b) ans = 1
>> x=1 x = 1 >> y= -1 y = -1 >> x>0 & y>0 ans = 0 >> x>0 | y>0 ans = 1 >> a=4 a = 4 >> b=3 b = 3 >> c=5 c = 5 >> (a>b) | (a>c) ans = 1 >> (a>b) | (b<c) ans = 1
>> u = [0 0 1 1 0 1]; >> v = [0 1 1 0 0 1]; >> u | v ans = 0 1 1 1 0 1 >> u & v ans = 0 0 1 0 0 1
>> A=magic(4) A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 >> mean(A) ans = 8.5000 8.5000 8.5000 8.5000 >> sum(A) ans = 34 34 34 34 >> prod(A) ans = 2880 2156 2700 1248 >> A=magic(4) A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 >> max(A) ans = 16 14 15 13 >> min(A) ans = 4 2 3 1
>> A=[1 2 3 4 5 6] A = 1 2 3 4 5 6 >> max(A) ans = 6 >> min(A) ans = 1 >> sum(A) ans = 21 >> prod(A) ans = 720 >> A=[8 7 4 3 2 1 9 8] A = 8 7 4 3 2 1 9 8 >> sort(A) ans = 1 2 3 4 7 8 8 9