Message Boards Message Boards

Integrate doesn't seems to work

Posted 2 years ago

Please, I have two different functions "pwtemp1" and "pwPAMV[t]" (see notebook below). Anyone knows why when I Integrate each one, that have different values (see plot), the integrations returns the same value? Seems that the integration for "pwtemp'" is OK, but for "pwPAMV[t]" its NOT OK. "pwPAMV[t]" shall be the multiplication of functions "pwtemp1" and "pwtemp2[t]". PS: I divided the Integration result by m=10 in order to get a Average value along the axis x. Please help me find what I am missing.

15 Replies

Have you tried MovingMap and similar functions that work with temporal data? I have never used them, but from a first reading they sound similar to what you are after.

POSTED BY: Gianluca Gorni

Yes, I have tried MovingMap before, but I could not get what I want. Reading the Help I thought that it would be a option... But I dont have much experience.

That plot is wrong. It is not the plot of pwPAMV. If you ask ?pwPAMV you will see that its definition contains the replacement t->ut, which is treated badly by Plot, and correctly by NIntegrate.

POSTED BY: Gianluca Gorni

Thank you. But could you see any solution to get the Integral of what was wrongly plotted? How I get the correct function that represents what is plotted?

There is one more problem with your code: Count does not play well with symbolic manipulation:

Clear[f];
f[t_] := Count[{1, 2, 3}, x_ /; (t - 2) \[LessSlantEqual] x < t];
f[3]
f[t] /. t -> 3

This means that your functions FC1, FC2 and those built with these, must be calculated only with numerical values of t. If you feed them a symbolic value, they give nonsensical results. Here is a reworking of your code that corrects for the problem:

pwPA[t_] = 
  Piecewise[
   Partition[
    Riffle[vf, 
     Table[
      vti[[i]] \[LessSlantEqual] t < vti[[i + 1]], {i, 1, ni + 1}]], 
    2]];

pwtemp1[t_] = 
  Piecewise[{{pwPA[t - Vscc], 
     Vscc \[LessSlantEqual] t < 2 Vscc}, {pwPA[t - f Vscc], 
     2 Vscc \[LessSlantEqual] t < m}}];

FC1[t_?NumericQ] := 
  Count[vti, x_ /; (t - Vscc) \[LessSlantEqual] x < t];
FC2[t_?NumericQ] := 
  Count[vti, x_ /; (t - f Vscc) \[LessSlantEqual] x < t];
pwtemp2[t_?NumericQ] := 
  Piecewise[{{PSA^FC1[t], 
     Vscc \[LessSlantEqual] t < 2 Vscc}, {PSA^FC2[t], 
     2 Vscc \[LessSlantEqual] t < m}}];

pwPAMV[ut_?NumericQ] := pwtemp1[ut] pwtemp2[ut];

pl1 = Plot[pwtemp1[tt], {tt, 0, m}, PlotRange -> Full, Filling -> Axis]
NIntegrate[pwtemp1[tt], {tt, 0, m}]/m
pl2 = Plot[pwPAMV[tt], {tt, 0, m}, PlotRange -> Full, 
  Filling -> Axis]
NIntegrate[pwPAMV[tt], {tt, 0, m}]/m

In a previous post I suggested using DiracDelta instead of Count. That would allow symbolic manipulation.

POSTED BY: Gianluca Gorni

I tried to solve via your tip from DiracDelta instead of Count, but I could not get in the correct solution yet.

The "vti" list (without the first and last elements) is the time in years when a certain event occurrs. pwtemp2 should represent a constant (PSA) elevated to the power of (nr. of events inside a moving time window with length Vscc, or f Vscc, depending of t)).

This seems to work:

FC1alternative[t_] = 
 Integrate[Sum[DiracDelta[t - k], {k, vti}], {t, t - Vscc, t}, 
    Assumptions -> Element[t, Reals]] /. HeavisideTheta -> UnitStep //
   PiecewiseExpand
Plot[FC1[t], {t, -1, 12}, PlotRange -> {-.1, 3.1}]
Plot[FC1alternative[t], {t, -1, 12}, PlotRange -> {-.1, 3.1}, 
 Exclusions -> None]
