# Module is taking too much of time to produce its results

I have written a function f using Module. What it does is take 6 inputs and output a plot. The module seems perfectly fine. I have constructed similar modules in the past to extract the plots, I used the same structure here but unable to execute it properly. The function I used involves solving some transcendental equation P which contains some six inputs.

ClearAll["Global*"];
W[1] = c[1]*Sin[b*x1] + c[2]*Cos[b*x1] + c[3]*Sinh[b*x1] +
c[4]*Cosh[b*x1];
W[2] = c[5]*Sin[b*(x1 - z[1])] + c[6]*Cos[b*(x1 - z[1])] +
c[7]*Sinh[b*(x1 - z[1])] + c[8]*Cosh[b*(x1 - z[1])];
W[3] = c[9]*Sin[b*(x1 - z[2])] + c[10]*Cos[b*(x1 - z[2])] +
c[11]*Sinh[b*(x1 - z[2])] + c[12]*Cosh[b*(x1 - z[2])];

(*a piecewise function is generated using W[1],W[2],W[3] *)
w = Piecewise[{{W[1], x1 <= z[1]}, {W[2], z[1] <= x1 <= z[2]}, {W[3],
x1 >= z[2]}}];

boundary[i_, j_] :=
Module[ {bc},
bc1 = {W[i] /. {x1 -> 0}, (D[W[i], {x1, 2}]) /. {x1 -> 0},
W[j] /. {x1 -> L1}, ((D[W[j], {x1, 2}]) /. {x1 -> L1})}; bc = bc1]

countinuity[i_, j_] :=
Module[{eq},
eq1 = {((W[i] /. x1 -> z[i]) - (W[j] /.
x1 -> z[i])), (((D[W[i], {x1}]) /.
x1 -> z[i]) - ((D[W[j], {x1}]) /.
x1 -> z[i])), (((D[W[i], {x1, 2}]) /.
x1 -> z[i]) - ((D[W[j], {x1, 2}]) /.
x1 -> z[i])), (((D[W[i], {x1, 3}]) /.
x1 -> z[i]) - ((D[W[j], {x1, 3}]) /. x1 -> z[i])) + (K[i]*
W[i] /. x1 -> z[i])}; eq = eq1 ]

(* the above two modules are used to bring create a list which \
consist of equation *)
e1 = boundary[1, 3];
e2 = countinuity[1, 2];
e3 = countinuity[2, 3];

(* using e1 e2, e3 and the associated coefficients are seperated to \
form  R matrix*)
eq = Flatten[{e1, e2, e3}];
var = Table[c[i], {i, 1, Length[eq]}];
R = Normal@CoefficientArrays[eq, var][[2]];
R = R /. {K[1] -> K1, K[2] -> K2};
MatrixForm[R];
(*P is the determinant of R matrix *)
P = -b^10 (Sinh[
b L1] (16 b^3 ((K1 + K2) Cos[b L1] - K1 Cos[b (L1 - 2 z[1])] -
K2 Cos[b (L1 - 2 z[2])]) + 2 (32 b^6 - K1 K2) Sin[b L1] +
K1 K2 (4 Sin[b (L1 - 2 z[1])] -
2 I Sin[b (L1 - 2 (z[1] + I z[2]))] -
4 Sin[b (L1 - 2 z[2])] +
4 Sin[b (L1 + 2 z[1] - 2 z[2])] + (1 + 2 I) Sin[
b (L1 - 2 I z[2])] + (1 - 2 I) Sin[b (L1 + 2 I z[2])] +
2 I Sin[b (L1 - 2 z[1] + 2 I z[2])] +

4 Cosh[b z[
2]] ((1 - 2 Cosh[2 b z[1]]) Cosh[b z[2]] Sin[b L1] +
8 Sin[b z[1]] Sin[b (L1 - z[2])] Sinh[b z[1]])) +
4 K1 (K2 Cos[b L1] - K2 Cos[b (L1 - 2 z[2])] +
4 b^3 Sin[b L1]) Sinh[2 b z[1]] +
4 K2 Sin[b L1] (4 b^3 + K1 Sinh[2 b z[1]]) Sinh[2 b z[2]]) -
Cosh[b L1] (-K1 K2 (10 Cos[b L1] - 6 Cos[b (L1 - 2 z[1])] -
4 Cos[b (L1 - 2 z[2])] - (1 - 2 I) Cos[b (L1 - 2 I z[2])] +
Cos[b (L1 - 2 z[1] - 2 I z[2])] - (1 + 2 I) Cos[
b (L1 + 2 I z[2])] + Cos[b (L1 - 2 z[1] + 2 I z[2])]) +
4 K1 Cosh[b z[1]]^2 (K2 Cos[b L1] - K2 Cos[b (L1 - 2 z[2])] +
4 b^3 Sin[b L1]) +
2 (2 b^3 (-2 (2 K1 + 3 K2) Sin[b L1] +
K2 (Sin[b (L1 - 2 I z[2])] + Sin[b (L1 + 2 I z[2])])) +
2 K2 Cosh[b z[2]]^2 (K1 Cos[b L1] -
K1 Cos[b (L1 - 2 z[1])] + 4 b^3 Sin[b L1] +
K1 Sin[b L1] Sinh[2 b z[1]]) +
K1 (2 (K2 Cos[b L1] - K2 Cos[b (L1 - 2 z[2])] +
4 b^3 Sin[b L1]) Sinh[b z[1]]^2 +
16 K2 Sin[b z[1]] Sin[b (L1 - z[2])] Sinh[b z[1]] Sinh[
b z[2]] +
K2 Sin[b L1] ((-3 + Cosh[2 b z[2]]) Sinh[2 b z[1]] -
2 Cosh[2 b z[1]] Sinh[2 b z[2]])))));

(* The determinant is solved for 'b' by giving six input*)
f[z1_, z2_, l1_, k1_, k2_, beta_] :=
Module[{m}, z[1] = z1; z[2] = z2 ; L1 = l1; K1 = k1; K2 = k2;
r = beta; s1 = P; s2 = NSolve[s1 == 0 && 0 < b < 30];
s3 = N[b /. s2];
s4 = s3[[r]]; {uu, ww, vv} =
SingularValueDecomposition[R /. b -> s4];
NN = Last[Transpose[vv]];   sub1 = Flatten[{var, b}];
sub2 = Flatten[{NN, s4}];
m = w /. Table[sub1[[i]] -> sub2[[i]], {i, 1, Length[sub1]}];
Return[m]];
n = 2;
comb = Tuples[{0, 1*^12}, 2];
(* The below function call f function do bring final result*)
g[i_, r_] :=
Module[{s5}, spring = comb[[i]]; n1 = spring[[1]];
n2 = spring[[2]];  n3 = r; s5 = f[0.25, 0.75, 1, n1, n2, n3];
Return[s5]]

beammodes1 = Table[g[i, 1], {i, 1, 2^n}]
beammodes2 = Table[g[i, 2], {i, 1, 2^n}]
beammodes = Flatten[{beammodes1, beammodes2}];

Table[Plot[beammodes[[i]], {x1, 0, L1}, PlotRange -> All], {i, 1,
Length[beammodes]}]

• Do you think Module is responsible? Does it take a long time outside Module`? There's a lot of stuff going on in your code. Could you add some comments on what it's supposed to be doing? – Chris K Aug 14 at 11:45
• Now I have added some comments – acoustics Aug 14 at 12:07