numerical project refrence report

49
Project Report Page 1 of 49 Linear Algebra Matlab Final Project Spring 2006 National University of Computer and Emerging Science Islamabad

Upload: muhammad-rizwan

Post on 22-May-2015

505 views

Category:

Education


0 download

DESCRIPTION

Numerical project Refrence report is for your help, to understand how you should have to documented your project

TRANSCRIPT

Page 1: NUmerical Project Refrence Report

Project Report

Page 1 of 49

Linear Algebra Matlab Final Project

Spring 2006

National University of Computer and Emerging Science

Islamabad

Page 2: NUmerical Project Refrence Report

Project Report

Page 2 of 49

Submitted to: Sir Sher Afzal Khan

Submitted BY:

Muhammad Rizwan----(060388) Kamran Ali-----------------(060377) Mansoor Ahmed-------- (060392)

Page 3: NUmerical Project Refrence Report

Project Report

Page 3 of 49

Matrix Algebra ...................................................................................................................................... 5

Addition of two Matrices .................................................................................................................. 5 Algorithm: ..................................................................................................................................... 6 Input: ............................................................................................................................................. 6 Two matrices A and B. ................................................................................................................. 6 Code .............................................................................................................................................. 7 Output ........................................................................................................................................... 8

Subtraction of two Matrices .............................................................................................................. 9 Algorithm: ................................................................................................................................... 10 Input: ........................................................................................................................................... 10 Two matrices A and B. ............................................................................................................... 10 Code ............................................................................................................................................ 10 Output ......................................................................................................................................... 11

Multiplication of two Matrices ....................................................................................................... 12 Algorithm .................................................................................................................................... 13 Input ............................................................................................................................................ 13 Code ............................................................................................................................................ 13 Output ......................................................................................................................................... 15

Transpose of a Matrix ..................................................................................................................... 16 Algorithm .................................................................................................................................... 17 Input ............................................................................................................................................ 17 Matrix A. ..................................................................................................................................... 17 Code ............................................................................................................................................ 17 Output ......................................................................................................................................... 18

Transformation .................................................................................................................................... 18 Reflection through the x-axis .......................................................................................................... 18

Algorithm .................................................................................................................................... 19 Input ............................................................................................................................................ 19 Code ............................................................................................................................................ 19 Output ......................................................................................................................................... 20

Reflection through the y-axis .......................................................................................................... 20 Algorithm .................................................................................................................................... 20 Input ............................................................................................................................................ 20 Code ............................................................................................................................................ 21 Output ......................................................................................................................................... 21

Reflection through the line y = x .................................................................................................... 22 Algorithm .................................................................................................................................... 22 Input ............................................................................................................................................ 22 Code ............................................................................................................................................ 23 Output ......................................................................................................................................... 23

Reflection through the line y = -x ................................................................................................... 23 Algorithm .................................................................................................................................... 24 Input ............................................................................................................................................ 24 Code ............................................................................................................................................ 24 Output ......................................................................................................................................... 25

Reflection through the origin .......................................................................................................... 25

Page 4: NUmerical Project Refrence Report

Project Report

Page 4 of 49

Algorithm .................................................................................................................................... 25 Input ............................................................................................................................................ 25 Code ............................................................................................................................................ 26 Output ......................................................................................................................................... 26

Rotation ........................................................................................................................................... 27 Algorithm .................................................................................................................................... 27 Input ............................................................................................................................................ 27 Code ............................................................................................................................................ 28 Output ......................................................................................................................................... 28

Horizontal Contraction and Expansion ........................................................................................... 28 Algorithm: ................................................................................................................................... 29 Input ............................................................................................................................................ 29 Code ............................................................................................................................................ 29 Output ......................................................................................................................................... 30

Vertical Contraction and Expansion ............................................................................................... 30 Algorithm .................................................................................................................................... 31 Input ............................................................................................................................................ 31 Code ............................................................................................................................................ 31 Output ......................................................................................................................................... 32

Horizontal Shear ............................................................................................................................. 32 Algorithm .................................................................................................................................... 32 Input ............................................................................................................................................ 32 Code ............................................................................................................................................ 33 Output ......................................................................................................................................... 33

Vertical Shear .................................................................................................................................. 33 Algorithm .................................................................................................................................... 34 Input ............................................................................................................................................ 34 Code ............................................................................................................................................ 34 Output ......................................................................................................................................... 34

Cryptology .......................................................................................................................................... 35 Encryption and Decryption ............................................................................................................. 35

Algorithm .................................................................................................................................... 35 Input ............................................................................................................................................ 35 Code ............................................................................................................................................ 35 Output ......................................................................................................................................... 36

Clock ................................................................................................................................................... 36 Working .......................................................................................................................................... 36

Input ............................................................................................................................................ 37 Code ............................................................................................................................................ 37 Output ......................................................................................................................................... 38

System of Linear equations ................................................................................................................. 38 Jacobi’s Method .............................................................................................................................. 38

Algorithm .................................................................................................................................... 39 Input ............................................................................................................................................ 39 Code ............................................................................................................................................ 39 Output ......................................................................................................................................... 41

Gauss Seidel’s Method ................................................................................................................... 45 Algorithm .................................................................................................................................... 45 Input ............................................................................................................................................ 45

Page 5: NUmerical Project Refrence Report

Project Report

Page 5 of 49

Code ............................................................................................................................................ 45 Output ......................................................................................................................................... 47

Matrix Algebra Algebra is the branch of mathematical in which, the mathematical operators +,-,*, etc. are involved. Matrix Algebra is the branch of mathematics in which algebraic expressions involved.

Addition of two Matrices Let A is an mxn matrix and B is pxq matrix. B can be added to A only when order of A and order of B are same, in other words m=p and n=q. (A & B are equivalent) Let aij is the (i,j)th entry of A and bij is (i,j)th entry of B. Let Matrix C is the addition of Matrices A & B, then (i,j)th entry of C is cij and cij=aij + bij. where i=1,2…m j=1,2,…n. Example A = 1 2 3 4 B = 4 5 6 7 C=A+B

Page 6: NUmerical Project Refrence Report

Project Report

Page 6 of 49

C = 6 8 10 12

Algorithm:

Input:

