0
$\begingroup$

I'm trying to solve a problem where I can't do more than 1 calculation inside a Module here. I have a list of numbers where v={143.5,66.4,...} that needs to run into the Module and gives the result of each number entered in v.

My problem is when v={143.5}, I seems to get the answer that I'm looking for which is

{{{{{0.00134915}}, {{-1.00001}}, {{0.0112934}}, {{-16.9408}}}, 
{{{-0.499981}}, {{0.998664}}, {{-1765.75}}, {{-0.0113652}}}}}

When I set v={143.5,66.4,...}, instead I get an error of

Thread::tdlen: Objects of unequal length in -3033.72 {54.3206,117.395} {1/2 (15590. +σ^2)} cannot be combined.

I've tried checking my code but it seems to work with 1 input and only buggy when it's more than 1 input in v. What I'm trying to do is to calculate different value of q1 inside the module. Since q1=1/v where v={143.5, 66.4, 29.8, 5.6}. Meaning here there's 4 different q1 variables that need to be looped and calculated.I'm sorry beforehand that my code is messy:

k = 155900;
{rr2 = 2 k q1^2 q2 + 2 k q1 q2^2, 
  rr3 = q1^2 + 2 q1 q2 + (k^2 + 1) q2^2};
ee2 = k q1 q2;
ee3 = b2 /. Solve[2 b2 - σ^2 == 2 k* q2, {b2}];
oo5 = (-b1^2 + 2 (ee2) (ee3) == rr2);
{ee4, ee5} = b1 /. Solve[oo5, {b1}];
oo6 = (2 (ee2) + (ee3)^2 - 2 (ee5) σ == rr3);
oo8 = σ /. Solve[oo6, {σ}];
oo9 = Extract[oo8, {8}];

q2 = 0.05;
value1 = {b0 -> ee2, b1 -> ee5, b2 -> ee3};
value2 = {σ -> oo9};
calcJ[q1_] := 
 Module[{bd, X1, inverseX1, X2, X}, 
  bd = {{0, 0}, {0, 0}, {-q2, k q2}, {q1, 0}};
  X1 = {{-b0 q1 q2 σ, b0 q1 q2 - b1 q1 q2 σ, 
     b1 q1 q2 - b2 q1 q2 σ, 
     b2 q1 q2 - k  q1 q2^2 - 
      q1 q2 σ^2}, {b0 q1^2 σ, -b0 q1^2 + k^2 q1^2 q2^2 +
       b1 q1^2 σ, -b1 q1^2 + b2 q1^2 σ, -b2 q1^2 + 
      2 k q1^2 q2 + q1^2 σ^2}, {-b0 b2 q1 q2 + k  b0 q1 q2^2 + 
      b0 q1 q2 σ^2, -b1 b2 q1 q2 + k  b1 q1 q2^2 - 
      b0 q1 q2 σ + b1 q1 q2 σ^2, 
     b0 q1 q2 - b2^2 q1 q2 + k  b2 q1 q2^2 - b1 q1 q2 σ + 
      b2 q1 q2 σ^2, 
     b1 q1 q2 - 2 b2 q1 q2 σ + k  q1 q2^2 σ + 
      q1 q2 σ^3}, {b0 b2 q1^2 - 2 k b0 q1^2 q2 - 
      b0 q1^2 σ^2, 
     b1 b2 q1^2 - 2 k b1 q1^2 q2 + b0 q1^2 σ - 
      b1 q1^2 σ^2, -b0 q1^2 + b2^2 q1^2 - 2 k b2 q1^2 q2 + 
      k^2 q1^2 q2^2 + b1 q1^2 σ - 
      b2 q1^2 σ^2, -b1 q1^2 + 2 b2 q1^2 σ - 
      2 k q1^2 q2 σ - q1^2 σ^3}};
  inverseX1 = Inverse[X1];
  X2 = {{-b0 q1^2 - b0 q1 q2, -b1 q1^2 - b1 q1 q2, -b2 q1^2 - 
      b2 q1 q2 + k  q1^2 q2, -q1^2 σ - 
      q1 q2 σ}, {b0 q1^2 + b0 q1 q2 - k^2 q1^2 q2^2, 
     b1 q1^2 + b1 q1 q2, 
     b2 q1^2 + b2 q1 q2 - 2 k q1^2 q2 - k  q1 q2^2, 
     q1^2 σ + q1 q2 σ}, {0, -k  q1^2 q2, 
     0, -q1^2 - q1 q2}, {-b0 b1 + 2 b0 b2 σ - 
      2 k b0 q2 σ - b0 σ^3, -b1^2 - b0 b2 + 2 k b0 q2 - 
      k  q1 q2^2 + 2 b1 b2 σ - 2 k b1 q2 σ + 
      b0 σ^2 - b1 σ^3, -2 b1 b2 + 2 k b1 q2 - 
      b0 σ + 2 b2^2 σ - 2 k b2 q2 σ + 
      b1 σ^2 - b2 σ^3, 
     b0 - b2^2 + 2 k b2 q2 - q1 q2 - (k^2 + 1) q2^2 - 2 b1 σ + 
      3 b2 σ^2 - 2 k q2 σ^2 - σ^4}};
  X = X2.inverseX1;
  (-Transpose[bd].X) /. value1 /. value2]

v = {143.5,66.4,29.8,5.6};
q1 = 1/v;

calcJ /@ q1
$\endgroup$
1
$\begingroup$

The problem comes from your assignment of a list to q1 (second line from the end). It is used symbolically all over the place and that list gets inappropriately substituted into many expressions.

fix the last line to this:

 calcJ /@ 1/{143.5, 66.4, 29.8, 5.6}

Do not assign anything to q1. You should restart your kernel as well.

$\endgroup$
  • $\begingroup$ calcJ /@ 1/v perchance? $\endgroup$ – Edmund Feb 6 '18 at 21:45
  • $\begingroup$ @george2079, @Edmund, I've tried changing into your suggestion. Tested and the result I get is when v = {143.5}; calcJ /@ 1/v gives the result of (* {0.00696864} *) which is not the same as i'm looking above. What I'm trying to do is to calculate different value of q1 inside the module. Since q1=1/v where v={143.5, 66.4, 29.8, 5.6}. Meaning here there's 4 different q1 variables that need to be looped and calculated. $\endgroup$ – Zen 禅 Feb 7 '18 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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