0
$\begingroup$

I have written 2 functions to calculate the stress and strain of a material, the strain function works perfectly, but the stress function which very similar to the Strain function did not return what I expected? Could you please shed some light on this issue?

Strain[neuaxis_, y_] := Module[{b = 0.003,a}, a = -b / neuaxis, a*y + b];

Stress[neuaxis_, y_] = Module[{temp, res, Es = 200*10^3, fsy = 500}, temp = 
Strain[neuaxis, y] * Es res = If[temp > fsy, fsy, If[temp < -fsy, -fsy, temp]] res]

The function Strain[30, 50] gives -0.002 which is fine, but my function Stress[30, 50] returns me the unsatisfactory result as below:

res$61948 If[temp$61948 > 500, fsy$61948, If[temp$61948 < -fsy$61948,
-fsy$61948, temp$61948]]
$\endgroup$
  • 1
    $\begingroup$ You are missing a ; after the res = If[...] statement. As a matter of fact, you really don't need the res variable at all, since If returns a value. $\endgroup$ – MarcoB Jul 17 '16 at 6:05
  • 1
    $\begingroup$ Strain[neuaxis_, y_] := Module[{b = 0.003, a}, a = -b/neuaxis; a*y + b]; Stress[neuaxis_, y_] := Module[{temp, res, Es = 200*10^3, fsy = 500}, temp = Strain[neuaxis, y]*Es; res = If[temp > fsy, fsy, If[temp < -fsy, -fsy, temp]] ; res] $\endgroup$ – happy fish Jul 17 '16 at 6:05
  • $\begingroup$ Possible duplicate: (25507) $\endgroup$ – Mr.Wizard Jul 17 '16 at 8:54
2
$\begingroup$

Your code has syntax errors. It should be

Strain[neuaxis_, y_] := Module[{b = 0.003, a}, a = -b/neuaxis; a*y + b]

Stress[neuaxis_, y_] := 
  Module[{temp, Es = 200*10^3, fsy = 500}, 
  temp = Strain[neuaxis, y]*Es;
  If[temp > fsy, fsy, If[temp < -fsy, -fsy, temp]]]

but I would write

With[{b = 0.003}, Strain[neuaxis_, y_] := b (1 - y/neuaxis)]

With[{Es = 200*10^3, fsy = 500},
  Stress[neuaxis_, y_] := Clip[Es Strain[neuaxis, y], {-fsy, fsy}]]

which will give the same results with much simpler code.

$\endgroup$
  • $\begingroup$ Thanks @m_goldberg for the suggestion, definitely it is much clearer than my original code. (I am a very newbie to Mathematica). Thanks ! $\endgroup$ – N.T.C Jul 17 '16 at 23:47

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