Presentation is loading. Please wait.

Presentation is loading. Please wait.

DREAM PLAN IDEA IMPLEMENTATION. 2 Present to: Amirkabir University of Technology (Tehran Polytechnic) & Semnan University Dr. Kourosh Kiani

Similar presentations


Presentation on theme: "DREAM PLAN IDEA IMPLEMENTATION. 2 Present to: Amirkabir University of Technology (Tehran Polytechnic) & Semnan University Dr. Kourosh Kiani"— Presentation transcript:

1 DREAM PLAN IDEA IMPLEMENTATION

2 2 Present to: Amirkabir University of Technology (Tehran Polytechnic) & Semnan University Dr. Kourosh Kiani Web: Introduction to Matlab

3 Training

4 >>3^2 - (5 + 4)/2 + 6*3 ans = >>ans^2 + sqrt(ans) ans = >>u = cos(10) u = >>v = sin(10) v = >>uˆ2 + vˆ2 ans = 1

5 >>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^ = 0') ans = 3 - 3/2 - 3^(1/2)*3/2*I - 3/2 + 3^(1/2)*3/2*I

6 >>[x, y] = solve('x^2 - y = 2', 'y - 2*x = 5') x = 2*2^(1/2) *2^(1/2) y = 4*2^(1/2) *2^(1/2) >>Z = [2,4,6,8] Z = >>Y = [ ] Y =

7 >>X = 1:9 X = >>X = 0:2:10 X = >>X(3) ans = 4

8 >>X = 0:2:10 X = >>X’ ans = >>X.^2 ans =

9 >>X = 0:2:10 X = >>Y=-4 Y = -4 >> X.*Y ans =

10 >> A = [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12] A = >> A = [ ; ; ] A = >> [2 3] < [3 2] ans = 1 0

11 >> x = -2:2; x >= 0 ans = >> x(x >= 0) ans = 0 1 2

12 >> a=magic(4) a = >> b=sum(a) b = >> diag(a) ans =

13 >> a=magic(4) a = >> a(4,4)=1000 a =

14 >> A = [2 7 4] A = >> A = [2; 7; 4] A = 2 7 4

15 >> A = [2 7 4; 3 8 9] A = >> B=[A A] B =

16 >> u = [ 1:3 ]' u = >> v = [ u u ] v =

17 >> a=[1 2;3 4] a = >> cat_a=[a, 2*a; 3*a, 4*a; 5*a, 6*a] cat_a = >> a=[1 2;3 4] a = >> cat_a=[a, 2*a; 3*a, 4*a; 5*a, 6*a] cat_a = Use square brackets [ ] 4*a

18 >> A=magic(4) A = >> A(4,4) ans = 1 >> A(4,1) ans = 4 >> A(3,3) ans = 6

19 >> x = [ ] x = >> 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.

20 >> Z=[1+i 2 1; 2+5i i 2] Z = i i i

21 >> A=magic(4) A = >> A(1:2,3:4) ans =

22 >> A=magic(4) A = >> A([2 3],[1 2]) ans =

23 >> A=magic(4) A = >> B=A([3 2],[2 1]) B =

24 >> A=magic(4) A = >> B=[A(3,2),A(3,1);A(2,2),A(2,1)] B =

25 >> A=magic(4) A = >> A(1,:) ans = >> B=A(1,:) B =

26 >> A=magic(4) A = >> A(1:2,:) ans = >> A([1 2],:) ans =

27 >> a = [ ] a = >> b = [3 5 6] b = >> c = a(b) c =

28 >> A=magic(6) A = >> B=A(1:2:5,3) B =

29 >> A=magic(6) A = >> B = A(:, 2:3) B =

30 >> x(1:5) ans = >> x([2 4]) ans = 0 1

31 >> X=[ ] X = >> X([ ]) ans =

32 >> x = 1:2:10 x = >> y = 0:0.1:0.5 y = >> C= 10:-1:2 C =

33 >> A=100:-7:50 A = Columns 1 through Columns 6 through >> B=0:pi/4:pi B = 0 355/ / / /113

34 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 =

35 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 =

36 >> A = [1:3:15; linspace(0,1,5)] A = >> A = [(1:3:15)', linspace(0,1,5)'] A =

37 >> eye(3) ans = >> eye(4) ans =

38 >> zeros(4) ans = >> zeros(3,5) ans = >> zeros(4,5) ans =

39 >> ones(4) ans = >> ones(3,5) ans = >> ones(2,5) ans =

40 rand: uniformly distributed random numbers. >>A = rand(3,5) A = randn: normally distributed random numbers. >>B = randn(3,5) B =

41

42 >> a=[1 2; 3 5] a = >> b=[3 4; 7 5] b = >> a.*b ans = >> a=[1 2; 3 5] a = >> b=[3 4; 7 5] b = >> a*b ans =

