TransWikia.com

How can I evaluate a matrix for a set of values?

Mathematica Asked by Nohora Alejandra Hernndez Cepe on September 25, 2020

I’m trying to evaluate a system, that depends on two variables, in a specific set of values. I will write an example, but my system is more complicated.

H = {{x^2, y}, {y^2, x}};
{val, vect} = Eigensystem[H];

I select an eigenvector

b = vect[[1]];

Then I write new variables in terms of the eigenvector elements

A = b[[1]];
B = b[[2]];

Finally, a new matrix is built.

M = {{A, B}, {A*A, B}};

x and y must be evaluate for the following list of data:

x={1,2,3,4};
y={0,1,2,5};

The thing is that I need to find the matrix M for each pair of coordinates [x,y]:

{{1,0},{2,1},{3,2},{4,5}}

with the help of a loop, for example Do. This, because my system is more complicated and I can not do it manually (It will take me a lot of time). I was trying to use the loop Do with the iterator Thread, nonetheless I don´t know if this is possible .

I’m new using Mathematica. I really don’t understand why this is happening. Some of you guys can help me? Thank you.

3 Answers

There are lots of possible way to do this; which is preferable is likely to depend on your specific needs. One method is to form a Function and then MapThread that across your values. Below Evaluate is used to get the definition of M into the held Function body instead of a verbatim M.

Clear[x, y]

MapThread[{x, y} [Function] Evaluate[M], {{1, 2, 3, 4}, {0, 1, 2, 5}}] // Quiet
{{{Indeterminate, 1}, {Indeterminate, 1}},
 {{1/2 (2 - 2 Sqrt[2]), 1}, {1/4 (-2 + 2 Sqrt[2])^2, 1}},
 {{1/8 (6 - 2 Sqrt[17]), 1}, {1/64 (-6 + 2 Sqrt[17])^2, 1}},
 {{1/50 (12 - 2 Sqrt[161]), 1}, {(-12 + 2 Sqrt[161])^2/2500, 1}}}

Another method is to make a Listable function to handle the threading directly, as follows:

fn = Function[{x, y}, Evaluate[M], Listable];

fn[{1, 2, 3, 4}, {0, 1, 2, 5}] // Quiet   (* same output *)

Answered by Mr.Wizard on September 25, 2020

Another possibility

ClearAll[x, y];
H = {{x^2, y}, {y^2, x}};
v = First@Eigenvectors[H];
A = v[[1]]; B = v[[2]];
M = {{A, B}, {A*A, B}};
xCoord = {1, 2, 3, 4};
yCoord = {0, 1, 2, 5};
coord = Partition[Riffle[xCoord, yCoord], 2];
data = {{#[[1]], #[[2]]}, Limit[M, {x -> #[[1]], y -> #[[2]]}]} & /@ coord;
data = {#[[1]], MatrixForm[#[[2]]]} & /@ data;
Grid[Join[{{"(x,y)", "M matrix"}}, data], Frame -> All]

Mathematica graphics

If you want numerical, change the line above to

 data = {#[[1]], MatrixForm[N@#[[2]]]} & /@ data;

Mathematica graphics

Answered by Nasser on September 25, 2020

Two things to note are (1) different eigenvector normalization would lead to different M-matrices and (2) when $(x,y)=(1,0)$ we have two eigenvectors for the repeated eigenvalue, so which eigenvector do we call "first"? We let Mathematica choose the normalization and the eigenvector order in the following code:

H = {{x^2, y}, {y^2, x}};
allM = Table[
   With[{v = First@Eigenvectors[
        H /. x -> First@val /. y -> Last@val]},
    {v, v*{First@v, 1}}],
   {val, Thread[{{1, 2, 3, 4}, {0, 1, 2, 5}}]}];

Column[MatrixForm /@ allM]

$begin{array}{l} left( begin{array}{cc} 0 & 1 0 & 1 end{array} right) left( begin{array}{cc} 1+sqrt{2} & 1 left(1+sqrt{2}right)^2 & 1 end{array} right) left( begin{array}{cc} frac{1}{4} left(3+sqrt{17}right) & 1 frac{1}{16} left(3+sqrt{17}right)^2 & 1 end{array} right) left( begin{array}{cc} frac{1}{25} left(6+sqrt{161}right) & 1 frac{1}{625} left(6+sqrt{161}right)^2 & 1 end{array} right) end{array}$

The reason these results differ from other methods is that this method puts the numbers in first so Mathematica can use numeric sort.

When $(x,y)=(1,0)$ the H-matrix is the identity matrix, so any finite, non-zero vector ${A,B}$ is an eigenvector.

Answered by LouisB on September 25, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP