0% found this document useful (0 votes)
130 views3 pages

Matrix Inversion in Matlab - Gauss-Jordan Method

This document describes a program that performs matrix inversion using a Gauss-Jordan elimination method. The program takes a square input matrix and transforms it into the identity matrix through row operations, simultaneously performing the same row operations on an identity matrix to obtain the inverse matrix. The program is exposed as a function that returns the inverse matrix. Examples are provided comparing the output to Matlab's built-in inverse function.

Uploaded by

anon020202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views3 pages

Matrix Inversion in Matlab - Gauss-Jordan Method

This document describes a program that performs matrix inversion using a Gauss-Jordan elimination method. The program takes a square input matrix and transforms it into the identity matrix through row operations, simultaneously performing the same row operations on an identity matrix to obtain the inverse matrix. The program is exposed as a function that returns the inverse matrix. Examples are provided comparing the output to Matlab's built-in inverse function.

Uploaded by

anon020202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1/9/2017 MatrixInversioninMatlabGaussJordanmethod

MatrixInversion
Home

Welcome
MatrixmaniaBlog

Sitemap/Search

MatlabBooks

ForumsandHelp

Contact

Basics
QuickMatlabGuide

MatlabTutorial Thisprogramperformsthe
matrixinversionofasquare
MatlabExamples matrixstepbystep.The
inversionisperformedbya
MatlabFlow modifiedGaussJordan
eliminationmethod.Westart
BooleanLogic withanarbitrarysquarematrix
andasamesizeidentitymatrix
PlotsandGUI (alltheelementsalongits
Matlab2DPlots diagonalare1).

Matlab3DPlots Weperformoperationsonthe
rowsoftheinputmatrixin
MatlabGUI ordertotransformitandobtain
anidentitymatrix,and
Applications
performexactlythesameoperationsontheaccompanyingidentitymatrixin
Calculus
ordertoobtaintheinverseone.
LinearAlgebra
Ifwefindarowfullofzerosduringthisprocess,thenwecanconcludethat
thematrixissingular,andsocannotbeinverted.
MatlabCookbookI

MatlabCookbookII We'reexposingaverynaivemethod,justaswasperformedintheoldBasic
style.Naturally,Matlabhasappropriateandfastinstructionstoperform
ElectricalCalc matrixinversions(aswith'inv'or'\',forexample),butwewanttoexplain
theGaussJordanconceptandshowhownestedloopsandcontrolflowwork.
ProbabandStats

FinanceApps First,wedevelopafunctionlikethis(let'sassumewesaveitas
'mat_inv2.m'):
Other
OnlineCalculators
functionb=mat_inv2(a)
RelevantLinks
%Finddimensionsofinputmatrix
NotesonComp [r,c]=size(a)

Fun! %Ifinputmatrixisnotsquare,stopfunction
ifr~=c
Scilab disp('OnlySquareMatrices,please')
b=[]
YourownWebsite? return
end
Terms/Policies
%Targetidentitymatrixtobetransformedintotheoutput
%inversematrix
b=eye(r)

%Thefollowingcodeactuallyperformsthematrixinversionbyworking
%oneachelementoftheinput
forj=1:r
fori=j:r
ifa(i,j)~=0
fork=1:r
s=a(j,k)a(j,k)=a(i,k)a(i,k)=s
s=b(j,k)b(j,k)=b(i,k)b(i,k)=s
end
t=1/a(j,j)
fork=1:r
a(j,k)=t*a(j,k)

http://www.matrixlabexamples.com/matrixinversion.html 1/3
1/9/2017 MatrixInversioninMatlabGaussJordanmethod
b(j,k)=t*b(j,k)
end
forL=1:r
ifL~=j
t=a(L,j)
fork=1:r
a(L,k)=a(L,k)+t*a(j,k)
b(L,k)=b(L,k)+t*b(j,k)
end
end
end
end
break
end
%Displaywarningifarowfullofzerosisfound
ifa(i,j)==0
disp('Warning:SingularMatrix')
b='error'
return
end
end
%Showtheevolutionoftheinputmatrix,sothatwecan
%confirmthatitbecameanidentitymatrix.
a

Andthen,wecancallitortestitfromanyotherscriptorfromthecommand
window,likethis:

%Inputmatrix
a=[3514
14.73
0201
260.3];

%Callthefunctiontofinditsinverse
b=mat_inv2(a)

%ComparewitharesultgeneratedbyMatlab
c=inv(a)

Matlabproducesthisresponse:

First,weseehowtheoriginalmatrixwastransformedintoanidentity
matrix:

a=
1000
0100
0010
0001

Then,ourfunctionshowsitsresult:

b=
0.65440.93480.19120.0142
0.19830.28330.10340.1558
0.36831.95474.26350.4249
0.39660.56660.79320.3116

Finally,thisistheinversionproducedbyaninstructionfromMatlab(inv(a)):

c=
0.65440.93480.19120.0142
0.19830.28330.10340.1558
0.36831.95474.26350.4249
0.39660.56660.79320.3116

Anotherexample:

%Inputmatrix
a=[11
11]

%Callthefunctiontofinditsinverse
b=mat_inv2(a)

%ComparewitharesultgeneratedbyMatlab
c=inv(a)

http://www.matrixlabexamples.com/matrixinversion.html 2/3
1/9/2017 MatrixInversioninMatlabGaussJordanmethod
AndMatlabdisplayis:

Warning:SingularMatrix
b=
error

Warning:Matrixissingulartoworkingprecision.
>Intest_mat_invat42

c=
InfInf
InfInf

Inthiscase,ouralgorithmfoundasingularmatrix,soaninversecannotbe
calculated.ThisagreeswithwhatMatlabfoundwithitsownbuiltinfunction.

IfyouareinterestedinaModifiedGaussJordanAlgorithm,youcanseethis
article.

From'MatrixInversion'tohome

From'MatrixInversion'to'LinearAlgebra'

ExamplesonFlowControl

Top

FlowControl

Sharethispage: Facebook Twitter Google


Whatsthis?
Pinterest Tumblr Reddit

Enjoythispage?Pleasepayitforward.Here'show...

http://www.matrixlabexamples.com/matrixinversion.html 3/3

You might also like