43 >> A=[ ] A = >> B=[ ] B = >> C=A.*B C = >> d=A./B d = >> A=[ ] A = >> B=[ ] B = >> d=A.^3 d = >> E=(3).^B E =

44 >> 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

45 >> a=[1 2; 0 -1] a = >> a>1 ans = >> a<0 ans =

46 >> a=4 a = 4 >> b=3 b = 3 >> ~(a==b) ans = 1 >> a=4 a = 4 >> b=3 b = 3 >> c=5 c = 5 >> (a

47 >> a=4 a = 4 >> b=3 b = 3 >> c=5 c = 5 >> (a>b) | (a>c) ans = 1 >> (a>b) | (b> x=1 x = 1 >> y= -1 y = >> x>0 & y>0 ans = 0 >> x>0 | y>0 ans = 1

48 >> u = [ ]; >> v = [ ]; >> u | v ans = >> u & v ans =

49 >> A=magic(4) A = >> max(A) ans = >> min(A) ans = >> A=magic(4) A = >> mean(A) ans = >> sum(A) ans = >> prod(A) ans =

50 >> A=[ ] A = >> max(A) ans = 6 >> min(A) ans = 1 >> sum(A) ans = 21 >> prod(A) ans = 720 >> A=[ ] A = >> sort(A) ans =

51 >> A=[ ] A = >> size(A) ans = 1 8 >> length(A) ans = 8 >> z=sqrt(16) z = 4 >> A=[ ] A = >> B=sqrt(A) B =

52 >> A=0/0 A = NaN >> B=1/0 B = Inf >> realmax ans = e+308 >> C=2^realmax C = Inf >> Inf/Inf ans = NaN >> t = t = 45

53 >> s = 'abc' s = abc >> s(1) ans = a >> s( [ 1 2 ] ) = 'XX' s = XXc

54 >> A=double( 'abc xyz' ) A = >> double( 'ABC XYZ' ) ans = >> char( [ ] ) ans = Hello!

55 >> s = char( 'my first string', 'my second string' ) s = my first string my second string >> size(s) ans = 2 16 >> size( deblank( s(1,:) ) ) ans = 1 15

56 ischar() : returns 1 for a character array >>ischar ( 'CS 111' ) ans = 1 isletter() : returns 1 for letters of the alphabet >>isletter( 'CS 111' ) ans = isspace() : returns 1 for whitespace (blank, tab, new line) >>isspace( 'CS 111' ) ans =

57 >> 'a' < 'e' ans = 1 >> 'fate' == 'cake' ans = >> 'fate' > 'cake' ans =

58 strcmp() : returns 1 if two strings are identical >>a = 'Bilkent'; >>strcmp( a, 'Bilkent' ) ans = 1 >>strcmp( 'Hello', 'hello' ) ans = 0 strcmpi() : returns 1 if two strings are identical ignoring case >>strcmpi( 'Hello', 'hello' ) ans = 1

59 >> str1='My first string' str1 = My first string >> findstr(str1,'first') ans = 4 >> strcmp(str1,'My') ans = 0

60 >> strncmp(str1,'My',2) ans = 1 >> str2='45.6' str2 = 45.6 >> str2num(str2) ans =

61 >> str1 ='My first string' str1 = My first string >> str2 = '45.6' str2 = 45.6 >> strcat(str1,str2) ans = My first string45.6

62 Lowercase-to-uppercase >>a = upper( 'This is test 1!' ) a = THIS IS TEST 1! Uppercase-to-lowercase >>a = lower( 'This is test 1!' ) a = this is test 1!

63 findstr() : finds one string within another one >>test = 'This is a test!'; >>pos = findstr( test, 'is' ) pos = 3 6 >>pos = findstr( test, ' ' ) pos =

64 strrep() : replaces one string with another >>s1 = 'This is a good example'; >>s2 = strrep( s1, 'good', 'great' ) s2 = This is a great example

65 Recall num2str() for numeric-to-string conversion >>str = [ 'Plot for x = ' num2str( 10.3 ) ] str = Plot for x = 10.3 str2num() : converts strings containing numbers to numeric form >>x = str2num( '3.1415' ) x =

66 >> format long e; >> pi ans = e+000 >> format short g; >> pi ans = >> format long g; >> pi ans = >> format long e; >> pi ans = e+000 >> format short g; >> pi ans = >> format long g; >> pi ans =

67 >> format hex; >> pi ans = fb54442d18 >> format bank; >> pi ans = 3.14 >> format +; >> pi ans = + >> format rat; >> pi ans = 355/113 >> a=magic(4) a = >> format +; >> a a = ++++

68 >> A=rand(4) A = >> format rat; >> A=rand(4) A = 659/ / / / / / / / / / / / / / / /1841

69 >> N = 10*rand(1,8) N = >> N = fix(N) N =

70 >> A = [ ] A = >> B = [A A+32; A+48 A+16] B =

71 A = >> X=A X = >> X(:,2) = [] X =

72 Deleting Rows and Columns >> A=[1 5 9; ; i+1] A = i >> A(:,2)=[] A = i >> A(2,2)=[] ??? Indexed empty matrix assignment is not allowed. >> A=[1 5 9; ; i+1] A = i >> A(:,2)=[] A = i >> A(2,2)=[] ??? Indexed empty matrix assignment is not allowed.

73 >> A = [5.36; 7.01; [ ]; 9.44] A =

74 >> v = [ 1 3, sqrt(5)] v = >> D = [1:5; 6:10; 11:2:20] D = >> d = [-3 4 2], M = diag(d) d = M =

75 >> J = [1:4; 5:8; 9:12; ] J = >> K = [ diag(1:4) J; J' zeros(4,4)] K =

76 >> F = [ ; ; ] F = >> diag(F) ans =

77 The command spy(K) will produce a graphical display of the location of the nonzero entries in K (it will also give a value for nz|the number of nonzero entries) >> J = [1:4; 5:8; 9:12; ] J = >> K = [ diag(1:4) J; J' zeros(4,4)] K = >> spy(K)

78 >> x = 0:0.1:0.5; >> y = 4*sin(3*x); >> u = 3*sin(4*x); >> w=[ x' y' u'] w =

79 >> i = [1, 3, 5]; >> j = [2,3,4]; >> v = [ ]; >> S = sparse (i,j,v) S = (1,2) 10 (3,3) 11 (5,4) 12 >> T = full(S) T =

80 >> x = 0:0.05:6; >> y = sin(pi*x); >> Y = (y>=0).*y; >> plot(x,y,':',x,Y,'-' )

81 >> x = pi*(-1:3) x = >> round(x) ans = >> fix(x) ans = >> floor(x) ans = >> x = pi*(-1:3) x = >> ceil(x) ans = >> sign(x) ans = >> rem(x,3) ans =

82 >> A = [ ; ; ] A = >> k = find(A==0) k = 2 9 >> k = find(A==4) k = 7 10

83 >> A = [ ; ; ] A = >> n = find(A <= 0) n = >> A(n) ans =

84 >> A=[1 2 3;4 5 6;7 8 9] A = >> A(:) ans =

85 >> A = 3+2*i; % The number (3+2i) is saved to variable A. >> A A = i >> B = phase(A); % The phase angle of (3+2i) is saved to variable B. >> B B = >> C = real(A); % The real part of (3+2i) is saved to variable C. >> C C = 3 >> D = imag(A); % The imaginary part of (3+2i) is saved to variable D. >> D D = 2 >> E = abs(A); % The absolute value of (3+2i) is saved to variable E. >> E E =

86 To represent the polynomial x3 - 2x - 5, save the coefficients in a matrix: >>p = [1 0 –2 –5] To factor the polynomial equation x3 - 6x2 +11x - 6 = 0, enter the coefficients into a matrix and use the roots() command: >> p = [ ]; >> r = roots(p)' r =

87 >> student.name = 'Clinton, Bill'; >> student.SSN = ; >> student.homework = [ ]; >> student.exam = [98 94]; >> student student = name: 'Clinton, Bill' SSN: homework: [ ] exam: [98 94] >> student(2).name = 'Bush, G. W.'; >> student(2).SSN = ; >> student(2).homework = [ ]; >> student(2).exam = [53 66]; >> student student = 1x2 struct array with fields: name SSN homework exam

88 >> k=struct('a',10,'b',4, 'c', 'kiani'); >> k k = a: 10 b: 4 c: 'kiani' >> k.a ans = 10 >> k.b ans = 4 >> k.c ans = kiani

89 >> kk=struct('name',{'kourosh' 'pantea' 'shain'}, 'age', {40, 17 70}, 'other',{[ ] [4 5 6] [7 8 9]}); >> kk kk = 1x3 struct array with fields: name age other >> kk(1) ans = name: 'kourosh' age: 40 other: [ ] >> kk(2) ans = name: 'pantea' age: 17 other: [4 5 6] >> kk(3) ans = name: 'shain' age: 70 other: [7 8 9]

90 >> A(1,1) = {15}; >> A(1,2) = {[1,2,3,4,5]}; >> A(2,1) = {'kiani'}; >> A(2,2) = {[1 0 0;0 1 0;0 0 1]}; >> A A = [ 15] [1x5 double] 'kiani' [3x3 double] >> celldisp(A) A{1,1} = 15 A{2,1} = kiani A{1,2} = A{2,2} = >> A{2,2} ans =

91 >>cellplot(A)

92 >> A={[1 4 5;3 5 7;5 6 7],'Kourosh kiani';4+6i,-pi:pi/4:pi}; >> A A = [3x3 double] 'Kourosh kiani' [ i] [1x9 double] >> A(:) ans = [3x3 double] [ i] 'Kourosh kiani' [1x9 double]

93 >> cellplot(A)

94 >> A{:} ans = ans = i ans = Kourosh kiani ans = Columns 1 through

95 >> A{1}(:,:) ans = >> A{2}(:,:) ans = i >> A{3}(:,:) ans = Kourosh kiani >> A{4}(:,:) ans = Columns 1 through

96 >> A{1}(:,:) ans = >> A{1}(1,:) ans = >> A{1}(2,2) ans = 5 >> A{3}(1,:) ans = Kourosh kiani >> A{3}(1,4) ans = r

97 >> A(:,:,1) =zeros(3,3) A = >> A(:,:,2) =eye(3,3) A(:,:,1) = A(:,:,2) = >> A(:,:,3) =magic(3) A(:,:,1) = A(:,:,2) = A(:,:,3) =

98 >> A=[1 2 3;4 5 6;7 8 9] A = >> B=flipdim(A,1) B = >> B=flipdim(A,2) B = flipdim flip array along a specified dimension When the value of dim is 1, the array is flipped row-wise down. When dim is 2, the array is flipped columnwise left to right

99 >> A=[1 2 3;4 5 6;7 8 9] A = >> B=fliplr(A) B =

100 >> A=[1 2 3;4 5 6;7 8 9] A = >> B=flipud(A) B =

101 >> A = reshape(1:15,5,3) A = >> x=1:8 x = >> C = reshape(x,2,4) C =

102 >> A=[3 4; 5 2] A = >> B=[2 4; 3 5] B = >> C=kron(A,B) C = >> A=[3 4; 5 2] A = >> B=[2 4; 3 5] B = >> C=kron(B,A) C =

103 >> A=[1 2 0] A = >> norm(A, 1) ans = 3 >> norm(A, 2) ans = >> norm(A, 3) ans = >> norm(A, inf) ans = 2 >> norm(A) ans = Vector and Mtrix Norms The p-norm of a vector x

104 >> A=[1 2 0] A = >> B=[norm(A) norm(A,1) norm(A,2) norm(A,3) norm(A,100) norm(A,inf)] B =

105 >> C = fix(10*rand(3,2)) C = >> B=[norm(C,1) norm(C) norm(C,inf)] B =

106 A=LU >> A=magic(4) A = >> [L U]=lu(A) L = U =

107 QR Factorization A=QR >> A=[1 4 2;3 5 7;3 5 1] A = >> [Q R]=qr(A) Q = R = >> Q'*Q ans =

108

109 A = >> b=[9;21;-1] b = 9 21 >> inv(A) ans = >> inv(A) ans = >> inv(A)*b ans = 1 3 2

110 >> a=[1 2 3]; % inner product >> b=[2 3 4]; >> c=dot(a,b) c = 20 >> a=[1 2 3]; % cross product >> b=[2 3 4]; >> d= cross(a,b) d =

111 >> f = inline('x^3 +x -1') f = Inline function: f(x) = x^3 +x -1 >> f(4) ans = 67 >> f(-2) ans = -11

112 >> f = inline('3*x.^2 -2*y', 'x', 'y'); >> f(2,3) ans = 6 >> f(3,3) ans = 21

113 >> f = inline('x^3 +x -1') f = Inline function: f(x) = x^3 +x -1 >> A=[1 2 3;4 5 6;7 8 9] A = >> B=f(A) B =

114 >> x = -1:.4:1; y = 0:.4:4; >> [X,Y] = meshgrid(x,y) X =

115 Y =

116 >> f = inline('10*x.^2 + y.^2', 'x', 'y') f = Inline function: f(x,y) = 10*x.^2 + y.^2 >> Z = f(X,Y) Z =

117

118 r1=0.5:0.1:4; r2=1:0.1:5; [X,Y] = meshgrid(r1,r2); z=1-(X-2).^2-(Y-3).^2; z=max(z,0); surf(r1, r2, z); figure(gcf)

119 r1=0.5:0.1:4; r2=1:0.1:5; [X,Y] = meshgrid(r1,r2); s=max(min(1-abs(X-2),1-abs(Y-3)),0) surf(r1, r2, z); figure(gcf)

120 Questions? Discussion? Suggestions ?

121


Download ppt "DREAM PLAN IDEA IMPLEMENTATION. 2 Present to: Amirkabir University of Technology (Tehran Polytechnic) & Semnan University Dr. Kourosh Kiani"

Similar presentations


Ads by Google