Professional Documents
Culture Documents
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
Enjoythispage?Pleasepayitforward.Here'show...
http://www.matrixlabexamples.com/matrixinversion.html 3/3