DREAMDREAM
PLANPLAN
IDEAIDEA
IMPLEMENTATIONIMPLEMENTATION
2
Present to:Amirkabir University of Technology (Tehran Polytechnic) & Semnan
University
Dr. Kourosh KianiEmail: [email protected]: [email protected]: www.kouroshkiani.com
Introduction to Matlab
Training
>>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ˆ2ans = 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
>> 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
]81 ,25[]4^3 ,2^5[]4 ,2[].^3 ,5[
]243 ,9[]5^3 ,2^3[]5 ,2[.^3
]8 ,64[]3^2 ,3^4[3].^2 ,4[.^tionexponentiaArray .^
]7143.0,667.2[]5\7 ,8\3[]5 ,8[\].7 ,3[divisionleft Array \.
]400.1,375.0[]5/7 ,8/3[]5 ,8/[].7 ,3[divisionright Array ./
]18 ,6[]3 ,2[*].6 ,3[tionmultiplicaArray *.
]3 ,4[]3 ,8[]6 ,4[nsubtractioArray -
]9 ,12[]3 ,8[]6 ,4[additionArray
]3 ,2[6]3 ,8[nsubtractioarray -Scalar-
]9 ,7[3]6 ,4[additionarray -Scalar
ExampleFormOperationSymbol
BA
A.\B
A./B
A.*B
BA
BA
bA
bA
>> 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=[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=[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
>> ~(a==b)
ans =
1
>> a=4
a =
4
>> b=3
b =
3
>> c=5
c =
5
>> (a<b) & (b<c)
ans =
0
>> a=4a = 4
>> b=3b = 3>> c=5
c = 5
>> (a>b) | (a>c)
ans = 1
>> (a>b) | (b<c)
ans =
1
>> x=1
x =
1
>> y= -1
y =
-1
>> x>0 & y>0
ans =
0
>> x>0 | y>0
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
>> max(A)
ans =
16 14 15 13
>> min(A)
ans =
4 2 3 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=[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
>> A=[8 7 4 3 2 1 9 8]
A =
8 7 4 3 2 1 9 8
>> size(A)
ans =
1 8
>> length(A)
ans =
8
>> z=sqrt(16)
z =
4
>> A=[16 25 49 100 12]
A =
16 25 49 100 12
>> B=sqrt(A)
B =
4.0000 5.0000 7.0000 10.0000 3.4641
>> A=0/0
A =
NaN
>> B=1/0
B =
Inf
>> realmax
ans =
1.7977e+308
>> C=2^realmax
C =
Inf
>> Inf/Inf
ans =
NaN
>> t = 1 + 2 + 3 + ...4+5+6+7+8+9
t =
45
>> s = 'abc'
s =
abc
>> s(1)
ans =
a
>> s( [ 1 2 ] ) = 'XX'
s =
XXc
>> A=double( 'abc xyz' )
A =
97 98 99 32 120 121 122
>> double( 'ABC XYZ' )
ans =
65 66 67 32 88 89 90
>> char( [ 72 101 108 108 111 33 ] )
ans =
Hello!
>> 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
• ischar() : returns 1 for a character array>>ischar ( 'CS 111' )
ans = 1
• isletter() : returns 1 for letters of the alphabet>>isletter( 'CS 111' )
ans = 1 1 0 0 0 0
• isspace() : returns 1 for whitespace (blank, tab, new line)>>isspace( 'CS 111' )
ans = 0 0 1 0 0 0
>> 'a' < 'e'
ans =
1
>> 'fate' == 'cake'
ans =
0 1 0 1
>> 'fate' > 'cake'
ans =
1 0 1 0
• 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
>> str1='My first string'str1 = My first string
>> findstr(str1,'first')ans = 4>> strcmp(str1,'My')ans = 0
>> strncmp(str1,'My',2)ans = 1>> str2='45.6'str2 = 45.6
>> str2num(str2)ans = 45.6000
>> str1 ='My first string'str1 = My first string>> str2 = '45.6'str2 = 45.6>> strcat(str1,str2)ans = My first string45.6
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!
• findstr() : finds one string within another one>>test = 'This is a test!';>>pos = findstr( test, 'is' )
pos = 3 6
>>pos = findstr( test, ' ' )pos = 5 8 10
• strrep() : replaces one string with another>>s1 = 'This is a good example';>>s2 = strrep( s1, 'good', 'great' )
s2 =This is a great example
• 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 = 3.1415
>> format long e; >> pi
ans =
3.141592653589793e+000
>> format short g; >> pi
ans =
3.1416
>> format long g;>> pi
ans =
3.14159265358979
>> format long e; >> pi
ans =
3.141592653589793e+000
>> format short g; >> pi
ans =
3.1416
>> format long g;>> pi
ans =
3.14159265358979
>> format hex; >> pians = 400921fb54442d18
>> format bank; >> pi
ans = 3.14
>> format +; >> pi
ans =
+
>> format rat; >> pians =
355/113
>> a=magic(4)
a =
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
>> format +;>> a
a =
++++++++++++++++
>> A=rand(4)
A =
0.2769 0.6948 0.4387 0.1869 0.0462 0.3171 0.3816 0.4898 0.0971 0.9502 0.7655 0.4456 0.8235 0.0344 0.7952 0.6463
>> format rat;>> A=rand(4)
A =
659/929 302/461 1049/1093 1927/2565 1409/1867 655/4028 547/1607 388/1521 175/634 1078/9059 580/991 637/1259 365/537 457/917 438/1957 1287/1841
>> N = 10*rand(1,8)
N =
5.3080 7.7917 9.3401 1.2991 5.6882 4.6939 0.1190 3.3712
>> N = fix(N)
N =
5 7 9 1 5 4 0 3
>> A = [ ...16.0 3.0 2.0 13.05.0 10.0 11.0 8.09.0 6.0 7.0 12.04.0 15.0 14.0 1.0 ]
A = 16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1
>> B = [A A+32; A+48 A+16]
B = 16 3 2 13 48 35 34 45 5 10 11 8 37 42 43 40 9 6 7 12 41 38 39 44 4 15 14 1 36 47 46 33 64 51 50 61 32 19 18 29 53 58 59 56 21 26 27 24 57 54 55 60 25 22 23 28 52 63 62 49 20 31 30 17
A =
16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1
>> X=A
X =
16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1
>> X(:,2) = []
X =
16 2 13 5 11 8 9 7 12 4 14 1
Deleting Rows and Columns
>> A=[1 5 9;4 3 2.5; 0.1 10 3i+1]
A =
1.0000 5.0000 9.0000
4.0000 3.0000 2.5000
0.1000 10.0000 1.0000+3.0000i
>> A(:,2)=[]
A =
1.0000 9.0000
4.0000 2.5000
0.1000 1.0000 + 3.0000i
>> A(2,2)=[]
??? Indexed empty matrix assignment is not allowed.
>> A=[1 5 9;4 3 2.5; 0.1 10 3i+1]
A =
1.0000 5.0000 9.0000
4.0000 3.0000 2.5000
0.1000 10.0000 1.0000+3.0000i
>> A(:,2)=[]
A =
1.0000 9.0000
4.0000 2.5000
0.1000 1.0000 + 3.0000i
>> A(2,2)=[]
??? Indexed empty matrix assignment is not allowed.
>> A = [5.36; 7.01; [ ]; 9.44]
A =
5.3600 7.0100 9.4400
>> v = [ 1 3, sqrt(5)]v =
1.0000 3.0000 2.2361
>> D = [1:5; 6:10; 11:2:20]
D =
1 2 3 4 5 6 7 8 9 10 11 13 15 17 19
>> d = [-3 4 2], M = diag(d)
d =
-3 4 2
M = -3 0 0 0 4 0 0 0 2
>> J = [1:4; 5:8; 9:12; 20 0 5 4]
J =
1 2 3 4 5 6 7 8 9 10 11 12 20 0 5 4
>> K = [ diag(1:4) J; J' zeros(4,4)]
K =
1 0 0 0 1 2 3 4 0 2 0 0 5 6 7 8 0 0 3 0 9 10 11 12 0 0 0 4 20 0 5 4 1 5 9 20 0 0 0 0 2 6 10 0 0 0 0 0 3 7 11 5 0 0 0 0 4 8 12 4 0 0 0 0
>> F = [0 1 8 7; 3 -2 -4 2; 4 2 1 1]
F =
0 1 8 7 3 -2 -4 2 4 2 1 1
>> diag(F)
ans =
0 -2 1
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; 20 0 5 4]J =
1 2 3 4 5 6 7 8 9 10 11 12 20 0 5 4
>> K = [ diag(1:4) J; J' zeros(4,4)]
K = 1 0 0 0 1 2 3 4 0 2 0 0 5 6 7 8 0 0 3 0 9 10 11 12 0 0 0 4 20 0 5 4 1 5 9 20 0 0 0 0 2 6 10 0 0 0 0 0 3 7 11 5 0 0 0 0 4 8 12 4 0 0 0 0>> spy(K)
0 1 2 3 4 5 6 7 8 9
0
1
2
3
4
5
6
7
8
9
nz = 34
>> x = 0:0.1:0.5;>> y = 4*sin(3*x);>> u = 3*sin(4*x);>> w=[ x' y' u']
w =
0 0 0 0.1000 1.1821 1.1683 0.2000 2.2586 2.1521 0.3000 3.1333 2.7961 0.4000 3.7282 2.9987 0.5000 3.9900 2.7279
>> i = [1, 3, 5];>> j = [2,3,4];>> v = [10 11 12];>> S = sparse (i,j,v)
S =
(1,2) 10 (3,3) 11 (5,4) 12
>> T = full(S)
T =
0 10 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 0 12
>> x = 0:0.05:6;>> y = sin(pi*x);>> Y = (y>=0).*y;>> plot(x,y,':',x,Y,'-' )
0 1 2 3 4 5 6-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
>> x = pi*(-1:3)
x =
-3.1416 0 3.1416 6.2832 9.4248
>> round(x)
ans =
-3 0 3 6 9
>> fix(x)
ans =
-3 0 3 6 9
>> floor(x)
ans =
-4 0 3 6 9
>> x = pi*(-1:3)
x =
-3.1416 0 3.1416 6.2832 9.4248
>> ceil(x)
ans =
-3 0 4 7 10
>> sign(x)
ans =
-1 0 1 1 1
>> rem(x,3)
ans =
-0.1416 0 0.1416 0.2832 0.4248
>> A = [ -2 3 4 4; 0 5 -1 6; 6 8 0 1]
A =
-2 3 4 4 0 5 -1 6 6 8 0 1
>> k = find(A==0)
k =
2 9
>> k = find(A==4)
k =
7 10
>> A = [ -2 3 4 4; 0 5 -1 6; 6 8 0 1]
A =
-2 3 4 4 0 5 -1 6 6 8 0 1
>> n = find(A <= 0)
n =
1 2 8 9>> A(n)
ans = -2 0 -1 0
>> A=[1 2 3;4 5 6;7 8 9]
A =
1 2 3 4 5 6 7 8 9
>> A(:)
ans =
1 4 7 2 5 8 3 6 9
>> A = 3+2*i; % The number (3+2i) is saved to variable A.>> AA = 3.0000 + 2.0000i
>> B = phase(A); % The phase angle of (3+2i) is saved to variable B.>> BB = 0.5880
>> C = real(A); % The real part of (3+2i) is saved to variable C.>> CC = 3
>> D = imag(A); % The imaginary part of (3+2i) is saved to variable D.>> DD = 2>> E = abs(A); % The absolute value of (3+2i) is saved to variable E.>> EE = 3.6056
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 theroots() command:
>> p = [1 -6 11 -6]; >> r = roots(p)'
r =
3.0000 2.0000 1.0000
>> student.name = 'Clinton, Bill';>> student.SSN = 123456789;>> student.homework = [10 10 7 9 10];>> student.exam = [98 94];>> student
student =
name: 'Clinton, Bill' SSN: 123456789 homework: [10 10 7 9 10] exam: [98 94]
>> student(2).name = 'Bush, G. W.';>> student(2).SSN = 987654321;>> student(2).homework = [4 6 7 3 0];>> student(2).exam = [53 66];>> student
student =
1x2 struct array with fields: name SSN homework exam
>> 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
>> kk=struct('name',{'kourosh' 'pantea' 'shain'}, 'age', {40, 17 70}, 'other',{[1 2 3 4] [4 5 6] [7 8 9]});>> kk
kk =
1x3 struct array with fields: name age other
>> kk(1)
ans =
name: 'kourosh' age: 40 other: [1 2 3 4]
>> kk(2)ans = name: 'pantea' age: 17 other: [4 5 6]
>> kk(3)
ans =
name: 'shain' age: 70 other: [7 8 9]
>> 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} = 1 2 3 4 5
A{2,2} = 1 0 0 0 1 0 0 0 1
>> A{2,2}ans =
1 0 0 0 1 0 0 0 1
>>cellplot(A)
15
kiani
>> A={[1 4 5;3 5 7;5 6 7],'Kourosh kiani';4+6i,-pi:pi/4:pi};>> A
A =
[3x3 double] 'Kourosh kiani' [4.0000 + 6.0000i] [1x9 double]
>> A(:)
ans =
[3x3 double] [4.0000 + 6.0000i] 'Kourosh kiani' [1x9 double]
>> cellplot(A)
Kourosh kiani
4+6i
>> A{:}
ans =
1 4 5 3 5 7 5 6 7
ans =
4.0000 + 6.0000i
ans =
Kourosh kiani
ans =
Columns 1 through 9
-3.1416 -2.3562 -1.5708 -0.7854 0 0.7854 1.5708 2.3562 3.1416
>> A{1}(:,:)ans =
1 4 5 3 5 7 5 6 7
>> A{2}(:,:)ans = 4.0000 + 6.0000i
>> A{3}(:,:)ans =
Kourosh kiani
>> A{4}(:,:)
ans =
Columns 1 through 9
-3.1416 -2.3562 -1.5708 -0.7854 0 0.7854 1.5708 2.3562 3.1416
>> A{1}(:,:)ans = 1 4 5 3 5 7 5 6 7
>> A{1}(1,:)ans = 1 4 5
>> A{1}(2,2)ans = 5
>> A{3}(1,:)
ans =Kourosh kiani
>> A{3}(1,4)ans =
r
>> A(:,:,1) =zeros(3,3)
A =
0 0 0 0 0 0 0 0 0>> A(:,:,2) =eye(3,3)
A(:,:,1) =
0 0 0 0 0 0 0 0 0
A(:,:,2) =
1 0 0 0 1 0 0 0 1
>> A(:,:,3) =magic(3)
A(:,:,1) =
0 0 0 0 0 0 0 0 0
A(:,:,2) =
1 0 0 0 1 0 0 0 1
A(:,:,3) =
8 1 6 3 5 7 4 9 2
>> A=[1 2 3;4 5 6;7 8 9]
A =
1 2 3 4 5 6 7 8 9
>> B=flipdim(A,1)
B =
7 8 9 4 5 6 1 2 3
>> B=flipdim(A,2)
B =
3 2 1 6 5 4 9 8 7
flipdim flip array along a specified dimensionWhen 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
>> A=[1 2 3;4 5 6;7 8 9]
A =
1 2 3 4 5 6 7 8 9
>> B=fliplr(A)
B =
3 2 1 6 5 4 9 8 7
>> A=[1 2 3;4 5 6;7 8 9]
A =
1 2 3 4 5 6 7 8 9
>> B=flipud(A)
B =
7 8 9 4 5 6 1 2 3
>> A = reshape(1:15,5,3)
A =
1 6 11 2 7 12 3 8 13 4 9 14 5 10 15
>> x=1:8
x =
1 2 3 4 5 6 7 8
>> C = reshape(x,2,4)
C =
1 3 5 7 2 4 6 8
>> A=[3 4; 5 2]
A =
3 4 5 2
>> B=[2 4; 3 5]
B =
2 4 3 5
>> C=kron(A,B)
C =
6 12 8 16 9 15 12 20 10 20 4 8 15 25 6 10
>> A=[3 4; 5 2]
A =
3 4 5 2
>> B=[2 4; 3 5]
B =
2 4 3 5
>> C=kron(B,A)
C =
6 8 12 16 10 4 20 8 9 12 15 20 15 6 25 10
>> A=[1 2 0]
A =
1 2 0
>> norm(A, 1)
ans =
3
>> norm(A, 2)
ans =
2.2361
>> norm(A, 3)
ans =
2.0801
>> norm(A, inf)
ans =
2
>> norm(A)
ans =
2.2361
pp
ipxx
1
Vector and Mtrix NormsThe p-norm of a vector x
>> A=[1 2 0]
A =
1 2 0
>> B=[norm(A) norm(A,1) norm(A,2) norm(A,3) norm(A,100) norm(A,inf)]
B =
2.2361 3.0000 2.2361 2.0801 2.0000 2.0000
pp
ipxx
1
>> C = fix(10*rand(3,2))
C =
9 1 4 4 8 9
>> B=[norm(C,1) norm(C) norm(C,inf)]
B =
21.0000 15.2147 17.0000
A=LU>> A=magic(4)A =
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
>> [L U]=lu(A)
L =
1.0000 0 0 0 0.3125 0.7685 1.0000 0 0.5625 0.4352 1.0000 1.0000 0.2500 1.0000 0 0
U =
16.0000 2.0000 3.0000 13.0000 0 13.5000 14.2500 -2.2500 0 0 -1.8889 5.6667 0 0 0 0.0000
QR Factorization
A=QR>> A=[1 4 2;3 5 7;3 5 1]
A = 1 4 2 3 5 7 3 5 1
>> [Q R]=qr(A)
Q =
-0.2294 0.9733 -0.0000 -0.6882 -0.1622 -0.7071 -0.6882 -0.1622 0.7071
R =
-4.3589 -7.8001 -5.9648 0 2.2711 0.6489 0 0 -4.2426
>> Q'*Q
ans =
1.0000 -0.0000 -0.0000 -0.0000 1.0000 -0.0000 -0.0000 -0.0000 1.0000
132
216
932
zyx
zyx
zyx
1
21
9
321
161
132
z
y
x
1
21
9
321
161
132
bA
1
21
9
321
161
1321
z
y
x
A =
2 3 -1 1 6 1 -1 -2 3
>> b=[9;21;-1]
b =
9 21 -1
>> inv(A)
ans =
0.8333 -0.2917 0.3750 -0.1667 0.2083 -0.1250 0.1667 0.0417 0.3750
>> inv(A)
ans =
0.8333 -0.2917 0.3750 -0.1667 0.2083 -0.1250 0.1667 0.0417 0.3750
>> inv(A)*b
ans =
1 3 2
2
3
1
z
y
x
>> 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 =
-1 2 -1
>> f = inline('x^3 +x -1')
f =
Inline function: f(x) = x^3 +x -1
>> f(4)
ans =
67
>> f(-2)
ans =
-11
>> f = inline('3*x.^2 -2*y', 'x', 'y');>> f(2,3)
ans =
6
>> f(3,3)
ans =
21
>> 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 =
1 2 3 4 5 6 7 8 9
>> B=f(A)
B =
468 577 686 1065 1309 1553 1662 2041 2420
>> x = -1:.4:1; y = 0:.4:4;>> [X,Y] = meshgrid(x,y)
X =
-1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000 -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000 -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000 -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000 -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000 -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000 -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000 -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000 -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000 -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000 -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000
Y =
0 0 0 0 0 0 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.8000 0.8000 0.8000 0.8000 0.8000 0.8000 1.2000 1.2000 1.2000 1.2000 1.2000 1.2000 1.6000 1.6000 1.6000 1.6000 1.6000 1.6000 2.0000 2.0000 2.0000 2.0000 2.0000 2.0000 2.4000 2.4000 2.4000 2.4000 2.4000 2.4000 2.8000 2.8000 2.8000 2.8000 2.8000 2.8000 3.2000 3.2000 3.2000 3.2000 3.2000 3.2000 3.6000 3.6000 3.6000 3.6000 3.6000 3.6000 4.0000 4.0000 4.0000 4.0000 4.0000 4.0000
>> 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 =
10.0000 3.6000 0.4000 0.4000 3.6000 10.0000 10.1600 3.7600 0.5600 0.5600 3.7600 10.1600 10.6400 4.2400 1.0400 1.0400 4.2400 10.6400 11.4400 5.0400 1.8400 1.8400 5.0400 11.4400 12.5600 6.1600 2.9600 2.9600 6.1600 12.5600 14.0000 7.6000 4.4000 4.4000 7.6000 14.0000 15.7600 9.3600 6.1600 6.1600 9.3600 15.7600 17.8400 11.4400 8.2400 8.2400 11.4400 17.8400 20.2400 13.8400 10.6400 10.6400 13.8400 20.2400 22.9600 16.5600 13.3600 13.3600 16.5600 22.9600 26.0000 19.6000 16.4000 16.4000 19.6000 26.0000
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)
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)
Questions? Discussion? Suggestions ?