POSTED BY: Gianluca Gorni

Success... Thank you so much Gianluca Gorni! With your DiracDelta solution I was able to calculate the Integral that I needed! Case closed!

You forgot to divide by m. Anyway, your "functions" are awfully convoluted in their definition and Plot gets them in the wrong evaluation order. Insert Evaluate and see what happens:

Plot[pwPAMV[tt], {tt, 0, m}, PlotRange -> Full, Filling -> Axis]
Plot[Evaluate[pwPAMV[tt]], {tt, 0, m}, PlotRange -> Full, 
 Filling -> Axis]

Try this simpler example, where it is easier to understand what happens:

f := Sin[x] /. x -> Pi/2 - x;
Plot[f, {x, 0, 2 Pi}]
Plot[Evaluate[f], {x, 0, 2 Pi}]
POSTED BY: Gianluca Gorni

Thank you Gianluca, I understood your Sin example for Plot. However, I still do not understand how can I get the Integral of this graph below (that represents the function that I want in the end), divided by m=10, that should be some value near 0.0004:

enter image description here

Here I get different integrals

Vscc = 1.5; f = 0.6;
\[Tau]1 = Vscc; \[Tau]2 = f Vscc; \[Tau]3m = f Vscc; m = 10;
PAI = 0.04819277; \[Lambda] = 0.03150354; PSA = 0.025; ni = 
 IntegerPart[(m - \[Tau]1 - \[Tau]2)/\[Tau]3m] + 2;
vti = Flatten[
   Append[Append[{0, \[Tau]1, \[Tau]1 + \[Tau]2}, 
     Table[\[Tau]1 + \[Tau]2 + (i - 2) \[Tau]3m, {i, 3, ni}]], m]];
F[t_] = 1 - Exp[-\[Lambda] t];
vf = {F[t] + PAI};
Do[vf = Append[vf, 
    F[t - vti[[i]]] + PSA (vf[[i - 1]] /. t -> vti[[i]])], {i, 2, 
   ni + 1}];
pwPA = Piecewise[
   Partition[
    Riffle[vf, 
     Table[vti[[i]] \[LessSlantEqual] t < vti[[i + 1]], {i, 1, 
       ni + 1}]], 2]];

pwtemp1 = 
  Piecewise[{{pwPA /. t -> (t - Vscc), 
     Vscc \[LessSlantEqual] t < 2 Vscc}, {pwPA /. t -> (t - f Vscc), 
     2 Vscc \[LessSlantEqual] t < m}}];

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

pwPAMV[ut_] := (pwtemp1 /. t -> ut) pwtemp2[ut];

    pl1 = Plot[(pwtemp1 /. t -> tt), {tt, 0, m}, PlotRange -> Full, Filling -> Axis]
    NIntegrate[pwtemp1, {t, 0, m}]
    pl2 = Plot[pwPAMV[tt], {tt, 0, m}, PlotRange -> Full, Filling -> Axis]
    NIntegrate[pwPAMV[tt], {tt, 0, m}]/m
POSTED BY: Hans Dolhaine
Posted 2 years ago

Thank you Mr. Hans Dolhaine, but we can see by the Plot pl2, that the result of the Nintegrate, divided by m=10, should be something near 0.0004 (the average value of pl2 between 0 and 10), and your solution resulted in the value 0.0210715, that is out of what we would expect for the average.

POSTED BY: Updating Name

Hello Andre Luiz,

in my opinion the main problem is the absolute complex structure of your code.

Did you consider to use something like this?

Plot[
 {
  Piecewise[
   {
    {0, x < 3},
    {1 + 1.5 x, 3 <= x < 4},
    {SawtoothWave[{0, 1.3}, .9 x], 4 <= x < 9},
    {0, x < 9}
    }
   ]
  },
 {x, 0, 10},
 PlotStyle -> Red,
 PlotRange -> {0, 8},
 Filling -> Axis
 ]
POSTED BY: Hans Dolhaine
Posted 2 years ago

Similar crossposted here.

POSTED BY: Rohit Namjoshi

Your integrands seem to be equal

pwPAMV[t] - pwtemp1 // Simplify
POSTED BY: Hans Dolhaine
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract