1
$\begingroup$

I want to construct a n*m matrix, such that: 1- its first element is given by:

c0 = 1 - 4/\[Pi] (1 - 10^-4)*
NIntegrate[
 1/u^2*Sin[u/2]^2/(1 + 10^-7*I*Sqrt[u^2 - 0.006^2]), {u, 
  0, \[Infinity]}];

2- The rest elements of the first row are given by:

f[n_] := 4/\[Pi] (1 - 10^-4)*(-1)^(n + 1)*NIntegrate[
1/(1 + 10^-7*I*Sqrt[u^2 - 0.006^2])*Sin[u/2]^2/(
 u^2 - 4*n^2*\[Pi]^2), {u, 0, \[Infinity]}];

3- Diagonal elements -except first element; say x[[1,1]] that is defined in 1- are given by:

g[m_] := 1 - 8/\[Pi] (1 - 10^-4)*NIntegrate[
 u^2/(1 + 10^-7*I*Sqrt[u^2 - 0.006^2]) Sin[
    u/2]^2/(u^2 - 4*m^2*\[Pi]^2)^2, {u, 0, \[Infinity]}];

4- The rest elements X[[n,m]] are given by:

fnm[n_, m_] := 8/\[Pi] (1 - 10^-4)*(-1)^(n + m + 1)*NIntegrate[
u^2/(1 + 10^-7*I*Sqrt[u^2 - 0.006^2]) Sin[
   u/2]^2/((u^2 - 4*n^2*\[Pi]^2)*(u^2 - 4*m^2*\[Pi]^2)), {u, 
 0, \[Infinity]}];

My question is could you please tell me what is the simplest way with mathematica I can use to construct arbitrary n*m X matrix may be 100*100 matrix for example.

$\endgroup$

3 Answers 3

2
$\begingroup$

One of many possible ways (and certainly not an optimal one):

X = ConstantArray[0., {n, m}];
X[[1, 2 ;;]] = Array[f, {m - 1}, 2];
X[[2 ;;]] = Array[fnm, {n - 1, m}, {2, 1}];
LinearAlgebra`SetMatrixDiagonal[X, Join[{c0}, Array[g, {Min[n, m] - 1}, 2]]];
$\endgroup$
1
$\begingroup$
fn[x_, y_] := Which[
  x == 1 && y == 1, c0,
  x == 1, f[y],
  x == y, g[y],
  True, fnm[x, y]
  ]
result=Array[fn, {100,100}]
$\endgroup$
0
$\begingroup$

One possibility is to redefine your function so that it is consistently named, and then use the Outer product. Withf[n,m] as defined, let f[1,m] be your current f[m], let f[m,m] be your g[m], and set f[1,1]=c0. Then you can define the matrix as:

Outer[f, Range[n], Range[m]]

For an easy visualizable example:

Clear[f]; f[1, m_] := f[m]; f[m_, m_] := g[m]; f[1, 1] = c0;
n = 4; m = 6; Outer[f, Range[n], Range[m]] // MatrixForm

enter image description here

$\endgroup$
3
  • $\begingroup$ All of you, Thank you very much. I tried all the three suggested ways. In fact I found that the bill s's way is the simplest one!!! Many thanks bill s! $\endgroup$ Commented Oct 18, 2017 at 13:47
  • $\begingroup$ The usual thing to do with answers you like is to upvote them. Then wait a few days, and if no better answers appear, to accept them. $\endgroup$
    – bill s
    Commented Oct 18, 2017 at 14:46
  • $\begingroup$ Thank you bill for your advise. $\endgroup$ Commented Oct 18, 2017 at 14:50

Your Answer

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

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