nummethods2x.nb Num. Math.2 Mathematica Lineáris Algebra Lineáris Egyenletrendszerek In[]:= Out[]= In[2]:= Solvex^250 x 5,x 5 Solvexy2, xy0,x, y Out[2]= x 3 0, y 5 Numerikus egyenlet(rendszer) megoldó rutin In[4]:= Out[4]= NSolvexy.5, xy.,x, y x 0.3, y 0.2 Kompakt írásmód, automatikus knoverzió megoldott (m rendszermátrix, v jobboldali oszlopvektor) In[9]:= In[]:= Out[]= In[8]:= v.5,.; m,,,; LinearSolvem, v 0.3, 0.2? LinearSolve LinearSolvem, b finds an x which solves the matrix equation m.x b. LinearSolvem generates a LinearSolveFunction... which can be applied repeatedly to different b. More Intermezzo: Mátrixok bevitele m
nummethods2x.nb 2 Cramer In[2]:= Out[2]= Detm 2 Nemszinguláris m, x i D i D In[3]:=? ReplacePart ReplacePartexpr, new, n yields an expression in which the nth part of expr is replaced by new. ReplacePartexpr, new, i, j,... replaces the part at position i, j,.... ReplacePartexpr, new, i, j,..., i2, j2,...,... replaces parts at several positions by new. ReplacePartexpr, new, pos, npos replaces parts at positions pos in expr by parts at positions npos in new. More In[4]:= MatrixFormTransposeReplacePartTransposem, v, Out[4]//MatrixForm= 0.5 0. In[5]:= DetTransposeReplacePartTransposem, v, Detm Out[5]= 0.3 In[6]:= DetTransposeReplacePartTransposem, v, 2Detm Out[6]= 0.2 Demonstráció: Numerikus szempontok Vandermonde mátrix, paraméterek 2,3,4,5,...és bi i n i) Egy lineáris egyenletrendszer sorozatot vizsgálunk. Az n paraméter függvényében nő a rendszer mérete b oszlopvektor V(Vanderomnde) együtthatómátrix In[7]:= In[8]:= Out[8]= In[9]:= bn_ : Tablei^ni,i, n b4 5, 40, 85, 56 Vn_ : Tablei^j,i, 2, n,j, 0, n Változónevek automatikus generálása In[20]:= varlistn_ : TableToExpressionStringJoin"x", ToStringi,i, n
nummethods2x.nb 3 In[2]:= V4 MatrixForm Out[2]//MatrixForm= 2 4 8 3 9 27 4 6 64 5 25 25 In[22]:= TimingLinearSolveV00, b00 Out[22]= 7.36288 Second,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, Észretvételek: ha egzakt aritmetikát alkalmazunk, akkor a megoldásvektor meinden komponense. Most numerikus módszerekkel dolgozunk. In[23]:= Out[23]= In[24]:= TimingLinearSolveV5, Nb5.47885 0 6 Second,.,.,.,.,. TimingLinearSolveV4, Nb4 LinearSolve::luc : Result for LinearSolve of badly conditioned matrix., 2., 4., 8., 6., 32., 64., 28., 256., 52., 4,., 3., 9., 27., 8., 243., 729., 287., 656., 9683., 4, 8, 4 may contain significant numerical errors. More Out[24]= 0.24964 Second,0.099476, 0.228596, 0.466264, 0.867372,.29352,.28758, 0.684088,.209, 0.9754,.0034, 0.99975,.0000,.,. In[25]:= NLinearAlgebra MatrixConditionNumberV4 Out[25]= 9.778 0 20 Megj. Figyeljük meg a megoldásvektor komponenseit! A mátrix kondiciószáma nagy (a mátrix gyengén meghatározott)! NLinearAlgebra MatrixConditionNumber, 0,0, 0 Ugyanez Nsolve val, csak különbözõ pontossággal. In[26]:= ThreadV4.varlist4 b4 Out[26]= x 2 x2 4 x3 8 x4 5, x 3 x2 9 x3 27 x4 40, x 4 x2 6 x3 64 x4 85, x 5 x2 25 x3 25 x4 56 In[27]:= TimingNSolveThreadV4.varlist4 b4, varlist4 Out[27]= 0.003 Second,x 328.927, x2 723.329, x3 697.09, x4 397.297, x5 46.87, x6 39.393, x7 6.5026, x8.96894, x9 0.90435, x0.0068, x 0.99966, x2.0000, x3., x4.
nummethods2x.nb 4 In[28]:= TimingNSolveThreadV4.varlist4 b4, varlist4, WorkingPrecision 6 Out[28]= 0.004999 Second, x.000000000000007, x2 0.9999999999847547, x3 0.9999999999996490, x4 0.9999999999987662, x5 0.9999999999994556, x6 0.999999999999999, x7.000000000000000, x8.000000000000000, x9.000000000000000, x0.000000000000000, x.000000000000000, x2.000000000000000, x3.000000000000000, x4.000000000000000 Ez már elegendõen pontos. * Inverz A 2 3 4 ; DetA 2 B InverseA; MatrixFormB 2 3 2 2 MatrixFormA.B 0 0 Ált. (Moore Penrose) inverz A Transpose, 2, 3, 5,,,, ; A MatrixForm 2 3 5 MatrixRankA 2 r=o A oszlopreguláris Ha A oszlopreg, akkor A T A reg. A A T A A T
nummethods2x.nb 5 InverseTransposeA.A.TransposeA 5, 3 35, 35, 9 35, 4 5, 7 35, 6 6, 35 35 PseudoInverseA 5, 3 35, 35, 9 35, 4 5, 7 35, 6 6, 35 35 Alkalmazás: LSM FindFitTranspose, 2, 3, 5,2, 2, 4, 6,Α xα0,α,α0, x Α.0857, Α0 0.54286 PseudoInverseA.2, 2, 4, 6 38 35, 8 35 N%.0857, 0.54286 GraphicsRGBColor, 0, 0, PointSize.03, Point, 2, Point2, 2, Point3, 4, Point5, 6, RGBColor0,, 0, Line0, 835,7, 385835; Show%;
nummethods2x.nb 6 PlotΑ xα0.α.0857,α00.54286,x, 0, 7, PlotStyleRGBColor0,, 0, PrologRGBColor, 0, 0, PointSize.03, Point, 2, Point2, 2, Point3, 4, Point5, 6; 8 6 4 2 2 3 4 5 6 7 Demonstráció: Eüh. változtatása megoldásvektor komponeneinek változása Munka programcsomagokal: Nem minden függvény/szolgáltatás érhető el a Mathematica indítása után, de programcsoma gok bárnmikor betölthetők. Itt a Lináris Algebra csomagot töltjük be. In[30]:= In[3]:= LinearAlgebra? MatrixConditionNumber MatrixConditionNumbermat gives an estimate of the infinitynorm condition number of the matrix mat of approximate numbers. MatrixConditionNumbermat, p gives an estimate of the condition number in the pnorm, where p must be, 2, or Infinity. More Két egyenletrendszer megoldásvekrorainak összehasonlítása. Az együtthatók alig térnek el. Elsõ egyenletrendszer In[32]:= In[34]:= Out[34]= er 2x6y8; er2 2x600000^5 y800000^5; Solveer, er2,x, y x, y Második egyenletrendszer In[35]:= er2 2x6y8; er22 2x5999990^5 y8000020^5; General::spell : Possible spelling error: new symbol name "er2" is similar to existing symbol "er2". More
nummethods2x.nb 7 In[37]:= Out[37]= Solveer2, er22,x, y x 0, y 2 Mekkora eltérést okozott a 22 és b 2 kicsi megváltozása a megoldásvektorban? (! a számításokat szimbolikusan végeztük, ez nem okozhat pontatlanságot itt) In[38]:= A 2 6 2 600000^5 ; In[39]:= NMatrixConditionNumberA MatrixConditionNumber::prec : MatrixConditionNumber has received a matrix with infinite precision. Out[39]= 4.8000 0 6 Megj. Normálás szükségessége In[40]:= In[43]:= Out[43]= In[44]:= A., 2,3, 4; A2 0., 20,30, 40; A3.7, 27,37, 47; MatrixConditionNumberA, MatrixConditionNumberA2, MatrixConditionNumberA3 2., 2., 2. InverseA3 MatrixForm Out[44]//MatrixForm= 4. 7. 0.5 3.5 In[45]:= NNormInverseA3, Infinity Out[45]= 2. In[46]:= InverseMatrixNormNA3 Out[46]= 2. Kondiciószám kiszámítása (lsd. később)
nummethods2x.nb 8 Gauss Elimináció, Trianguláris Felbontás LU (LR) felbontás Cél: LU (LR) felbontás, ahol L alsó trianguláris, l ii U felülrõl trianguláris Megj. Nem minden mátrixnak létezik, még akkor sem, ha az nemszinguláris. Ha létezik, nem feltétlen egyértelmű. Elegendõ felt. a felb. létezésére: Balfelsõ fõminorok nemzérók Feladat: Ellenõrizzük, hogy az alábbi mátrixok esetén létezik felbontás (és ezekre a mátrixokra a fõelem kiv. nélküli par ketta alg. meghatározza az egyértelmű felbontást) Vn_ : Tablei^j,i, 2, n,j, 0, n In[50]:= A 2 2 3 3 4 ; A2 4 5 9 ; A3 V0; 7 8 9 *Hint In[5]:= DetA2 Out[5]= 8 In[52]:= Out[52]= In[53]:= TableMinorsA2, i,,i, LengthA2, 3, 8 TableMinorsA3, i,,i, LengthA3 Out[53]=,, 2, 2, 288, 34560, 24883200, 254328000, 5056584744960000, 83493347225084800000 In[54]:= In[55]:= Out[55]= In[56]:= Out[56]= LinearAlgebra SubMatrixA2,,,2, 2, 2,4, 5 Det% 3
nummethods2x.nb 9 In[57]:= Out[57]= And TableDetSubMatrixA2,,,i, i 0,i, 3 True Elimináló mátrixok In[59]:= In[60]:= A A2; A MatrixForm Out[60]//MatrixForm= 2 3 4 5 9 7 8 9 In[6]:= In[62]:= L 0 0 4 0 ; 7 0 L.A MatrixForm Out[62]//MatrixForm= 2 3 0 3 3 0 6 2 In[63]:= In[64]:= L2 0 0 0 0 ; 0 2 L2.L.A MatrixForm Out[64]//MatrixForm= 2 3 0 3 3 0 0 6 In[65]:= InverseL.InverseL2 MatrixForm Out[65]//MatrixForm= 0 0 4 0 7 2 In[66]:= Out[66]= MatrixFormInverseL.InverseL2, MatrixFormL2.L.A, MatrixFormA 0 0 2 3 4 0, 2 3 0 3 3, 4 5 9 7 2 0 0 6 7 8 9
nummethods2x.nb 0 Feladat V[4], Mathematica built in fgv. In[7]:= LUD LUDecompositionV4; LUD MatrixForm Out[7]//MatrixForm= 2 4 8 5 9 2 2 8 3 3 6 Hogyan kajuk L t és U t? In[72]:= 0 0 0 2 4 8 0 0 0 5 9. MatrixForm 2 0 0 0 2 8 3 3 0 0 0 6 Out[72]//MatrixForm= 2 4 8 3 9 27 4 6 64 5 25 25 In[73]:= V4 MatrixForm Out[73]//MatrixForm= 2 4 8 3 9 27 4 6 64 5 25 25