Two matrices A and B. Body of algorithm: a= Input Enter the rows of A b= Input Enter the columns of A for i=1 to number of rows of A for j=1 to number of columns of A A(i,j)=Input Enter the array entries of A End for j End for i Display Matrix A c= Input Enter the rows of B d= Input Enter the columns of B for i=1 to number of rows of B for j=1 to number of columns of B B(i,j)=Input Enter the array entries of B End for j End for i Display Matrix B if Size of A ==Size of B Matrix Addition is possible for i=1 to number of rows of A for j=1 to number of columns of B C(i,j)=A(i,j)+B(i,j); End for j End for i Display Matrix C else Matrix Addition is not possible End for if

Page 7: NUmerical Project Refrence Report

Project Report

Page 7 of 49

Code clc clear disp('matrix A') a= input ('enter the rows of A='); b= input ('enter the columns of A='); for i=1:a; for j=1:b; disp(['enter the entry of Row ' int2str(i) ' Column ' int2str(j)]); A(i,j)=input ('enter the entries of A ='); end end disp('press any key to see the matrix'),pause clc (A) disp('Matrix B') c=input('enter the rows of B='); d=input('enter the columns of B='); for i=1:c; for j=1:d; disp(['enter the entry of Row ' int2str(i) ' Column ' int2str(j)]); B(i,j)=input('enter the entries of B='); end end disp('press any key to see the matrix'),pause clc disp('Matrix A') (A) disp('Matrix B') (B) if a==c & b==d disp('Matrix Addition is pissible'); for i=1:a; for j=1:b; C(i,j)=A(i,j)+B(i,j); end end disp('press any key to see the Matrix C'),pause clc disp('Matrix C=A+B') disp('Matrix C') (C) else disp('Matrix Addition is not possiblle') disp('Because size of both matrices are not same') end disp('press any key to End'),pause

Page 8: NUmerical Project Refrence Report

Project Report

Page 8 of 49

clc

Output matrix A enter the rows of A=2 enter the columns of A=2 enter the entry of Row 1 Column 1 enter the entries of A =1 enter the entry of Row 1 Column 2 enter the entries of A =2 enter the entry of Row 2 Column 1 enter the entries of A =3 enter the entry of Row 2 Column 2 enter the entries of A =4 press any key to see the matrix ----------------------------------------------------------- A = 1 2 3 4 Matrix B enter the rows of B=2 enter the columns of B=2 enter the entry of Row 1 Column 1 enter the entries of B=5 enter the entry of Row 1 Column 2 enter the entries of B=6 enter the entry of Row 2 Column 1 enter the entries of B=7 enter the entry of Row 2 Column 2 enter the entries of B=8 press any key to see the matrix ---------------------------------------- Matrix A A = 1 2 3 4 Matrix B B = 5 6 7 8

Page 9: NUmerical Project Refrence Report

Project Report

Page 9 of 49

Matrix Addition is pissible press any key to see the Matrix C ------------------------------------------- Matrix C=A+B Matrix C C = 6 8 10 12 press any key to End --------------------------------------------

Subtraction of two Matrices Subtraction of matrices is same as the addition of matrices. If we want to subtract B from A then we multiply B with -1 and add it to A Let A is an mxn matrix and B is mxn matrix. We can find the subtraction A and B by Adding A and –B. A+ (-B) =A-B. We can find –B by multiplying B with a constant -1 as (-1) B= -B Example A = 1 2 3 4 B = 4 5 6 7 C=A-B C = -3 -3 -3 -3

Page 10: NUmerical Project Refrence Report

Project Report

Page 10 of 49

Algorithm:

Input:

Two matrices A and B. Body of algorithm: a= Input Enter the rows of A b= Input Enter the columns of A for i=1 to number of rows of A for j=1 to number of columns of A A(i,j)=Input Enter the array entries of A End for j End for i Display Matrix A c= Input Enter the rows of B d= Input Enter the columns of B for i=1 to number of rows of B for j=1 to number of columns of B B(i,j)=Input Enter the array entries of B End for j End for i Display Matrix B B=-1*B; if Size of A ==Size of B Matrix Addition is possible for i=1 to number of rows of A or B for j=1 to number of columns of A or B C(i,j)=A(i,j)+B(i,j); End for j End for i Display Matrix C else Matrix Addition is not possible End for if

Code clc clear disp('matrix A') a= input ('enter the rows of A='); b= input ('enter the columns of A='); for i=1:a; for j=1:b; disp(['enter the entry of Row ' int2str(i) ' Column ' int2str(j)]); A(i,j)=input ('enter the entries of A =');

Page 11: NUmerical Project Refrence Report

Project Report

Page 11 of 49

end end disp('press any key to see the matrix'),pause clc (A) disp('Matrix B') c=input('enter the rows of B='); d=input('enter the columns of B='); for i=1:c; for j=1:d; disp(['enter the entry of Row ' int2str(i) ' Column ' int2str(j)]); B(i,j)=input('enter the entries of B='); end end disp('press any key to see the matrix'),pause clc disp('Matrix A') (A) disp('Matrix B') (B) if a==c & b==d disp('Matrix subtraction is pissible'); B=-1*B; for i=1:a; for j=1:b; C(i,j)=A(i,j)+B(i,j); end end disp('press any key to see the Matrix C'),pause clc disp('Matrix C=A+(-B)=A-B') disp('Matrix C') (C) else disp('Matrix subtraction is not possiblle') disp('Because size of both matrices are not same') end disp('press any key to End'),pause clc

Output matrix A enter the rows of A=2 enter the columns of A=2 enter the entry of Row 1 Column 1 enter the entries of A =1 enter the entry of Row 1 Column 2 enter the entries of A =2 enter the entry of Row 2 Column 1

Page 12: NUmerical Project Refrence Report

Project Report

Page 12 of 49

enter the entries of A =3 enter the entry of Row 2 Column 2 enter the entries of A =4 press any key to see the matrix A = 1 2 3 4 Matrix B enter the rows of B=2 enter the columns of B=2 enter the entry of Row 1 Column 1 enter the entries of B=4 enter the entry of Row 1 Column 2 enter the entries of B=5 enter the entry of Row 2 Column 1 enter the entries of B=6 enter the entry of Row 2 Column 2 enter the entries of B=7 press any key to see the matrix Matrix C=A+(-B)=A-B Matrix C C = -3 -3 -3 -3 press any key to End

