Solving systems of equations with matrices
2016-09-23 00:00
204 查看
1:SystemsOfEquations
Let'ssaythatwehavetwoships.Weknowthetotalvalueofthecargoineachship'shold,butwedon'tknowhowmucheachitemcosts.Therearetwodifferentkindsofitems--fusionreactors,andgiantrobots.Wecanrepresentfusionreactorswiththevariable
Wecanrepresentthismathematicallyasasystemofequations:
Thisisjustrewritingtheinformationwehadfrombefore--
Tosolvethis,wecanfirstmultiplythetopequationbytwo:
Thismakessense,becauseif,thenitmakessensethat--we'rejustdoublingeverything.
Thenextstepistosubtractthebottomequationfromthetopequation:
Thistellsusthat
Nowthatweknowthat
Whichsimplifiesto:
Andfinally:
So,fusionreactorsareworth
2:SystemsOfEquationsAsMatrices
We'veworkedabitwithmatricesbefore,andtheverycoolthingaboutsystemsofequationsisthatwecanrepresentthemthesameway.Ourequationsabovecouldberepresentedasamatrix:
Thisisjustamatrixthatcontainsthecoefficientsandconstantsintheequationswehave.Wecalledthisanaugmentedmatrix,becauseithasboththeconstantsandthecoefficients--thelineseparatesthem,withconstantstotheright.
Thismatrixhastworows(eachofwhichrepresentsanequationinthesystem),and3columns.Thefirstcolumnrepresents
Theunderpinningsoflinearalgebraaresystemsofequations,butwegenerallyworkwithmatriceslikethisbecausetheyaresimplertorepresent.
Thesimplestwaytorepresentamatrixinpythonisanumpyarray.Anumpyarraycanhaverowsandcolumns,justlikeamatrix.
Instructions
Multiplythefirstrowofthematrixbytwo.Thensubtractthesecondrowfromthefirstrow.
Then,subtractthreetimesthefirstrowfromthesecondrow.
Finally,dividethesecondrowby2togetridofthecoefficient.
Attheend,thefirstrowshouldindicatethat
importnumpyasnp
#Setthedtypetofloattodofloatmathwiththenumbers.
matrix=np.asarray([
[2,1,25],
[3,2,40]
],dtype=np.float32)
matrix[0]*=2
matrix[0]-=matrix[1]
matrix[1]-=(3*matrix[0])
matrix[1]/=2
3:Gauss'sMethod
WejustusedGauss'sMethodtosolvesystemsoflinearequations,butwedidn'texplicitlystatewhatitis.Gauss'smethodisprettysimple,andjuststatesthat:Thisishowwecantransformoursystemsofequationsthroughswappingandmultiplying,andsolvethesystem.It'sbecauseoursimplifiedrepresentationleadstothesamesolutionsasthemorecomplexinitialsystem.
4:SolvingMoreComplexEquations
Welookedataprettysimplesystemofequationsbeforehand,butwecanextendourmethodstoevensolvemorecomplexsystems.Inthissystem,wehavethreevariables(firstcolumnis
Thefirstthingwecandoissubtract
Thewecansubtractthefirstrowfromthethirdrow:
Then,wecansubtractthesecondrowfromthethirdrow:
Thistellsusthat
Thistellsusthat
Now,wehaveeverythingsolved!
Instructions
SolveforYoucanuse
importnumpyasnp
matrix=np.asarray([
[1,2,0,7],
[0,3,3,11],
[1,2,2,11]
],dtype=np.float32)
matrix[2]-=matrix[0]
matrix[2]/=2
matrix[1]-=(matrix[2]*3)
matrix[1]/=3
matrix[0]-=(2*matrix[1])
5:EchelonForm
Aleadingvariableisthefirstvariablewithanonzerocoefficientinarow.Echelonformiswhathappenswhentheleadingvariableofeachrowistotherightoftheleadingvariableinthepreviousrow.Anyrowsconsistingofallzerosshouldbeatthebottom.Here'sanexampleofamatrixrepresentingasystemoflinearequationsinechelonform:
⎡⎣⎢⎢1000501715104⎤⎦⎥⎥[1015057100014]
Here'sanotherexampleofamatrixinechelonform:
⎡⎣⎢⎢⎢⎢1000−150010205−2−50⎤⎦⎥⎥⎥⎥[1−115050−2002−50000]
Andhere'samatrixthatisn'tinechelonform:
⎡⎣⎢⎢1211−1−203−1033⎤⎦⎥⎥[11002−1331−2−13]
Gettingamatrixintoechelonformmakesasystemoflinearequationsmucheasiertosolve.Thisiswhererowswappingcancomeinhandy.
Instructions
Swaprowstogetmatrix=np.asarray([
[0,0,0,7],
[0,0,1,11],
[1,2,2,11],
[0,5,5,1]
],dtype=np.float32)
#Swapthefirstandthethirdrows-firstswap
matrix[[0,2]]=matrix[[2,0]]
#Secondswap
matrix[[1,3]]=matrix[[3,1]]
#Thirdswap
matrix[[2,3]]=matrix[[3,2]]
6:ReducedRowEchelonForm
Generally,thewaytosolvesystemsoflinearequationsistofirsttrytogetthemintoreducedrowechelonform.Wejustcoveredechelonform.reducedrowechelonformmeetsallthesameconditionsasechelonform,excepteveryleadingvariablemustequalHere'sanaugmentedmatrixinreducedrowechelonform.Notethatcoefficientsandconstantsaretreatedseparately,soconstantsdon'thavetofollowtheguidelinesforreducedrowechelonform:
⎡⎣⎢⎢100010001030⎤⎦⎥⎥[100001030010]
Here'saregularmatrixinreducedrowechelonform:
[1001][1001]
Generally,togettoreducedrowechelonform,werepeatthefollowingsteps:
Startonanewrow
Performanyneededswapstomovetheleftmostavailableleadingcoefficienttothecurrentrow
Dividetherowbyitsleadingcoefficienttomaketheleadingcoefficientequal
Subtracttherowfromallotherrows(withanappropriatemultiplier)toensurethatitsleadingvariableistheonlynonzerovalueinitscolumn.
Repeatuntilentirematrixisinreducedrow-echelonform.
Instructions
Thisstepisademo.Playaroundwithcodeoradvancetothenextstep.A=np.asarray([
[0,2,1,5],
[3,0,1,10],
[1,2,1,8]
],dtype=np.float32)
#First,we'llswapthesecondrowwiththefirsttogetanonzerocoefficientinthefirstcolumn
A[[0,1]]=A[[1,0]]
#Then,wedividethefirstrowby3togetacoefficientof1
A[0]/=3
#Now,weneedtomakesurethatour1coefficientistheonlycoefficientinitscolumn
#Wehavetosubtractthefirstrowfromthethirdrow
A[2]-=A[0]
#Now,wemovetorow2
#Wedivideby2togetaoneastheleadingcoefficient
A[1]/=2
#Wesubtract2timesthesecondrowfromthethirdtogetridofthesecondcolumncoefficientinthethirdrow
A[2]-=(2*A[1])
#Now,wecanmovetothethirdrow,butitalreadylooksgood.
#We'refinished,andoursystemissolved!
print(A)
7:Inconsistency(矩阵无解)
Notallsystemsofequationscanbesolved.Inthecaseswheretheycan't,wecallthesysteminconsistent.Aninconsistesystemwillhavetwoormoreequationsthatconflict,makingitimpossibletofindasolution.Here'sanexample:[844255][845425]
Wecandividethefirstrowby2:
[44222.55][422.5425]
There'snowaythat4x+2y=2.54x+2y=2.5and4x+2y=54x+2y=5,
sowewouldconsiderthisaninconsistentsystem.Inconsistentsystemshavenosolutions.
Instructions
FindwhetherAssign
Findwhether
Assign
A=np.asarray([
[10,5,20,60],
[3,1,0,11],
[8,2,2,30],
[0,4,5,13]
],dtype=np.float32)
B=np.asarray([
[5,-1,3,14],
[0,1,2,8],
[0,-2,5,1],
[0,0,6,6]
],dtype=np.float32)
#Dividefirstrowby10
A[0]/=10
#Subtract3timesthefirstrowfromthesecond
A[1]-=(3*A[0])
#Subtract8timesthefirstrowfromthethird
A[2]-=(8*A[0])
#Multiplythesecondrowby-2
A[1]*=-2
#Subtract.5timesthesecondrowfromthefirst
A[0]-=(A[1]*.5)
#Subtract-2timesthesecondrowfromthethird
A[2]-=(A[1]*-2)
#Subtract4timesthesecondrowfromthefourth
A[3]-=(A[1]*4)
#Dividethethirdrowby10
A[2]/=10
#Subtract-4timesthethirdrowfromthefirst
A[0]-=(A[2]*-4)
#Subtract12timesthethirdrowfromthesecond
A[1]-=(A[2]*12)
#Subtractthethirdrowtimes-43fromthefourth
A[3]-=(A[2]*-43)
#Aisinrowechelonform,andwehaveauniquesolution,soitisconsistent.
A_consistent=True
#Dividethefourthrowby6
B[3]/=6
#Subtract-2timesthesecondrowfromthethirdrow
B[2]-=(B[1]*-2)
#Dividethethirdrowby9
B[2]/=9
#Thelastvariable(z)cannotsimultaneouslyequal1.88and1,soBisinconsistent
B_consistent=False
8:InfiniteSolutions(无穷解)
We'veseencasesinwhichsystemsofequationshavezerosolutions,oronesolution.There'soneothercaseweneedtoworryabout:sometimes,systemshaveinfinitesolutions.Thisususallyhappenswhenwe'reunabletosimplifyanequationenoughthateachvariableisaloneinarow.Inthesecases,therearefreevariables,whichdonotleadanyrows,andleadingvariables,whichdo.Weexpressthevaluesoftheleadingvariablesusingthefreevariables.Here'sanexample:
[121−10303][11002−133]
Wecansimplify,andsubtracttwotimesthefirstrowfromthesecond:
[101−30303][11000−333]
Nowwe'restuck--we'reinechelonform,butwecan'tsimplifyanymore.
Westartwiththesecondrow,andget:
−3y+3z=3−3y+3z=3
y−z=−1y−z=−1
y=z−1y=z−1
So,wehaveexpressed
x+(z−1)=0x+(z−1)=0
x=1−zx=1−z
Thissystemofequationshasinfinitesolutions,because
Instructions
CheckwhetherIfitdoes,assign
Ifitdoesn't,
Checkwhether
Ifitdoes,assign
Ifitdoesn't,assign
A=np.asarray([
[2,4,8,20],
[4,8,16,40],
[20,5,5,10]
],dtype=np.float32)
B=np.asarray([
[1,1,1,4],
[3,-2,5,8],
[8,-4,5,10]
],dtype=np.float32)
#DividethefirstrowinAby2
A[0]/=2
#Subtractthefirstrowtimes4fromthesecondrow
#Thiszerosouttherow
A[1]-=(A[0]*4)
#Subtractthefirstrowtimes20fromthelastrow
A[2]-=(A[0]*20)
#Nowwe'restuck--wecan'tsimplifyAanymore,soithasinfinitesolutions
A_infinite=True
#B--Subtractthefirstrowtimes3fromthesecondrow
B[1]-=(B[0]*3)
#Subtractthefirstrowtimes8fromthethirdrow
B[2]-=(B[0]*8)
#Dividethesecondrowby-5
B[1]/=-5
#Subtractthesecondrowfromthefirsy
B[0]-=B[1]
#Subtractthesecondrowtimes-12fromthethirdrow
B[2]-=(B[1]*-12)
#Dividethelastrowby-7.8(thethirdcolumnelement)
B[2]/=B[2,2]
#Subtractthethirdrowtimesthethirdcolumnofthefirstrowfromthefirstrow
B[0]-=(B[2]*B[0][2])
#Subtractthethirdrowtimesthethirdcolumnofthesecondrowfromthesecondrow
B[1]-=(B[2]*B[1][2])
#Bissolveable,andhasasinglesolution--itisnotinfinite
B_infinite=False
9:Homogeneity
Alinearequationishomogeneousifithasaconstantofzero.Here'sanexample:4x+2y−4z=04x+2y−4z=0
Theseequationsarespecial,becausetheycanalwaysbesolvedbysettingthevalueofeachvariabletozero.
Asystemoflinearequationsishomogeneousifalltheequationshaveaconstantofzero.
⎡⎣⎢⎢1011−3−7034000⎤⎦⎥⎥[11000−3301−740]
Asystemofequationsthatishomogeneousalwayshasatleastonesolution--settingeachvariabletozerowillalwayssolvethesystem.
10:Singularity(奇点)
Amatrixissquareifithasthesamenumberofcolumnsasrows.Here'sasquarematrix:⎡⎣⎢1011−3−7034⎤⎦⎥[1100−331−74]
Asquarematrixisnonsingularifitrepresentsahomogenoussystemwithoneuniquesolution.Whenwerepresentahomegeneoussystem,wecanskipshowingthecoefficients,becausewecanassumethattheyarezero.Here'sanonsingularmatrix:
[1324][1234]
Thisisnonsingularbecausewhenwesimplifyit,wefindthat
[102−200][1200−20]
So,wehave−2y=0−2y=0,whichsimplifiestoy=0y=0.
[102100][120010]
Then,wecansubtracttwotimesthesecondrowfromthefirstrow:
[100100][100010]
Thistellsusthatboth
Asquarematrixissingularifitrepresentsahomogeneoussystemwithinfinitesolutions.Here'sanexample:
[1326][1236]
Thisissingularbecausewecan'tsimplifyitpastacertainpoint.Let'strythis.First,wesubtract
[102000][120000]
Wehavezeroedoutthewholesecondrow.Wecan'tsimplifythisanymore,soallwehaveisx+2y=0x+2y=0.Infinitesolutionswillworkforthis,includingx=0,y=0x=0,y=0,x=2,y=−1x=2,y=−1,andx=6,y=−3x=6,y=−3.
Theconceptofsingularitywillbeveryimportantdowntheline,aswelookintoinvertingmatrices.
相关文章推荐
- A Taste of AOP from Solving Problems with OOP and Design Patterns (Part I)
- A Taste of AOP from Solving Problems with OOP and Design Patterns (Part II)
- A Taste of AOP from Solving Problems with OOP and Design Patterns (Part III)
- A Taste of AOP from Solving Problems with OOP and Design Patterns (Part I)
- [导入]A Taste of AOP from Solving Problems with OOP and Design Patterns (Part I)
- A Taste of AOP from Solving Problems with OOP and Design Patterns (Part II)
- A Taste of AOP from Solving Problems with OOP and Design Patterns (Part I) zz
- [导入]A Taste of AOP from Solving Problems with OOP and Design Patterns (Part I)
- A Taste of AOP from Solving Problems with OOP and Design Patterns (Part III)
- A Taste of AOP from Solving Problems with OOP and Design Patterns (Part II) zz
- [导入]A Taste of AOP from Solving Problems with OOP and Design Patterns (Part I)
- A Taste of AOP from Solving Problems with OOP and Design Patterns (Part III) zz
- 三对角线性方程组(tridiagonal systems of equations)的求解
- A Taste of AOP from Solving Problems with OOP and Design Patterns (Part I)
- DEEP LEARNING IS THE FUTURE: Q&A WITH NAVEEN RAO OF NERVANA SYSTEMS
- A Taste of AOP from Solving Problems with OOP and Design Patterns (Part II)
- Schaum's Outline of Data Structures with Java, Second Edition
- imageNamed 与 imageWithContentsOfFile的区别
- *** Assertion failure in -[CompensaCommentView _dequeueReusableViewOfType:withIdentifier:],
- imageNamed:与imageWithContentsOfFile:的区别