4
$\begingroup$

Calculate n-order determinant (n>1):

$\left|\begin{array}{cccccccc}a & b & 0 & 0 & \cdots & 0 & 0 & 0 \\ 0 & a & b & 0 & \cdots & 0 & 0 & 0 \\ 0 & 0 & a & b & \cdots & 0 & 0 & 0 \\ \vdots & \vdots & \vdots & \vdots & & \vdots & \vdots & \vdots \\ 0 & 0 & 0 & 0 & \cdots & 0 & a & b \\ b & 0 & 0 & 0 & \cdots & 0 & 0 & a\end{array}\right|$

The result should be

$a^{n}+(-1)^{n+1} b^{n}$

I can use the MMA code to get this result:

Clear["Global`*"];
s[n_] := SparseArray[{{n, 1} -> b, {i_, j_} /; j - i == 1 -> 
    b, {i_, j_} /; j == i -> a}, {n, n}]
detmat[n_] := Det[s[n]]
tab = Table[detmat[i], {i, 2, 10}]

$\left\{a^{2}-b^{2}, a^{3}+b^{3}, a^{4}-b^{4}, a^{5}+b^{5}, a^{6}-b^{6}, a^{7}+b^{7}, a^{8}-b^{8}, a^{9}+b^{9}, a^{10}-b^{10}\right\}$

FindSequenceFunction[tab, n - 1]

$a^{n}+(-b)^{-1+n} b$

However, when I tried another method, I made a mistake. How should I modify this code?

Clear["Global`*"];
detNewMatrix[n_] := 
 Det[Module[{i = 1, j = 1, mm = Array[m, {n, n}]}, 
   For[i = 1, i <= n, i++, 
    For[j = 1, j <= n, j++, 
     If[j == i + 1, m[i, j] = b, 
      If [i == n && j == 1, m[i, j] = b, 
       If[i == j, m[i, j] = a, m[i, j] = 0]]]]]; mm]]
tab = Table[detNewMatrix[k], {k, 2, 10}]

$\left\{a^{2}-b^{2}, a^{3}-a b^{2}+b^{3}, a^{4}+a b^{3}-b^{4}, a^{5}-a b^{4}+b^{5}, a^{6}+a b^{5}-b^{6}, a^{7}-a b^{6}+b^{7}, a^{8}+a b^{7}-b^{8}, a^{9}-a b^{8}+b^{9}, a^{10}+a b^{9}-b^{10}\right\}$

I think there may be a "coverage" in the code. It is correct to directly calculate any determinant, but when listing together, only the first determinant is calculated correctly, and the subsequent determinants are calculated incorrectly. But I don't know what went wrong.

$\endgroup$
2
  • 1
    $\begingroup$ That nested For loop is an awkward way to set up the matrix. In any case the issue is that m is not localized and so retains a bunch of values from prior iterations. To avoid this you might start the code as Module[ {i = 1, j = 1, m, mm}, mm = Array[m, {n, n}];... $\endgroup$ Apr 19 at 18:14
  • $\begingroup$ @Daniel Lichtblau OK, I get it now, thanks! $\endgroup$
    – lotus2019
    Apr 20 at 2:10

2 Answers 2

9
$\begingroup$

Change mm[i,j]to mm[[i,j]] in your last approach!

newmatrix[n_] := 
 Det@Module[{i = 1, j = 1, mm = Array[m, {n, n}]}, 
   For[i = 1, i <= n, i++, For[j = 1, j <= n, j++,
     If[j == i + 1, mm[[i, j]] = b,
      If[i == n && j == 1, mm[[i, j]] = b,
       If[i == j, mm[[i, j]] = a, mm[[i, j]] = 0]]]]]; mm] 

newmatrix[6] (*a^6-b^6*)
$\endgroup$
5
  • $\begingroup$ I tried. It won't work. $\endgroup$
    – lotus2019
    Apr 19 at 8:33
  • $\begingroup$ I think there may be a "coverage" in my code. It is correct to directly calculate any determinant, but when listing together, only the first determinant is calculated correctly, and the subsequent determinants are calculated incorrectly. But I don't know what went wrong. $\endgroup$
    – lotus2019
    Apr 19 at 8:38
  • $\begingroup$ @lotus2019 It should work, see my modified answer! $\endgroup$ Apr 19 at 8:48
  • $\begingroup$ Yes! Thank you!! $\endgroup$
    – lotus2019
    Apr 19 at 13:09
  • $\begingroup$ @lotus2019 You're welcome ! $\endgroup$ Apr 19 at 13:15
9
$\begingroup$
  • Band is another approach.
table = Table[
  Det@SparseArray[{Band[{1, 1}] -> a, Band[{1, 2}] -> b, 
     Band[{n, 1}] -> b}, n], {n, 2, 10}]
FindSequenceFunction[table, n - 1]

{a^2 - b^2, a^3 + b^3, a^4 - b^4, a^5 + b^5, a^6 - b^6, a^7 + b^7, a^8 - b^8, a^9 + b^9, a^10 - b^10}

a^n + (-b)^(-1 + n) b

  • RotateRight+ PadRight.
Table[Det@NestList[RotateRight, PadRight[{a, b}, n + 1], n], {n, 1, 
  10}]

{a^2 - b^2, a^3 + b^3, a^4 - b^4, a^5 + b^5, a^6 - b^6, a^7 + b^7, a^8 - b^8, a^9 + b^9, a^10 - b^10, a^11 + b^11}

$\endgroup$
1
  • $\begingroup$ Thank you! @cvgmt $\endgroup$
    – lotus2019
    Apr 19 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.