0% found this document useful (0 votes)
13 views

Matlab Session 3

This document discusses matrices in Matlab. It shows how to create matrices filled with zeros and ones of varying sizes using functions like zeros, ones, and eye. It demonstrates how to create diagonal matrices using diag and transpose matrices using the apostrophe operator. It also provides an example of solving a system of 3 linear equations using the inv function to find the inverse of the coefficient matrix A.

Uploaded by

Amr Mahros
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Matlab Session 3

This document discusses matrices in Matlab. It shows how to create matrices filled with zeros and ones of varying sizes using functions like zeros, ones, and eye. It demonstrates how to create diagonal matrices using diag and transpose matrices using the apostrophe operator. It also provides an example of solving a system of 3 linear equations using the inv function to find the inverse of the coefficient matrix A.

Uploaded by

Amr Mahros
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Pharos University in Alexandria (PUA) Faculty of Engineering

Subject Name: BE 101 (Engineering Math I)


Lecturer(s): Dr. Sherif Rabia , Dr. Marwa Tharwat

Matlab Session No.3

Matrices
Matlab has a number of built-in routines to help create matrices. You can create a matrix of zeros of any size.
>> A=zeros(5) A= 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

>> B=zeros(3,5) B= 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

You easily can create a matrix of zeros that is the same size as a given matrix.
>> C=[1 2 3;4 5 6;7 8 9] C=

1 4 7

2 5 8

3 6
9

>>D=zeros(size(C)) D= 0 0 0 0 0 0 0 0 0

You can create matrices of ones in a similar manner.


>> A=ones(6) A= 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

1 1 1 1

1 1 1 1

Page | 1 of (3)

1 1

1 1

1 1

1 1

1 1

1 1

>> B=ones(2,10) B= 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

>>D=ones(size(C)) D= 1 1 1 1 1 1 1 1 1

The identity matrix has ones on its main diagonal and zeros everywhere else.3
>> I=eye(5)

Other types of diagonal matrices are possible with Matlabs diag command. >> E=diag([1,2,3,4,5]) E=

>> F=diag([1,2,3,4,5],-1) F= >> G=diag(1:5,1) G=

The transpose operator (single apostrophe) plays the same role with matrices as it does with vectors. Rows are changed to columns and columns are changed to rows. >> J=[1 2 3;4 5 6;7 8 9] >> J'

Two online calculators and solvers for systems of 2 by 2 and 3 by 3 linear equations. 2 by 2 systems of linear equations are of the form ax+by=c dx+ey=f A*Z=B Page | 2 of (3)

Where Z = A-1*B

Ex: Solve The system of equations x1 + 2x2 3x3 = 4 2x1 3x3 = 2 x2 + x3 = 0 solution
>>A = [1 2 -3;2 0 -3;0 1 1] A= 3- 2 3- 0 1 1 1 2 0

>>B= [ 4;-2;0] B= 4 20 >>Z = inv(A)*B Z= 3.14231.42.1 -1.4286

Page | 3 of (3)

You might also like