# why does this function fail to return? [closed]

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]]

• 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. – MarcoB Jul 17 '16 at 6:05
• 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] – happy fish Jul 17 '16 at 6:05
• Possible duplicate: (25507) – Mr.Wizard Jul 17 '16 at 8:54

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.

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