Multiplication of two Matrices Let A is an mxn matrix and B is pxq matrix. B can be multiply with A only when number of columns of A are same as the number of rows of B, in other words n=q. The order of resulting matrix is m x q. Let aij is the (i,j)th entry of A and bij is (i,j)th entry of B. Let Matrix C is the product of Matrices A & B, then (i,j)th entry of C is cij and cij=ai1b1j + ai2b2j + a13b3j…… an1 bnj. Where i =1,2…m j=1,2,…n. Example A = 1 2 3 4 B = 5 6

Page 13: NUmerical Project Refrence Report

Project Report

Page 13 of 49

7 8 Matrix C=A*B C= 14 16 28 32

Algorithm

Input Two Matrices A and B Body of algorithm: a= Input Enter the rows of A b= Input Enter the columns of A for i=1 to number of rows of A for j=1 to number of columns of A A(i,j)=Input Enter the array entries of A End for j End for i Display Matrix A c= Input Enter the rows of B d= Input Enter the columns of B for i=1 to number of rows of B for j=1 to number of columns of B B(i,j)=Input Enter the array entries of B End for j End for i Display Matrix B if number of coloumns of A == number of rows of B Matrix multiplication is possible for i=1 to number of rows of A for j=1to number of coloumns of B for t=1 to number of coloumns of A or number of rows of B C(i,j)=0; C(i,j)=C(i,j)+A(i,t)*B(t,j); End for t End for j End for i Display matrix C else Matrix multiplication is not possible End for if

Code clc

Page 14: NUmerical Project Refrence Report

Project Report

Page 14 of 49

clear disp('matrix A') a= input ('enter the rows of A='); b= input ('enter the columns of A='); for i=1:a; for j=1:b; disp(['enter the entry of Row ' int2str(i) ' Column ' int2str(j)]); A(i,j)=input ('enter the entry '); end end disp('press any key to see the matrix'),pause clc disp('Matrix A') (A) disp('matrix B') c= input ('enter the rows of B='); d= input ('enter the columns of B='); for i=1:c; for j=1:d; disp(['enter the entry of Row ' int2str(i) ' Column ' int2str(j)]); B(i,j)=input ('enter the entry '); end end disp('press any key to see the matrix'),pause clc disp('Matrix A') (A) disp('Matrix B') disp(B) if b==c; disp('Matrix multiplication is possible') for i=1:a; for j=1:d for t=1:b C(i,j)=0; C(i,j)=C(i,j)+A(i,t)*B(t,j); end end end disp('press any key to see the Matrix C'),pause clc disp('Matrix C=A*B') disp(C) else disp('Matrix multiplication is not possible') disp('Because it does not satisfy the order conditions') end disp('press any key to End'),pause clc

Page 15: NUmerical Project Refrence Report

Project Report

Page 15 of 49

Output matrix A enter the rows of A=2 enter the columns of A=2 enter the entry of Row 1 Column 1 enter the entry 1 enter the entry of Row 1 Column 2 enter the entry 2 enter the entry of Row 2 Column 1 enter the entry 3 enter the entry of Row 2 Column 2 enter the entry 4 press any key to see the matrix ------------------------------- Matrix A A = 1 2 3 4 matrix B enter the rows of B=2 enter the columns of B=2 enter the entry of Row 1 Column 1 enter the entry 5 enter the entry of Row 1 Column 2 enter the entry 6 enter the entry of Row 2 Column 1 enter the entry 7 enter the entry of Row 2 Column 2 enter the entry 8 press any key to see the matrix --------------------------------------- Matrix A A = 1 2 3 4 Matrix B 5 6 7 8 Matrix multiplication is possible press any key to see the Matrix C ------------------------------------------ Matrix A

Page 16: NUmerical Project Refrence Report

Project Report

Page 16 of 49

A = 1 2 3 4 Matrix B 5 6 7 8 Matrix multiplication is possible press any key to see the Matrix C --------------------------------------- Matrix C=A*B 14 16 28 32 press any key to End ---------------------------------

Transpose of a Matrix Let A is an mxn matrix and B is an nxm matrix. Let aij is the (i,j)th entry of A and Let bij= aji is the (i,j)th entry of B. Then we can say that B is the transpose matrix of A.and it is written as B=A’. Example Matrix A A = 1 2 3 4 At is transpose of A Transpose of A=At At = 1 3 2 4

Page 17: NUmerical Project Refrence Report

Project Report

Page 17 of 49

Algorithm

Input

Matrix A. Body of algorithm: a= Input Enter the rows of A b= Input Enter the columns of A for i=1 to number of rows of A for j=1 to number of columns of A A(i,j)=Input Enter the array entries of A End for j End for i Display Matrix A Let At is the transpose of matrix A for i=1 to number of coloumns of A for j=1 to number of rows of A At(i,j)=A(j,i); End for j End end i Display matrix At

Code clc clear disp('matrix A') a= input ('enter the rows of A='); b= input ('enter the columns of A='); for i=1:a; for j=1:b; disp(['enter the entry of Row ' int2str(i) ' Column ' int2str(j)]); A(i,j)=input ('enter the entry='); end end disp('press any key to see the matrix'),pause clc disp('Matrix A') (A) disp('press any key to see the transpose of A'),pause for i=1:b; for j=1:a; At(i,j)=A(j,i); end end disp('Transpose of A=At')

Page 18: NUmerical Project Refrence Report

Project Report

Page 18 of 49

(At) disp('press any key to End'),pause clc

Output matrix A enter the rows of A=2 enter the columns of A=2 enter the entry of Row 1 Column 1 enter the entry=1 enter the entry of Row 1 Column 2 enter the entry=2 enter the entry of Row 2 Column 1 enter the entry=3 enter the entry of Row 2 Column 2 enter the entry=4 press any key to see the matrix --------------------------------------- Matrix A A = 1 2 3 4 press any key to see the transpose of A ----------------------------------------- Transpose of A=At At = 1 3 2 4 press any key to End -------------------------------------------

Transformation In mathematics transformation mean when a matrix A as an object that “acts” on a vector v transform it into Av. Let v be in Rn and A is an mxn matrix then the transformation of v is Av and is in Rn. Generally we denoted it as T:Rm→Rn and T(v) = Av.

Reflection through the x-axis Let v be vector in R2 and it is in first quadrant when it is reflected through the x-axis it is transformed in 3rd quadrant. To reflect a vector v through the x-axis we multiply it with A =

Page 19: NUmerical Project Refrence Report

