您的位置:首页 > 其它

Solving systems of equations with matrices

2016-09-23 00:00 204 查看

1:SystemsOfEquations

Let'ssaythatwehavetwoships.Weknowthetotalvalueofthecargoineachship'shold,butwedon'tknowhowmucheachitemcosts.Therearetwodifferentkindsofitems--fusionreactors,andgiantrobots.

Wecanrepresentfusionreactorswiththevariablex,andgiantrobotswiththevariabley.Thefirstshiphas2fusionreactorsand1giantrobot,andthecargoisworth25billiondollars.Thesecondshiphas3fusionreactors,and2giantrobots,andthecargoisworth40billiondollars.

Wecanrepresentthismathematicallyasasystemofequations:



Thisisjustrewritingtheinformationwehadfrombefore--2fusionreactorsplus1giantrobotequals25billiondollars,and3fusionreactorsplus2giantrobotsequals40billiondollars.

Tosolvethis,wecanfirstmultiplythetopequationbytwo:



Thismakessense,becauseif,thenitmakessensethat--we'rejustdoublingeverything.

Thenextstepistosubtractthebottomequationfromthetopequation:



Thistellsusthatxis10,whichmeansthatafusionreactorisworth10billiondollars.Wecanperformthissubtractionbecausebothequationsaretruestatements--thussubtractingonefromtheotheralsoyieldsatruestatement.

Nowthatweknowthatxequals10,wecansubstitutethatvalueintothebottomequationtosolvefory:



Whichsimplifiesto:



Andfinally:



So,fusionreactorsareworth10billiondollarseach,andgiantrobotsare5billiondollarseach.

2:SystemsOfEquationsAsMatrices

We'veworkedabitwithmatricesbefore,andtheverycoolthingaboutsystemsofequationsisthatwecanrepresentthemthesameway.

Ourequationsabovecouldberepresentedasamatrix:



Thisisjustamatrixthatcontainsthecoefficientsandconstantsintheequationswehave.Wecalledthisanaugmentedmatrix,becauseithasboththeconstantsandthecoefficients--thelineseparatesthem,withconstantstotheright.

Thismatrixhastworows(eachofwhichrepresentsanequationinthesystem),and3columns.Thefirstcolumnrepresentsx,thesecondrepresentsy,andthethirdistheconstantsintheequations(therightofthe=sign).

Theunderpinningsoflinearalgebraaresystemsofequations,butwegenerallyworkwithmatriceslikethisbecausetheyaresimplertorepresent.

Thesimplestwaytorepresentamatrixinpythonisanumpyarray.Anumpyarraycanhaverowsandcolumns,justlikeamatrix.

Instructions

Multiplythefirstrowofthematrixbytwo.

Thensubtractthesecondrowfromthefirstrow.

Then,subtractthreetimesthefirstrowfromthesecondrow.

Finally,dividethesecondrowby2togetridofthecoefficient.

Attheend,thefirstrowshouldindicatethatxequals10,andthesecondrowshouldindicatethatyequals5.Wejustsolvedourequationwithmatrices!

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:


Ifalinearsystemischangedtoanotherbyoneofthefollowingoperations:

(1)anequationisswappedwithanother
(2)anequationhasbothsidesmultipliedbyanonzeroconstant
(3)anequationisreplacedbythesumofitselfandamultipleofanother

thenthetwosystemshavethesamesetofsolutions.
Thisishowwecantransformoursystemsofequationsthroughswappingandmultiplying,andsolvethesystem.It'sbecauseoursimplifiedrepresentationleadstothesamesolutionsasthemorecomplexinitialsystem.

4:SolvingMoreComplexEquations

Welookedataprettysimplesystemofequationsbeforehand,butwecanextendourmethodstoevensolvemorecomplexsystems.



Inthissystem,wehavethreevariables(firstcolumnisx,secondisy,andthethirdisz).Wealsohavethreeequationsthatwecanmanipulate.

Thefirstthingwecandoissubtract2timesthefirstrowfromthesecondrow:



Thewecansubtractthefirstrowfromthethirdrow:



Then,wecansubtractthesecondrowfromthethirdrow:



Thistellsusthatzequals0.Wecansubstituteintothesecondrowtosolvefory:



Thistellsusthatyequals-1.Wecansubstituteintothefirstequationtosolveforx:



Now,wehaveeverythingsolved!

Instructions

Solveforx,y,andzinmatrix.Whenyou'redone,thematrixshouldlooklikethis,exceptwiththerightanswerssubstitutedforthequestionmarks:



Youcanuseprint(matrix)toprintthematrixoutasyousolveitandassessyourprogress.

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

Swaprowstogetmatrixintoechelonform.

matrix[[0,2]]=matrix[[2,0]]willexchangethefirstandthethirdrows.

matrix=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,excepteveryleadingvariablemustequal1,anditmustbetheonlynonzeroentryinitscolumn.

Here'sanaugmentedmatrixinreducedrowechelonform.Notethatcoefficientsandconstantsaretreatedseparately,soconstantsdon'thavetofollowtheguidelinesforreducedrowechelonform:

⎡⎣⎢⎢100010001030⎤⎦⎥⎥[100001030010]

Here'saregularmatrixinreducedrowechelonform:

[1001][1001]

Generally,togettoreducedrowechelonform,werepeatthefollowingsteps:

Startonanewrow

Performanyneededswapstomovetheleftmostavailableleadingcoefficienttothecurrentrow

Dividetherowbyitsleadingcoefficienttomaketheleadingcoefficientequal1

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

FindwhetherAisconsistentbyattemptingtoconvertittoreducedrowechelonform.

AssignTruetoA_consistentifitis,Falseifitisn't.

FindwhetherBisconsistentbyattemptingtoconvertittoreducedrowechelonform.

AssignTruetoB_consistentifitis,Falseifitisn't.

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.

xandyaretheleadingvariables,andzisthefreevariable,becauseitdoesn'tleadanyrows.Thus,wecanexpressxandyintermsofz.

Westartwiththesecondrow,andget:

−3y+3z=3−3y+3z=3

y−z=−1y−z=−1

y=z−1y=z−1

So,wehaveexpressedyintermsofz.Wecannowdothesamewithx.First,wesubstitutethevalueforyintothefirstrow:

x+(z−1)=0x+(z−1)=0

x=1−zx=1−z

Thissystemofequationshasinfinitesolutions,becausezcouldtakeonanyvalue--wedon'thaveanywaytosimplifyanymore.Sincewehavechosentoexpressyandxintermsofz,ziscalledaparameter.Notallfreevariableshavetobeparameters--youcanchoosewhichonestousewhenexpressingaleadingvariable.

Instructions

CheckwhetherAhasinfinitesolutions.

Ifitdoes,assignTruetoA_infinite.

Ifitdoesn't,FalsetoA_infinite.

CheckwhetherBhasinfinitesolutions.

Ifitdoes,assignTruetoB_infinite

Ifitdoesn't,assignFalsetoB_infinite.

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,wefindthatxandybotharezero.First,wesubtractthreetimesthefirstrowfromthesecondrow.

[102−200][1200−20]



So,wehave−2y=0−2y=0,whichsimplifiestoy=0y=0.

[102100][120010]



Then,wecansubtracttwotimesthesecondrowfromthefirstrow:

[100100][100010]



Thistellsusthatbothxandymustequalzero.

Asquarematrixissingularifitrepresentsahomogeneoussystemwithinfinitesolutions.Here'sanexample:

[1326][1236]



Thisissingularbecausewecan'tsimplifyitpastacertainpoint.Let'strythis.First,wesubtract3timesthefirstrowfromthesecondrow:

[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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: