# Memory issue when using LinearSolve

When I use LinearSolve to solve a large system of linear equations where the left hand side is a matrix and the right hand side is a vector, the process takes a very long time and eventually a message appears telling me that there is not enough memory.

coords = Flatten[Table[{j, i} , {i, {0, 2, 4, 6}}, {j, {0, 2, 4, 6}}], 1];

ElementNodes = {{1, 2}, {2, 3}, {3, 4}, {1, 5}, {1, 6}, {2, 5}, {2,
6}, {2, 7}, {3, 6}, {3, 7}, {3, 8}, {4, 7}, {4, 8}, {5, 6}, {6,
7}, {7, 8}, {5, 9}, {5, 10}, {6, 9}, {6, 10}, {6, 11}, {7, 10}, {7,
11}, {7, 12}, {8, 11}, {8, 12}, {9, 10}, {10, 11}, {11, 12}, {9,
13}, {9, 14}, {10, 13}, {10, 14}, {10, 15}, {11, 14}, {11,
15}, {11, 16}, {12, 15}, {12, 16}, {13, 14}, {14, 15}, {15, 16}};

NumOfElements = 42 ; NumOfNodes = 16 ; Eevalue = 69; Ee =
Table[10^4, {NumOfElements}]; P = 100 ; dof =
2 * NumOfNodes; ebcDof = {1, 2, 25, 26} ; ebcDofValues = {0, 0, 0,
0} ;
nbcDof = {8}  ; nbcDofValues = {-P} ;

ElemetsArea = {1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1};
ElementsModulusE = {69, 69, 69, 69, 69, 69, 69,
69, 69, 69, 69, 69, 69, 69,
69, 69, 69, 69, 69, 69, 69,
69, 69, 69, 69, 69, 69, 69,
69, 69, 69, 69, 69, 69, 69,
69, 69, 69, 69, 69, 69, 69};

KPlaneTruss[coords_ , ElementNodes_, NumOfNodes_, NumOfElements_ ,
ElemetsArea_ , ElementsModulusE_ , ebcDof_, ebcDofValues_ , nbcDof_,
nbcDofValues_ ] :=
Module [ {dof , K, R, d, k, lm , x1 , y1 , x2 , y2 , L , ls, ms,
Llist , lslist , mslist, Kf, Rf , Freedof , FreeValues, T, ds1,
ds2, , detKf},
dof = 2*NumOfNodes ; K = Table [ 0 , {dof} , {dof}] ;
R = Table [ 0 , {dof}] ; d = Table [ 0 , {dof}] ;
R[[nbcDof]] += nbcDofValues;
Llist = lslist = mslist = Table[ 0 , {NumOfElements}];
Do [ lm = {(2*ElementNodes [[i, 1]] ) - 1 ,
2 ElementNodes [[i, 1]] , ( 2 ElementNodes [[i, 2]] ) - 1 ,
2 ElementNodes [[i, 2]]}  ;
{{x1 , y1} , { x2 , y2}} =  coords[[ElementNodes [[i]]]];
L = Sqrt[(x2 - x1)^2 + (y2 - y1)^2]; ls = (x2 - x1)/L ;
ms = (y2 - y1)/L ;
k = ElemetsArea[[i]]* ElementsModulusE[[i]]/
L*{{ls^2 , ls ms , -ls^2 , -ls ms} , {ls ms ,
ms^2 , -ls ms , -ms^2}, {-ls^2 , -ls ms , ls^2  ,
ls ms}, {-ls ms , -ms^2 , ls ms , ms^2}}  ;
K[[lm, lm]] += k ; Llist[[i]] += L ; lslist[[i]] += ls ;
mslist[[i]] += ms  , {i, 1, NumOfElements}];
Freedof =  Complement [ Range[dof] , ebcDof] ;
Rf = R[[Freedof]] - K[[Freedof , ebcDof]].ebcDofValues  ;
Kf = K[[Freedof , Freedof ]] ;
detKf = Det[Kf];
If[detKf == 0 , d = Table["Ind", { dof}] ,
(*FreeValues = LinearSolve [ Kf,Rf] ;*)
d[[Freedof]] += FreeValues ; d[[ebcDof]] += ebcDofValues ;
]; {Kf, Rf}]


Here I am implementing the function I have created to get the L.H.S matrix and the R.H.S vector

{matrix, R} =
KPlaneTruss[coords, ElementNodes, NumOfNodes, NumOfElements,
ElemetsArea , ElementsModulusE, ebcDof, ebcDofValues , nbcDof,
nbcDofValues ]

-
You really need to provide more information, otherwise it's impossible to give advice. Is it a symbolic or a numeric matrix/vector? If it's numeric, make sure it's a machine precision matrix by applying the N function to it. Is it a dense or a sparse matrix? If it's sparse, use SparseMatrix to represent it. – Szabolcs Feb 26 '14 at 1:11
When I try to Use LinearSolve as a last step it doesn't work – user12613 Feb 27 '14 at 20:47
I get a result in 3 seconds or so. I doubt you really need an exact value though. Could instead numericize the input. After computing the determinant for example you might add the line {Kf, Rf} = N[{Kf, Rf}];. – Daniel Lichtblau Feb 28 '14 at 0:56
Thank you. you are right numericizing the input helps a lot – user12613 Mar 1 '14 at 2:09