Project Report

Page 19 of 49

1 0 0 -1 Example V = 1 2 Transformation about x axis T(V)=A*V T(V) = 1 -2

Algorithm

Input A vector with x and y co ordinates Body of algorithm x=Input Enter the x co ordinate y=Input Enter the y co ordinate V=(x , y) A = 1 0 0 -1 Display Transformation about x axis Draw the graph of original vector Draw the graph of transformed vector

Code x=input('enter the x=') ; y=input('enter the y='); V=[x;y] A=[1 0;0 -1]; disp('Transformationn about x axis') T=A*V set(gca, 'Xlim',[-15 15],'Ylim',[-15 15]) grid on line([0 V(1,1)],[0 V(2,1)],'color','b') line([0 T(1,1)],[0 T(2,1)],'color','r') disp('Blue vector is the original vector') disp('Red vector is the transformed vector')

Page 20: NUmerical Project Refrence Report

Project Report

Page 20 of 49

Output enter the x=1 enter the y=2 V = 1 2 Transformation about x axis T = 1 -2 Blue vector is the original vector Red vector is the transformed vector Press any key to exit

Reflection through the y-axis Let v be vector in R2 and it is in 1st quadrant when it is reflected through the y-axis it is transformed in 2nd quadrant. To reflect a vector v through the x-axis we multiply it with A = - 1 0 0 1 Example V = 1 2 Transformation about y axis T(V)=A*V T (V)= -1 2

Algorithm

Input

Page 21: NUmerical Project Refrence Report

Project Report

Page 21 of 49

A vector with x and y co ordinates Body of algorithm x=Input Enter the x co ordinate y=Input Enter the y co ordinate V=(x , y) A = - 1 0 0 1 Display Transformation about x axis Draw the graph of original vector Draw the graph of transformed vector

Code x=input('enter the x=') ; y=input('enter the y='); V=[x;y] A=[-1 0;0 1]; disp('Transformationn about y axis') %disp('press any key to view the transformation'),pause T=A*V set(gca, 'Xlim',[-15 15],'Ylim',[-15 15]) grid on line([0 V(1,1)],[0 V(2,1)],'color','b') line([0 T(1,1)],[0 T(2,1)],'color','r') disp('Blue vector is the original vector') disp('Red vector is the transformed vector')

Output enter the x=1 enter the y=2 V = 1 2 Transformation about y axis T = -1 2

Page 22: NUmerical Project Refrence Report

Project Report

Page 22 of 49

Blue vector is the original vector Red vector is the transformed vector Press any key to exit

Reflection through the line y = x Let v be vector in R2 and it is in first quadrant when it is reflected through the line y=x. Then y becomes x and x becomes y in v. To reflect a vector v through the line x=y we multiply it with A = 0 1 1 0 Example V = 1 2 Transformation about line y=x axis T(V)=A*V T (V)= 2 1

Algorithm

Input A vector with x and y co ordinates Body of algorithm x=Input Enter the x co ordinate y=Input Enter the y co ordinate V=(x , y) A = 0 1 1 0 Display Transformation about x axis Draw the graph of original vector Draw the graph of transformed vector

Page 23: NUmerical Project Refrence Report

Project Report

Page 23 of 49

Code x=input('enter the x=') ; y=input('enter the y='); V=[x;y] A=[0 1;1 0]; disp('Transformationn through line y=x') T=A*V set(gca, 'Xlim',[-15 15],'Ylim',[-15 15]) grid on line([0 V(1,1)],[0 V(2,1)],'color','b') line([0 T(1,1)],[0 T(2,1)],'color','r') disp('Blue vector is the original vector') disp('Red vector is the transformed vector')

Output enter the x=1 enter the y=2 V = 1 2 Transformationn through line y=x T = 2 1 Blue vector is the original vector Red vector is the transformed vector Press any key to exit

Reflection through the line y = -x Let v be vector in R2 and it is in first quadrant when it is reflected through the line y=-x. Then it is transformed into 3rd quadrant in other words y becomes -x and x becomes -y in v and To reflect a vector v through the line y=-x we multiply it with A = 0 - 1 - 1 0 Example V =

Page 24: NUmerical Project Refrence Report

Project Report

Page 24 of 49

1 2 Transformation about y=-x axis T(V)=A*V T (V)= -2 -1

Algorithm

Input A vector with x and y co ordinates Body of algorithm x=Input Enter the x co ordinate y=Input Enter the y co ordinate V=(x , y) A = 0 - 1 - 1 0 Display Transformation about x axis Draw the graph of original vector Draw the graph of transformed vector

Code x=input('enter the x=') ; y=input('enter the y='); V=[x;y] A=[0 -1;-1 0]; disp('Transformationn about y=-x') %disp('press any key to view the transformation'),pause T=A*V set(gca, 'Xlim',[-15 15],'Ylim',[-15 15]) grid on line([0 V(1,1)],[0 V(2,1)],'color','b') line([0 T(1,1)],[0 T(2,1)],'color','r') disp('Blue vector is the original vector') disp('Red vector is the transformed vector')

Page 25: NUmerical Project Refrence Report

Project Report

Page 25 of 49

Output enter the x=3 enter the y=4 V = 3 4 Transformationn about y=-x T = -4 -3 Blue vector is the original vector Red vector is the transformed vector Press any key to exit

Reflection through the origin Let v be vector in R2 and it is in first quadrant when it is reflected through the origin. Then it is transformed into 3rd quadrant. To reflect a vector v through the origin we multiply it with A = - 1 0 0 -1 Example V = 1 2 Transformation about origin axis T(V)=A*V T (V)= -2 -1

Algorithm

Input A vector with x and y co ordinates

Page 26: NUmerical Project Refrence Report

Project Report

Page 26 of 49

Body of algorithm x=Input Enter the x co ordinate y=Input Enter the y co ordinate V=(x , y) A = - 1 0 0 -1 Display Transformation about x axis Draw the graph of original vector Draw the graph of transformed vector

Code x=input('enter the x=') ; y=input('enter the y='); V=[x;y] disp('Transformationn about origin') A=[-1 0;0 -1]; T=A*V set(gca, 'Xlim',[-15 15],'Ylim',[-15 15]) grid on line([0 V(1,1)],[0 V(2,1)],'color','b') line([0 T(1,1)],[0 T(2,1)],'color','r') disp('Blue vector is the original vector') disp('Red vector is the transformed vector')

