1
$\begingroup$

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?

$\endgroup$
6
  • $\begingroup$ What is the difference between this question and the last question that you linked to? $\endgroup$
    – C. E.
    Commented Jun 1, 2015 at 10:48
  • $\begingroup$ @Pickett the last question was "What functions can do that?", this one - what I'm doing wrong with these functions, or how to code it in a better way? $\endgroup$
    – Hedin
    Commented Jun 1, 2015 at 11:25
  • $\begingroup$ So basically how to do it without using CoefficientArrays? Your first sentence is confusing in this regard ("through a Do-cycle and CoefficientArrays") $\endgroup$
    – C. E.
    Commented Jun 1, 2015 at 11:32
  • 1
    $\begingroup$ @Pickett I tender an apology, if I expressed myself with ambiguity. I've tried to apply method from the last question's answer to my system of linear equations. Nevertheless, I grappled with a problem on that way. In the first block code I want to make a list with equations, in the second one - take coefficients from this list into sparse array. I can set equations and variables manually for three or four of them, but I have to deal with fifty or more. I need some "automatization" with Do, List and CoefficientArrays, but I did it in a bad way. So I ask for suggestions. $\endgroup$
    – Hedin
    Commented Jun 1, 2015 at 12:15
  • 1
    $\begingroup$ One first, simple problem is the fact that you cannot use Indexed with an index value of zero; however, that happens multiple times inside your Sum expression. Second, you cannot construct your list eqns 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 a Reap / Sow method... $\endgroup$
    – MarcoB
    Commented Jun 1, 2015 at 23:33

0

Your Answer

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

Browse other questions tagged or ask your own question.