Message Boards Message Boards

[✓] Build a matrix with the help of loop structures?


Please, friends, I ask your help. Based on my old minded and averaged skilled Fortran programmer, I would know how to build a matrix based on the Loop structure, as presented in the attached file. There is no need to having the same structure or similarity in commands, but only doing the same task. I thank you very much! Scabral!
Fortran example

POSTED BY: Sergio Cabral
1 year ago

Hi Sergio, this should work. Please check carefully against the results of the old code

xm = Array[x, {10, 10}];
For[i = 1, i <= 10, i++,
 xi = (N[i] - 0.5) h;
 x[i, i] = const * Log[0.44705*h*xi*2.];
 For[j = 1, j <= 10, j++,
  If[i == j, Break[]];
  xj = (N[j] - 0.5) h;
  x[i, j] = const* Log[Abs[xi - xj]* Abs[xi + xj]];
  x[j, i] = x[i, j];

Here you can see the structure of the matrix before putting in the numbers


Finally the variables are replaced by the numeric values

prms = {g -> 1.*10^8 / 2.83, freq -> 377., h -> 5.*10 (-3), 
   perm -> 4.* \[Pi]* 10^(-7), eo -> 1.};
MatrixForm[xm] /. const -> (freq * perm* g * h^2) / (2. \[Pi]) /. prms
POSTED BY: Michael Helmle
1 year ago

Thank you so much , Michael ! Absolutely great from you!

POSTED BY: Sergio Cabral
1 year ago

Hi, dears ! Unfortunately, it didn´t work. In fact, I need a Wolfram version to this programof building a matrix X . The key point seems to be the presence of j as the argument of logarithm. Thank you !

Mo= 0,4PiE-6 Sigma = 35335689,05 w = 120*Pi h = 0.005 const = (w * Mo * sigma * h^2 )/(2*Pi)

Define x(10,10) Do i =1 to 10, step = 1 X(i,i) = const LN(0.44705h)
Do j = 1 to 10, step =1 If (i= j ) Continue X(I,j) = const* LN( j * h) X(j,i) =X (i,j) Continue

POSTED BY: Sergio Cabral
1 year ago

Hi Sergio,

I propose you to verify the following code with Table[]:

    mo = 4/10  \[Pi];
    \[Sigma] = 3533568905/100;
    w = 120 \[Pi];
    h = 5/1000;
    const = (w  mo \[Sigma] h^2)/(2. \[Pi]);

    x = Table[
       Which[i < j, const Log[j h],
             i == j, const Log[0.44705 h],
             i > j, const Log[i h]], 
       {i, 10}, {j, 10}];

enter image description here

Your matrix is defined as a list $x$. Element $(i,j)$ of the matrix $x$ can be addressed by applying the syntax $x[[i,j]]$.

POSTED BY: Valeriu Ungureanu
1 year ago

Seems ti be great, Valerius ! Gonna try it and give you the feedback ! Thank you !

POSTED BY: Sergio Cabral
1 year ago

I haven't checked for efficiency of evaluation, but for sheer simplicity of code, why not just the following?

x[i_, j_] := const Log[Max[i, j] h]
x[i_, i_] := const Log[0.44705 h]
Table[x[i, j], {i, 1, 10}, {j, 1, 10}] // MatrixForm

That uses the symmetry of the condition for different i and j, and it exploits the fact that a specific rule, here


always overrides a general rule, here:

POSTED BY: Murray Eisenberg
1 year ago

Group Abstract Group Abstract