Output enter the x=5 enter the y=6 V = 5 6 Transformationn about origin T = -5 -6 Blue vector is the original vector Red vector is the transformed vector

Page 27: NUmerical Project Refrence Report

Project Report

Page 27 of 49

Press any key to exit

Rotation Let V be a vector in R2 we can rotate it through a an angle (a) and find a transformed vector T(V) To find a transformed vector T(V) we multiply V with matrix A,Matrix A is given below for Rotation. A = Cos a - Sin a Sin a Cos a Example V = 3 4 a = 450 Rotation T(V)=A*V T(V) = -0.7071 4.9497

Algorithm

Input A vector with x and y co ordinates Angle of rotation. Body of algorithm a=Input Angle of rotation change degree into radian A = Cos a - Sin a Sin a Cos a Transormation of V under T is below T=A*V Draw graph for original vector Draw graph for transformed vector

Page 28: NUmerical Project Refrence Report

Project Report

Page 28 of 49

Code

Output enter the x=3 enter the y=4 V = 3 4 About how many degree you want to rotate it 45 Given vector is V Transformation of V under T is below T = -0.7071 4.9497 Blue vector is the original vector Red vector is the transformed vector Press any key to exit

Horizontal Contraction and Expansion Let v be vector in R2 to contract or expand it horizontally we multiply it with A = k 0 contraction: if 0<k<1 0 1 expansion: if k>1 Example V = 3 4 Let k=2 T(V) =A*V T(V) = 6 4

Page 29: NUmerical Project Refrence Report

Project Report

Page 29 of 49

Algorithm:

Input A vector with x and y co ordinates A scalar k Body of algorithm for k=input. Enter the value of scalar k A = k 0 0 1 End for k T=A*V if k>0&k<1 display horizontal contraction else display horizontal expansion end for if Draw graph for original vector Draw graph for transformed vector

Code x=input('enter the x=') ; y=input('enter the y='); V=[x;y] for k=input('enter the value of k=') A=[k 0;0 1]; end disp('press any key to view the transformation'),pause T=A*V if k>0&k<1 disp('horizontal contraction') else disp('horizontal expansion') end set(gca, 'Xlim',[-15 15],'Ylim',[-15 15]) grid on line([0 V(1,1)],[0 V(2,1)],'color','b') line([0 T(1,1)],[0 T(2,1)],'color','r') disp('Blue vector is the original vector') disp('Red vector is the transformed vector')

Page 30: NUmerical Project Refrence Report

Project Report

Page 30 of 49

Output enter the x=3 enter the y=4 V = 3 4 enter the value of k=2 press any key to view the transformation T = 6 4 horizontal expansion Blue vector is the original vector Red vector is the transformed vector Press any key to exit

Vertical Contraction and Expansion Let v be vector in R2 to contract or expand it vertically we multiply it with A = 1 0 Contraction: if 0<k<1 0 k Expansion: if k>1 Example V = 3 4 Let k=2 T(V) =A*V T(V) =

Page 31: NUmerical Project Refrence Report

Project Report

Page 31 of 49

3 8

Algorithm

Input A vector with x and y co ordinates A scalar Body of algorithm for k=input. Enter the value of scalar k A = 1 0 0 k End for k T=A*V if k>0&k<1 display horizontal contraction else display horizontal expansion end for if Draw graph for original vector Draw graph for transformed vector

Code x=input('enter the x=') ; y=input('enter the y='); V=[x;y] for k=input('enter the value of k=') A=[1 0;0 k]; end T=A*[x;y] if k>0&k<1 disp('vertical contraction') else disp('vertical expansion') end set(gca, 'Xlim',[-15 15],'Ylim',[-15 15]) grid on line([0 V(1,1)],[0 V(2,1)],'color','b') line([0 T(1,1)],[0 T(2,1)],'color','r') disp('Blue vector is the original vector')

Page 32: NUmerical Project Refrence Report

Project Report

Page 32 of 49

disp('Red vector is the transformed vector')

Output V = 5 3 enter the value of k=2 T = 5 6 vertical expansion Blue vector is the original vector Red vector is the transformed vector Press any key to exit

Horizontal Shear Let V be a vector and T(V) be its transformed vector and it is horizontally sheared We can find T(V) by V→A*V and A is given below. A = 1 k 0 1

Algorithm

Input A vector with x and y co ordinates a scalar k Body of algorithm; Horizontal Shear for k=Input Enter the value of scalar k A = 1 k 0 1 End for For T=A*V Draw graph for original vector Draw graph for transformed vector

Page 33: NUmerical Project Refrence Report

Project Report

Page 33 of 49

Code x=input('enter the x=') ; y=input('enter the y='); V=[x;y] disp('Horizontal Shear') for k=input('enter the value of k=') A=[1 k;0 1]; end T=A*V set(gca, 'Xlim',[-15 15],'Ylim',[-15 15]) grid on line([0 V(1,1)],[0 V(2,1)],'color','b') line([0 T(1,1)],[0 T(2,1)],'color','r') disp('Blue vector is the original vector') disp('Red vector is the transformed vector')

Output enter the x=2 enter the y=1 V = 2 1 Horizontal Shear enter the value of k=2 T = 4 1 Blue vector is the original vector Red vector is the transformed vector Press any key to exit

Vertical Shear Let V be a vector and T(V) be its transformed vector and it is horizontally sheared We can find T(V) by V→A*V and A is given below.

Page 34: NUmerical Project Refrence Report

Project Report

Page 34 of 49

Algorithm

Input A vector with x and y co ordinates a scalar k Body of algorithm; Horizontal Shear for k=Input Enter the value of scalar k A = 1 0 k 1 End for For T=A*V Draw graph for original vector Draw graph for transformed vector

Code x=input('enter the x=') ; y=input('enter the y='); V=[x;y] disp('Vertical shear') for k=input('enter the value of k=') A=[1 0;k 1]; end T=A*V set(gca, 'Xlim',[-15 15],'Ylim',[-15 15]) grid on line([0 V(1,1)],[0 V(2,1)],'color','b') line([0 T(1,1)],[0 T(2,1)],'color','r') disp('Blue vector is the original vector') disp('Red vector is the transformed vector')

Output V = 5 4 Vertical shear enter the value of k=2

Page 35: NUmerical Project Refrence Report

Project Report

