2
$\begingroup$

I want to create a $n \times n$ Vandermonde matrix.

How could I set up a matrix, vMatrix so I can just use vMatrix[5] and get an element of the matrix?

Do I have to set up a $n \times n$ null matrix first to build it?

xk[k_, n_] := (-1 + k*1/(n/2))
xk[4, 7]
f[x_] := 1/(1 + 25 x^2)
f[6]
fk[n_] := Table[f[xk[i, n]], {i, 0, n}]
fk[10]
PlotPoint[x_] := 
 ListPlot[Table[{xk[i, x], Part[fk[x], i + 1]}, {i, 0, x}]]
PlotLine[x_] := Plot[f[i], {i, xk[0, x], xk[x, x]}]
Show[PlotLine[10], PlotPoint[10]]

getMatrix[N_] := Table[f[i]
```
$\endgroup$
4
  • 4
    $\begingroup$ The code in this post looks very much like the code posted here under the user name "Rack Cloud". $\endgroup$
    – m_goldberg
    Dec 1, 2019 at 18:19
  • $\begingroup$ yeah i have 2 stackaccounts due to the fact that some subforums have a question limit $\endgroup$
    – Rapiz
    Dec 1, 2019 at 19:32
  • $\begingroup$ Possible duplicate of Getting the $x$ values to generate a list plot for a given function $\endgroup$ Dec 2, 2019 at 7:29
  • $\begingroup$ Not quite the solution that's the easiest to understand, but you can use DesignMatrix for this. E.g., DesignMatrix[Array[{x[#], 1} &, 5], y^Range[5], y]. $\endgroup$ Jul 18, 2022 at 21:01

3 Answers 3

13
$\begingroup$

Use Array

vMatrix[n_Integer?Positive] :=
 Array[x[#1]^(#2 - 1) &, {n, n}]

Format[x[n_]] := Subscript[x, n]

(mat = vMatrix[5]) // MatrixForm

enter image description here

Det[mat] // Simplify

enter image description here

EDIT: To enable the argument to also be a vector, add to the definition

vMatrix[v_?VectorQ] :=
 #^Range[0, Length[v] - 1] & /@ v

mat == vMatrix[Array[x, 5]]

(* True *)
$\endgroup$
1
  • $\begingroup$ Solved it with table don't know the Suberscript function will read through that. $\endgroup$
    – Rapiz
    Dec 1, 2019 at 19:32
9
$\begingroup$

Just for the sake of some variety:

vandermonde[n_Integer?Positive] := 
 Outer[Power, Table[Subscript[x, i], {i, 1, n}], Range[0, n - 1]]
$\endgroup$
1
  • 3
    $\begingroup$ +1. For further variety, I'd probably write a general-purpose vandermonde and apply to my x array: vandermonde[x_] := Outer[Power, x, Range[0, Length@x - 1]] $\endgroup$
    – Michael E2
    Dec 1, 2019 at 23:30
7
$\begingroup$

I want to create a $n \times n$ Vandermonde matrix.

There is an undocumented function for this:

LinearAlgebra`Private`VandermondeMatrix[Array[x, 5], Transpose -> True]
   {{1, x[1], x[1]^2, x[1]^3, x[1]^4},
    {1, x[2], x[2]^2, x[2]^3, x[2]^4},
    {1, x[3], x[3]^2, x[3]^3, x[3]^4},
    {1, x[4], x[4]^2, x[4]^3, x[4]^4},
    {1, x[5], x[5]^2, x[5]^3, x[5]^4}}

Edit 7/19/2022

In the current version, the following should be evaluated instead:

StructuredArray`VandermondeMatrix[Array[x, 5], List]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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