njvb
njvb

Reputation: 1377

Matrix creation MATLAB

I am building a nxn matrix in matlab with the following code:

x = linspace(a,b,n);

for i=1:n
    for j=1:n
        A(i,j) = x(j)^(i-1);
    end
    A
    i
    b(i) = (1/i)*x(n)^i - (1/i)*x(1)^i;
end

I am testing it with a=1 b=10 and n=10. I get the expected results up to i=8

i =

 8

A =

Columns 1 through 7

       1           1           1           1           1           1           1
       1           2           3           4           5           6           7
       1           4           9          16          25          36          49
       1           8          27          64         125         216         343
       1          16          81         256         625        1296        2401
       1          32         243        1024        3125        7776       16807
       1          64         729        4096       15625       46656      117649
       1         128        2187       16384       78125      279936      823543
       1         256        6561       65536      390625     1679616     5764801

Columns 8 through 10

       1           1           1
       8           9          10
      64          81         100
     512         729        1000
    4096        6561       10000
   32768       59049      100000
  262144      531441     1000000
 2097152     4782969    10000000
16777216    43046721   100000000

however from i=9 on it becomes this:

i =

 9

A =

1.0e+09 *

Columns 1 through 9

0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000
0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000
0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000
0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000
0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000
0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0001
0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0001    0.0003    0.0005
0.0000    0.0000    0.0000    0.0000    0.0001    0.0003    0.0008    0.0021    0.0048
0.0000    0.0000    0.0000    0.0001    0.0004    0.0017    0.0058    0.0168    0.0430
0.0000    0.0000    0.0000    0.0003    0.0020    0.0101    0.0404    0.1342    0.3874

Column 10

0.0000
0.0000
0.0000
0.0000
0.0000
0.0001
0.0010
0.0100
0.1000
1.0000

Can someone please tell me what is happening? I am not very experienced in matlab (I mostly use c++ or python) and so far can't seem to figure it out myself.

Upvotes: 1

Views: 1436

Answers (2)

anon
anon

Reputation:

Because a common scaling is applied to your data display. See in your output:

A =

1.0e+09 *

A common factor of 10^9 was factored out of every entry in your matrix.

You may want to adjust your output display using:

format short g

Upvotes: 1

silvado
silvado

Reputation: 18187

It's just a formatting issue for larger numbers. Try

sprintf('%20.0f', A(end,end))

and you will see that the number is correct. At least up to some point, where you will run into double representation problems...

Upvotes: 2

Related Questions