I have a system of linear equations with variables $A_n$ and need its coefficients in a matrix form (Transform linear system into a matrix form). I'm trying to do that through a Do
-cycle and CoefficientArrays
, however, I have mistaken somewhere
ClearAll["Global`*"];
kk = 3; (* just test values *)
nn = 4; (* just test values *)
eqns = {};
(* I expect code below make a list of equations in form
eqns = {2 x - y + 4 z == 12, 3 x + 2 y + z == 10, -y + z == 1}; *)
Do[
eqns[[n]] =
Indexed[coefA,n] ((1 - I)/Sqrt[2] \[Eta] Sqrt[n]*
BesselJ[n - 1, (1 - I)/Sqrt[2] \[Eta] Sqrt[n]] -
n BesselJ[n, (1 - I)/Sqrt[2] \[Eta] Sqrt[n]]) +
Sum[Integrate[h[\[Phi]]/E^Ik\[Phi], {\[Phi], 0,2*Pi}] (Indexed[coefA, n - k] BesselJ[n - k, (1 - I)/Sqrt[2] \[Eta] Sqrt[n - k]] -
Integrate[t[\[Phi]]/E^(I (n - k) \[Phi]), {\[Phi], 0,
2*Pi}] )==0, {k, -kk, kk, 1}],
{n, 1, nn}
]
and get only following errors:
Set::partw: Part -4 of {} does not exist. >> Indexed::ind: The index 0 is not a nonzero integer. >> Set::partw: Part -3 of {} does not exist. >> Indexed::ind: The index 0 is not a nonzero integer. >> Set::partw: Part -2 of {} does not exist. >> General::stop: Further output of Set::partw will be suppressed during this calculation. >> Indexed::ind: The index 0 is not a nonzero integer. >> General::stop: Further output of Indexed::ind will be suppressed during this calculation. >>
Then I'm taking coefficients from eqns
, and here,probably, I'm mistaken too:
nlist = Table[n, {n, -nn, nn, 1}];
(* As a result of cycle I expect list coefAlist={Indexed[coefA, 1],Indexed[coefA, 2],...} *)
Do[coefAlist[n] = Indexed[coefA, n], {n, nlist}]
linsystem = CoefficientArrays[eqns, coefAlist];
I have no ideas how to correct this code in the proper way.
Can smb help?
Do
,List
andCoefficientArrays
, but I did it in a bad way. So I ask for suggestions. $\endgroup$Indexed
with an index value of zero; however, that happens multiple times inside yourSum
expression. Second, you cannot construct your listeqns
starting from an empty list and assigning values to the nonexistent parts; you have to e.g.AppendTo
a list (may be inefficient), or use aReap
/Sow
method... $\endgroup$