0
$\begingroup$

Considering inputs:

vti={0, 1.5, 2.4, 5.0675, 7.735, 10};
Vscc=1.5; PSA=0.025; f=0.6; m=10;

I defined a function "pwtemp2", using "FC1" and "FC2":

    FC1[t_] := Count[vti, x_ /; (t - Vscc) \[LessSlantEqual] x < t];
    FC2[t_] := Count[vti, x_ /; (t - f Vscc) \[LessSlantEqual] x < t];

pwtemp2[t_] := 
  Piecewise[{{PSA^FC1[t], 
     Vscc \[LessSlantEqual] t < 2 Vscc}, {PSA^FC2[t], 
     2 Vscc \[LessSlantEqual] t < m}}];

When I Plot pwtemp2, its OK:

Plot[pwtemp2[t], {t, 0, m}, PlotRange -> Full, Filling -> Axis, 
 PlotTheme -> "Detailed"]

enter image description here

I also have this Piecewise function pwtemp1 (its big, but here could be any simple Piecewise, not defined as a pattern function):

pwtemp1=Piecewise[{{Piecewise[{{1.04819277 - E^(-0.03150354*(-1.5 + t)), 
         Inequality[0, LessEqual, -1.5 + t, Less, 1.5]}, 
       {1.002358723234263 - E^(-0.03150354*(-3. + t)), 
         Inequality[1.5, LessEqual, -1.5 + t, Less, 2.4]}, 
       {1.000757843243746 - E^(-0.03150354*(-3.9 + t)), 
         Inequality[2.4, LessEqual, -1.5 + t, Less, 5.0675]}, 
       {1.0020339850944202 - E^(-0.03150354*(-6.5675 + t)), 
         Inequality[5.0675, LessEqual, -1.5 + t, Less, 
           7.734999999999999]}, {1.002065888640687 - 
           E^(-0.03150354*(-9.235 + t)), 
  Inequality[7.734999999999999, 
           LessEqual, -1.5 + t, Less, 10]}}, 0], 
   Inequality[1.5, LessEqual, t, Less, 3.]}, 
 {Piecewise[{{1.04819277 - E^(-0.03150354*(-0.8999999999999999 + 
                   t)), 
  Inequality[0, LessEqual, -0.8999999999999999 + t, 
           Less, 1.5]}, {1.002358723234263 - 
   E^(-0.03150354*(-2.4 + t)), 
         Inequality[1.5, LessEqual, -0.8999999999999999 + t, Less, 
           2.4]}, {1.000757843243746 - E^(-0.03150354*(-3.3 + t)), 
         Inequality[2.4, LessEqual, -0.8999999999999999 + t, Less, 
           5.0675]}, {1.0020339850944202 - 
           E^(-0.03150354*(-5.967499999999999 + t)), 
         
  Inequality[5.0675, LessEqual, -0.8999999999999999 + t, Less, 
           7.734999999999999]}, {1.002065888640687 - 
           E^(-0.03150354*(-8.635 + t)), 
  Inequality[7.734999999999999, 
           LessEqual, -0.8999999999999999 + t, Less, 10]}}, 0], 
   Inequality[3., LessEqual, t, Less, 10]}}, 0]

When I Plot pwtemp1, its OK:

Plot[pwtemp1, {t, 0, m}, PlotRange -> Full, Filling -> Axis, PlotTheme -> "Detailed"]

enter image description here

NOW, the core point! I simple need to calculate the Integral of the multiplication of "pwtemp1" and "pwtemp2". Both pwtemp1 and pwtemp2 are somehow in function of "t", however, pwtemp2 is explicity defined as "pwtemp2[t_]:=" in order to make possible the recursive evaluation of FC1 and FC2. pwtemp1 is a Piecewise function, and inside of it, also have the variable "t". But, when I try:

NIntegrate[pwtemp1 pwtemp2[t], {t, 0, m}]/m
0.0372106

It returns a WRONG value, that represents only the Integral of pwtemp1. To confirm, when I evaluate as below, returns the same result! WHY?

NIntegrate[pwtemp1, {t, 0, m}]/m
0.0372106

And the weird is that when I Plot the same expression that I tried to calculate the Integral, the Plot returns the correct graph. Note that 0.0372106 could not be the Integral result (divided by m=10 to get the Average), as far as visually the Integral result should be less that that.

Plot[pwtemp1 pwtemp2[t], {t, 0, m}, PlotRange -> Full, 
 Filling -> Axis, PlotTheme -> "Detailed"]

enter image description here

$\endgroup$
4
  • $\begingroup$ Please add values for $m$ and for $f$ to your code. I am not able to reproduce your first plot when I copy/paste your code into a fresh notebook. $\endgroup$
    – LouisB
    Nov 14, 2021 at 0:06
  • $\begingroup$ Similar crossposted here. $\endgroup$ Nov 14, 2021 at 1:57
  • $\begingroup$ Yes, Rohit, thank you. It is me making the same question in another Forum. $\endgroup$
    – drexalves
    Nov 16, 2021 at 11:06
  • $\begingroup$ Thank you LouisB, updated. f=0.6 and m=10; $\endgroup$
    – drexalves
    Nov 16, 2021 at 11:07

1 Answer 1

1
$\begingroup$

The problem is that pwtemp2 must be evaluated only when t is numeric, since the Count expressions in the definition of FC1 and FC2 don't work when t is not numeric. Your definition:

pwtemp2[t_] := Piecewise[
    {
    {PSA^FC1[t], Vscc \[LessSlantEqual] t < 2 Vscc},
    {PSA^FC2[t], 2 Vscc \[LessSlantEqual] t < m}
    }
]

With this definition:

pwtemp2[t] //InputForm

Piecewise[{{1., Inequality[1.5, LessEqual, t, Less, 3.] || Inequality[3., LessEqual, t, Less, 10]}}, 0]

which is not what you wanted. So, instead do:

Clear[pwtemp2]

pwtemp2[t_?NumericQ] := Piecewise[
    {
    {PSA^FC1[t], Vscc \[LessSlantEqual] t < 2 Vscc},
    {PSA^FC2[t], 2 Vscc \[LessSlantEqual] t < m}
    }
]

Then:

NIntegrate[pwtemp1 pwtemp2[t], {t, 0, m}]/m
NIntegrate[pwtemp1, {t, 0, m}]/m

NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small.

NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in t near {t} = {7.73885}. NIntegrate obtained 0.13804431935748623 and 0.000049087052203923556 for the integral and error estimates.

0.0138044

0.0372106

return different values.

$\endgroup$
1
  • $\begingroup$ One can get rid of the convergence problem by manually including all the possible discontinuities (e.g., replace {t, 0, m} by Evaluate@ Flatten@{t, Union@ Select[Flatten@N@{0, m, vti, vti + Vscc, vti + f Vscc}, 0 <= # <= m &]} -- might be a little overkill but yet sufficient) $\endgroup$
    – Michael E2
    Jan 13, 2022 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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