# Difficult on integration the normalization of a piecewise function

I have a little problem; I have defined the following Piecewise-function in the variables {x,L,w}:

f[x_, L_, w_] := Piecewise[{{x w (L - x), 0 <= x <= L}}];


Then I have defined the normalization of this function:

norm[L_, w_] := Integrate[f[x, L, w]^2, {x, 0, L},Assumptions -> (L > 0) \[And] (w > 0) \[And] (L >= x >= 0)];


(I have tried different forms of 'Assumptions' but the following problem persists). Then I defined the normalized function:

fnorm[x_, L_, w_] := f[x, L, w]/Sqrt[norm[L, w]];


When I tried to plot the previous normalized function, for some numerical values of variables {L,w}, many problems appears:

Plot[fnorm[x, 1, 1], {x, 0, 1}];
Integrate::ilim: Invalid integration variable or limit(s) in {0.0000204286,0,1}.
NIntegrate::itraw: Raw object 0.000020428571428571424 cannot be used as an iterator.
NIntegrate::itraw: Raw object 0.000020428571428571424 cannot be used as an iterator.
Integrate::ilim: Invalid integration variable or limit(s) in {0.0204286,0,1}.
NIntegrate::itraw: Raw object 0.02042859183673469 cannot be used as an iterator.
General::stop: Further output of NIntegrate::itraw will be suppressed during this calculation.
Integrate::ilim: Invalid integration variable or limit(s) in {0.0408368,0,1}.
General::stop: Further output of Integrate::ilim will be suppressed during this calculation.


Obviously the plot of the normalized function dosen't appeare. The plot,for a range of numerical value for {L,w}, of the normalization, appears:

Plot3D[norm[L, w], {L, 0.5, 1}, {w, 0.5, 1}]


The previos plot appears without problems.

Thanks for any tips and helps!

• Plot set x equal to a value, then calls fnorm[]. Try Evaluate[fnorm[x, 1, 1]]. — Somewhere on site this question is already answered in full. I’ll try to find it. Nov 27, 2021 at 13:53
• Nov 27, 2021 at 13:55
• One could also use = instead of := in defining norm[], since the symbolic integral can be done: norm[L_, w_] = Integrate[..]. If you want to keep :=, then best practice would be to localize x: norm[L_, w_] := Module[{x}, Integrate[..]]. The use of SetDelayed (:=) means the integral is recalculated every time norm[] is called, which would make the code quite slow probably. Nov 27, 2021 at 14:17

Redefine your functions by using different argument names:

f[x_, L_, w_] := (Print["x=", x];
Piecewise[{{x w (L - x), 0 <= x <= L}}]);
norm[L_, w_] :=
Integrate[f[x1, L, w]^2, {x1, 0, L},
Assumptions -> (L > 0) \[And] (w > 0) \[And] (L >= x >= 0)];
fnorm[x2_, L_, w_] := f[x2, L, w]/Sqrt[norm[L, w]];


Now, if we run this:

Plot[fnorm[x3, 1, 1], {x3, 0, 1}]


You get the output:

....

You see that Integrate and Plot call their functions with symbolic arguments. This is done to eventually simplify the expressions. But in your case "f" delivers not a number as result if called with a symbolic argument. Therefore, ensure that "f" is only called on numeric arguments by:

Clear[f, norm, fnorm]
f[x_?NumericQ, L_, w_] := Piecewise[{{x w (L - x), 0 <= x <= L}}];
norm[L_, w_] :=
Integrate[f[x1, L, w]^2, {x1, 0, L},
Assumptions -> (L > 0) \[And] (w > 0) \[And] (L >= x >= 0)];
fnorm[x2_, L_, w_] := f[x2, L, w]/Sqrt[norm[L, w]];
Plot[fnorm[x3, 1, 1], {x3, 0, 1}]
`

• Thanks for the answer! It works perfectly now. Nov 27, 2021 at 14:26