Page 35 of 49

T = 5 14 Blue vector is the original vector Red vector is the transformed vector Press any key to exit

Cryptology The study of Secret Message is called cryptology.

Encryption and Decryption Encryption In this process we transformed a message into a secret message. Decryption In this process we transformed a secret message into an original message.

Algorithm

Input A messge to encrypt and decrypt Body of Algorithm Msg = Input message into a string N=length of string Key = Anxn Encrypted message = Key*(Msg'); Decrypted Message = inv(Key)*Encrypted message.

Code clc msg = input('Enter a message: ','s') clc X=input('Enter your personal I.D'); n = length(msg); Key = rand(n,n); EM = Key*(msg'); a=char(round(EM)); disp('press any key to see the encrypted message'),pause EncryptedMessage= transpose(a) disp('press any key to see the decrypted message'),pause Key1=Key; DEM = inv(Key1)*EM; w=char(round(DEM));

Page 36: NUmerical Project Refrence Report

Project Report

Page 36 of 49

clc Y=input('Enter your personal I.D'); if X==Y; disp('Press any key to see the decrypted message'),pause DecryptenMessage=transpose(w) else disp('Your I.D is incorrect') end disp('Press any key to exit'),pause clc

Output Enter a message:My Name Is Rizwan msg = My Name Is Rizwan Press any kee to see Encrypted Message ------------------------------------------------ Your ID must be in the form of numeric data Before seen it Enter your personal ID= 123 Press any kee to see Encrypted Message ---------------------------------------------- EncryptedMessage = ����������������� Press any kee to see Decrypted Message ----------------------------------------------- Before seen it Enter your personal ID= 123 Press any kee to see Dencrypted Message ------------------------------------------------ DecryptenMessage = My Name Is Rizwan Press any key to end ----------------------------------------------

Clock

Working Clock works with the rotation of seconds, minutes and hour hand. When A second hand completes a loop of 60 then a minute hand moves forword by one and when minute hand moves 60 times an hour hand moves forword by one but hour hand moves five times larger than second and minute hands.

Page 37: NUmerical Project Refrence Report

Project Report

Page 37 of 49

Input CPU Clock time

Code function clock close all clc prog=figure('name','Terminator Clock'); set(prog,'NumberTitle','off'); set(prog,'MenuBar','none'); set(prog,'color','w'); set(prog,'visible','on'); set(prog,'Resize','off'); A=linspace(0,2*pi,5); B=linspace(0,2*pi,13); x1=15*cos(A); y1=15*sin(A); x2=10*cos(B); y2=10*sin(B); plot(x1,y1,'k','linewidth',6) hold on plot(x2,y2,'k','linewidth',8) axis([-12 12 -12 12]) axis off axis equal set(gca,'XLim',[-12 12],'YLim',[-12 12]) grid off text(0,8,'12','FontSize',18,'HorizontalAlignment','center','color','k'); text(4.2,7,'1','FontSize',18,'HorizontalAlignment','center','color','k'); text(7,4,'2','FontSize',18,'HorizontalAlignment','center','color','k'); text(8.3,0,'3','FontSize',18,'HorizontalAlignment','center','color','k'); text(7,-4,'4','FontSize',18,'HorizontalAlignment','center','color','k'); text(4.2,-7,'5','FontSize',18,'HorizontalAlignment','center','color','k'); text(0,-8,'6','FontSize',18,'HorizontalAlignment','center','color','k'); text(-4.2,-7,'7','FontSize',18,'HorizontalAlignment','center','color','k'); text(-7,-4,'8','FontSize',18,'HorizontalAlignment','center','color','k'); text(-8.3,0,'9','FontSize',18,'HorizontalAlignment','center','color','k'); text(-4.2,7,'11','FontSize',18,'HorizontalAlignment','center','color','k'); text(-7,4,'10','FontSize',18,'HorizontalAlignment','center','color','k'); title(Date) K = clock(); for hr=K(1,4):24 for min=K(1,5):60 for sec=K(1,6):60 timar = timer('TimerFcn','a=15;','StartDelay',1); start(timar); Anglesec=pi/2-sec*pi/30; line([0 7*cos(Anglesec)],[0 7*sin(Anglesec)],'Color','k','linewidth',1.5); Anglemin=pi/2-min*pi/30;

Page 38: NUmerical Project Refrence Report

Project Report

Page 38 of 49

line([0 6.5*cos(Anglemin)],[0 6.5*sin(Anglemin)],'Color','k','linewidth',2.5,'marker','none'); Anglehr=pi/2-hr*pi/6; line([0 5*cos(Anglehr)],[0 5*sin(Anglehr)],'Color','k','linewidth',2.5); pause(1); line([0 7*cos(Anglesec)],[0 7*sin(Anglesec)],'Color','w','marker','none','linewidth',1.5); line([0 6.5*cos(Anglemin)],[0 6.5*sin(Anglemin)],'Color','w','linewidth',2.5,'marker','none'); line([0 5*cos(Anglehr)],[0 5*sin(Anglehr)],'Color','w','linewidth',2.5,'marker','none'); end K(1,:)=1; end end end

Output

System of Linear equations An equation in which the sum of the product of variables is one is called the linear equation. A system of linear equation can be solved by many methods. There are most important methods are here.

Jacobi’s Method Let a11x1+a12x2+a13x3 = b11 , a21x1+a22x2+a23x3 = b21 , a31x1+a32x2+a33x3 = b31 be a System of linear equation.

Page 39: NUmerical Project Refrence Report

Project Report

Page 39 of 49

Let it is diagonally dominant, then xi+1 = (b11-a12 yi -a13 zi)/a11. y i+1 = (b21-a21 xi -a23 zi)/a21. z i+1 = (b31-a31 xi -a32 yi)/a31. Initially use the value of x=y=z=0, for first iteration. For second iteration we use the values of first iteration and in the similar way going on until the values of x, y, z converges to particular values. If the are not converges then the solution is not exist. Note:-If systemis not diagonally dominant then it may not converge to a particular number

Algorithm

Input Matrix A and Vector B Body of Algorithm Input Matix A and vector B If A is square Matrix for i=1:a; xp(i)=0; display xp(i) =0 end t=input number of iterations for k=1:t; for i=1:a; x(i)=B(i,1); for j=1:b; if i~=j; x(i)=x(i)-(A(i,j)*xp(j)); end end x(i)=x(i)/A(i,i); end for p=1:a; xp(p)=x(p); end else display message System does not satisfied the conditions of *------ JACOBI METHOD -----* end

Code clc

Page 40: NUmerical Project Refrence Report

Project Report

Page 40 of 49

clear a=input('Enter the number of rows of A '); b=input('Enterthe number of coloumns of A '); disp('*********************************************'); disp('*********************************************'); disp('Enter the matrix of coefficient of diagonally dominant matrix'); disp('*********************************************'); for i=1:a; for j=1:b; disp(['enter the entry of Row ' int2str(i) ' Column ' int2str(j) ' of matrix A']); A(i,j)=input('Enter the entry '); end end (A) for i=1:a; disp(['enter the entry of Row ' int2str(i) ' Column 1 of cloloumn vector B']); B(i,1)=input('Enter the entry '); end (B) disp('press any key to continue'),pause clc if a==b; for i=1:a; xp(i)=0; disp(['Let initial values of x' int2str(i) '=0']) end t=input('How many number of iterations you want to proceed== '); for k=1:t; disp(['Iteration number is ' int2str(k)]); for i=1:a; x(i)=B(i,1); for j=1:b; if i~=j; x(i)=x(i)-(A(i,j)*xp(j)); end end x(i)=x(i)/A(i,i); disp(['Value of x' int2str(i) ' = ' num2str(x(i))]); end for p=1:a; xp(p)=x(p); end disp('press any key to see the next iteration'),pause

Page 41: NUmerical Project Refrence Report

Project Report

Page 41 of 49

clc end disp('*************************************************'); disp('*********************************************'); disp('*********************************************'); disp('The exact solution is below'); disp('*********************************************'); for i=1:a; disp(['x' int2str(i) ' = ' num2str(x(i))]); end disp('press any key to Continue'),pause clc disp('NOTE:--If your matrix is diagonally dominent and the solution is not exact Then you should to proceed it to further iteratoins'); disp('************************************************************************************************') disp('NOTE:--If your matrix is not diagonally dominent, Then the is system is not convergent and solution may be wrong'); else disp('*********************************************'); disp('*********************************************'); disp('System does not satisfied the conditions of *------ JACOBI METHOD -----* '); end disp('Press any key to End'),pause clc

Output Enter the number of rows of A 3 Enterthe number of coloumns of A 3 ********************************************* ********************************************* Enter the matrix of coefficient of diagonally dominant matrix ********************************************* enter the entry of Row 1 Column 1 of matrix A Enter the entry 5 enter the entry of Row 1 Column 2 of matrix A Enter the entry 1 enter the entry of Row 1 Column 3 of matrix A Enter the entry 1 enter the entry of Row 2 Column 1 of matrix A Enter the entry 1 enter the entry of Row 2 Column 2 of matrix A Enter the entry 7 enter the entry of Row 2 Column 3 of matrix A

Page 42: NUmerical Project Refrence Report

Project Report

Page 42 of 49

Enter the entry 1 enter the entry of Row 3 Column 1 of matrix A Enter the entry 1 enter the entry of Row 3 Column 2 of matrix A Enter the entry 1 enter the entry of Row 3 Column 3 of matrix A Enter the entry 3 A = 5 1 1 1 7 1 1 1 3 enter the entry of Row 1 Column 1 of cloloumn vector B Enter the entry 5 enter the entry of Row 2 Column 1 of cloloumn vector B Enter the entry 7 enter the entry of Row 3 Column 1 of cloloumn vector B Enter the entry 3 B = 5 7 3 press any key to continue --------------------------------- Let initial values of x1=0 Let initial values of x2=0 Let initial values of x3=0 How many number of iterations you want to proceed== 16 Iteration number is 1 Value of x1 = 1 Value of x2 = 1 Value of x3 = 1 press any key to see the next iteration ----------------------------- Iteration number is 2 Value of x1 = 0.6 Value of x2 = 0.71429 Value of x3 = 0.33333 press any key to see the next iteration ------------------------------------- Iteration number is 3 Value of x1 = 0.79048 Value of x2 = 0.86667 Value of x3 = 0.5619

Page 43: NUmerical Project Refrence Report

Project Report

Page 43 of 49

press any key to see the next iteration ------------------------------- Iteration number is 4 Value of x1 = 0.71429 Value of x2 = 0.8068 Value of x3 = 0.44762 press any key to see the next iteration -------------------------- Iteration number is 5 Value of x1 = 0.74912 Value of x2 = 0.83401 Value of x3 = 0.49297 press any key to see the next iteration ------------------------ Iteration number is 6 Value of x1 = 0.7346 Value of x2 = 0.82256 Value of x3 = 0.47229 press any key to see the next iteration ------------------------------ Iteration number is 7 Value of x1 = 0.74103 Value of x2 = 0.82759 Value of x3 = 0.48095 press any key to see the next iteration ------------------------- Iteration number is 8 Value of x1 = 0.73829 Value of x2 = 0.82543 Value of x3 = 0.47713 press any key to see the next iteration -------------------------------- Iteration number is 9 Value of x1 = 0.73949 Value of x2 = 0.82637 Value of x3 = 0.47876 press any key to see the next iteration ----------------------------------- Iteration number is 10 Value of x1 = 0.73897 Value of x2 = 0.82596 Value of x3 = 0.47805 press any key to see the next iteration --------------------------------- Iteration number is 11 Value of x1 = 0.7392 Value of x2 = 0.82614 Value of x3 = 0.47835 press any key to see the next iteration

Page 44: NUmerical Project Refrence Report

Project Report

Page 44 of 49

-------------------------------- Iteration number is 12 Value of x1 = 0.7391 Value of x2 = 0.82606 Value of x3 = 0.47822 press any key to see the next iteration --------------------------- Iteration number is 13 Value of x1 = 0.73914 Value of x2 = 0.8261 Value of x3 = 0.47828 press any key to see the next iteration ----------------------------- Iteration number is 14 Value of x1 = 0.73913 Value of x2 = 0.82608 Value of x3 = 0.47825 press any key to see the next iteration ------------------------------ Iteration number is 15 Value of x1 = 0.73913 Value of x2 = 0.82609 Value of x3 = 0.47826 press any key to see the next iteration ------------------------------- Iteration number is 16 Value of x1 = 0.73913 Value of x2 = 0.82609 Value of x3 = 0.47826 press any key to see the next iteration ---------------------------------- ************************************************* ********************************************* ********************************************* The exact solution is below ********************************************* x1 = 0.73913 x2 = 0.82609 x3 = 0.47826 press any key to Continue ----------------------------------- NOTE:--If your matrix is diagonally dominant and the solution is not exact Then you should to proceed it to further iterations ************************************************************************************************ NOTE:--If your matrix is not diagonally dominant, Then the is system is not convergent and solution may be wrong Press any key to End ------------------------------------

Page 45: NUmerical Project Refrence Report

Project Report

Page 45 of 49

Gauss Seidel’s Method Gauss elimination method is more better form it converge rapidly as compare to the Jacobi’s Method There is a little difference from Jacobi that to calculate the value of x1, we initially use y1 and z1 equal to zero. Similarly to calculate the value of y1, we initially use x1 and z1 equal to zero and to calculate the value of z1, we initially use x1 and y1 equal to zero.For next iteration use the values of x, y, z from previous iteration.

Algorithm

Input Matrix A and Vector B Body of Algorithm Input Matix A and vector B If A is square Matrix for i=2:a; x(i)=0; display x(i) =0 end t=input number of iterations for k=1:t; for i=1:a; x(i)=B(i,1); for j=1:b; if i~=j; x(i)=x(i)-(A(i,j)*x(j)); end end x(i)=x(i)/A(i,i); end for p=1:a; xp(p)=x(p); end else display message System does not satisfied the conditions of *------ Gauss Method -----* end

Code clc

Page 46: NUmerical Project Refrence Report

Project Report

Page 46 of 49

clear a=input('Enter the number of rows of A '); b=input('Enterthe number of coloumns of A '); disp('*********************************************'); disp('*********************************************'); disp('Enter the matrix of coefficient of diagonally dominant matrix'); disp('*********************************************'); for i=1:a; for j=1:b; disp(['enter the entry of Row ' int2str(i) ' Column ' int2str(j) 'of matrix A']); A(i,j)=input('Enter the entry '); end end (A) for i=1:a; disp(['enter the entry of Row ' int2str(i) ' Column 1 of cloloumn vector B']); B(i,1)=input('Enter the entry '); end (B) disp('press any key to continue'),pause clc if a==b; for i=1:a; x(i)=0; disp(['Let initial values of x' int2str(i) '=0']) end t=input('How many number of iterations you want to proceed '); for k=1:t; disp(['Iteration number is ' int2str(k)]); for i=1:a; x(i)=B(i,1); for j=1:b; if i~=j; x(i)=x(i)-(A(i,j)*x(j)); end end x(i)=x(i)/A(i,i); disp(['Value of x' int2str(i) ' = ' num2str(x(i))]); end disp('press any key to see the next iteration'),pause clc end disp('*************************************************'); disp('*********************************************'); disp('*********************************************');

Page 47: NUmerical Project Refrence Report

Project Report

Page 47 of 49

disp('The exact solution is below'); disp('*********************************************'); for i=1:a; disp(['x' int2str(i) ' = ' num2str(x(i))]); end disp('press any key to Continue'),pause clc disp('NOTE:--If your matrix is diagonally dominent and the solution is not exact Then you should to proceed it to further iteratoins'); disp('************************************************************************************************') disp('NOTE:--If your matrix is not diagonally dominent, Then the is system is not convergent and solution may be wrong'); else disp('*********************************************'); disp('*********************************************'); disp('System does not satisfied the conditions of *------ Gauss Elimination Method -----* '); end disp('Press any key to End'),pause clc

Output Enter the number of rows of A 3 Enterthe number of coloumns of A 3 ********************************************* ********************************************* Enter the matrix of coefficient of diagonally dominant matrix ********************************************* enter the entry of Row 1 Column 1of matrix A Enter the entry 5 enter the entry of Row 1 Column 2of matrix A Enter the entry 1 enter the entry of Row 1 Column 3of matrix A Enter the entry 1 enter the entry of Row 2 Column 1of matrix A Enter the entry 1 enter the entry of Row 2 Column 2of matrix A Enter the entry 7 enter the entry of Row 2 Column 3of matrix A Enter the entry 1 enter the entry of Row 3 Column 1of matrix A Enter the entry 1 enter the entry of Row 3 Column 2of matrix A Enter the entry 1 enter the entry of Row 3 Column 3of matrix A Enter the entry 3

Page 48: NUmerical Project Refrence Report

Project Report

Page 48 of 49

A = 5 1 1 1 7 1 1 1 3 enter the entry of Row 1 Column 1 of cloloumn vector B Enter the entry 5 enter the entry of Row 2 Column 1 of cloloumn vector B Enter the entry 7 enter the entry of Row 3 Column 1 of cloloumn vector B Enter the entry 3 B = 5 7 3 press any key to continue ------------------------ Let initial values of x1=0 Let initial values of x2=0 Let initial values of x3=0 How many number of iterations you want to proceed 7 Iteration number is 1 Value of x1 = 1 Value of x2 = 0.85714 Value of x3 = 0.38095 press any key to see the next iteration ----------------------------- Iteration number is 2 Value of x1 = 0.75238 Value of x2 = 0.8381 Value of x3 = 0.46984 press any key to see the next iteration ------------------------------- Iteration number is 3 Value of x1 = 0.73841 Value of x2 = 0.82739 Value of x3 = 0.47807 press any key to see the next iteration -------------------------------- Iteration number is 4 Value of x1 = 0.73891 Value of x2 = 0.82615 Value of x3 = 0.47831 press any key to see the next iteration

Page 49: NUmerical Project Refrence Report

Project Report

Page 49 of 49

-------------------------------- Iteration number is 5 Value of x1 = 0.73911 Value of x2 = 0.82608 Value of x3 = 0.47827 press any key to see the next iteration -------------------------------- Iteration number is 6 Value of x1 = 0.73913 Value of x2 = 0.82609 Value of x3 = 0.47826 press any key to see the next iteration ----------------------------- Iteration number is 7 Value of x1 = 0.73913 Value of x2 = 0.82609 Value of x3 = 0.47826 press any key to see the next iteration ---------------------------- ************************************************* ********************************************* ********************************************* The exact solution is below ********************************************* x1 = 0.73913 x2 = 0.82609 x3 = 0.47826 press any key to End -------------------------------