2
$\begingroup$

I have a problem in defining a function in a module. I looked at Mathematica help and followed the same procedure.

ClearAll["Global`*"];
L = 4;
z1 = L/3;
Y = 2*10^11;
Iyy = 8.333*10^-6;
A = 0.1^2;
kb = (Y*Iyy)/L^3;
\[Rho] = 7850;
W1 = A1*Sin[b*x] + B1*Cos[b*x] + C1*Sinh[b*x] + E1*Cosh[b*x];
W2 = A2*Sin[b*(x - z1)] + B2*Cos[b*(x - z1)] + C2*Sinh[b*(x - z1)] + 
   E2*Cosh[b*(x - z1)];
(*SS BC*)
(*CANTILEVER BC*)
bcd0 = W1 /. {x -> 0};
bcm0 = (D[W1, {x, 1}]) /. {x -> 0};
bcdl = (D[W2, {x, 2}]) /. {x -> L};
bcml = (D[W2, {x, 3}]) /. {x -> L};

ccd1 = (W1 /. {x -> z1}) - (W2 /. {x -> z1});
ccs1 = ((D[W1, {x}]) /. {x -> z1}) - ((D[W2, {x}]) /. {x -> z1});
ccm1 = ((D[W1, {x, 2}]) /. {x -> z1}) - ((D[W2, {x, 2}]) /. {x -> z1});
ccsh1 = ((D[W1, {x, 3}]) /. {x -> z1}) - ((D[W2, {x, 3}]) /. {x -> 
       z1}) - K*(W2 /. {x -> z1});

(*Forming matrix*)
R = Normal@
   CoefficientArrays[{bcd0, bcm0, bcdl, bcml, ccd1, ccs1, ccm1, 
      ccsh1}, {A1, B1, C1, E1, A2, B2, C2, E2}][[2]];
eq1 = FullSimplify[Det[R]];
f = Function[{K, b}, eq1]
f[3, 2](*Not working but why*)
g[K_, b_] := eq1;
g[3, 2](*Not working but why*)
c[x_] := x^2
c[4](*working but why*)   

When I tried to write these functions inside a Module I started getting an error. So I started writing outside the Module and found that my Function definition itself is not correct. I don't know where I am making a mistake. I don't know why the Function f and g cannot evaluated at f[3,2] and g[3,2].

$\endgroup$
3
$\begingroup$

Attributes[Function] contains HoldAll, which prevents the evaluation of eq1 when the function is defined. When the function is called, eq1 is evaluated after the argument replacements which is too late.

You could write

f = Function[{K, b}, Evaluate@eq1];
f[3, 2]

or

(f = Function[{K, b}, #]) &[eq1];
f[3, 2]

Note that K has a built-in meaning (dummy-variable for integrations, e.g. look at the output of DSolve[y[x] == g[x] + y'[x], y , x]) and should not be used as a variable.

$\endgroup$
  • $\begingroup$ Suppose if I want to convert one of the output into a function, say I get one of the output as eq1 which is having a dependency on k and b. And now i want to use eq1 to define a function? how to do that. Why the procedure give in mathematica help is not working. $\endgroup$ – acoustics May 11 '18 at 10:18
  • $\begingroup$ @vijay Then write func = Function[{k, b}, Evaluate@Out[xxx]]. Which procedure in the documentation do you talk about? $\endgroup$ – Coolwater May 11 '18 at 10:23
  • $\begingroup$ Go to Mathematica help and type defining Functions there are some example i followed those $\endgroup$ – acoustics May 11 '18 at 10:29

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.