1

I am running a gamblers problem solution where I am testing the timing involved in solving the Ax = b equation for matrices of n=100, 1000, 10000, and 100000. For some reason I keep getting the same time (0.015625 seconds) for the time, but I know it takes many more seconds than this. The matrix for n=100000 ran for probably 20 mins and still returned 0.015625.

My process to create the matrix is basically:

 A = IdentityMatrix[100000, SparseArray];
 For[i = 2, i < 100000, i++,
 A[[i, i]] = -1;
 A[[i, i - 1]] = .5;
 A[[i, i + 1]] = .5
 ];

The vector is created by:

b = {1};
For[i = 2, i <= 100000, i++, b = Insert[b, 0, -1] ];

Then I run:

Timing[LinearSolve[A, b]]
Jaigus
  • 257
  • 1
  • 6

1 Answers1

1

I cannot tell what the problem with the timing is but I can show you how to set up and solve the system quicker:

First@AbsoluteTiming[
 n = 100000;
 A = SparseArray[{
  Band[{1, 1}] -> -1., 
  Band[{2, 1}] -> 0.5, 
  Band[{1, 2}] -> 0.5
  }, 
  {n, n}];
 b = Normal[SparseArray[{1} -> 1., {n}]];
 x = LinearSolve[A, b];
 ]

0.665885

Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309