0
$\begingroup$

I would like to understand why Mathematica produces the following output with very large numbers and how to avoid it. I suspect it has to do with how Mathematica handles precision.

I define

ga1[t_] =
  Piecewise[
    {{0, t < 0},
     {Sin[2 Pi t/(4*0.12)], t <= 0.12},
     {Sin[Pi/2. + 2.0 Pi (t - .12)/(4.*0.3)], t <= .42},
     {0, t <= .58},
     {-Sin[2 Pi (t - .58)/(4*0.12)], t <= 0.70},
     {-Sin[Pi/2. + 2.0 Pi (t - .7)/(4.*0.3)], t <= 1}},
    0];

ga[t_] = 
  Piecewise[{{ga1[t], t < 1}, {-ga1[t - 1], t >= 1}, {0, t >= 2}}, 0];

Although it may look nasty, it is quite simple

Plot[ga[t],{t,0,2}

Then I need its integral

qt[t_] = 
  Module[{s}, 
    FullSimplify[Integrate[ga[s], {s, 0, t}, Assumptions -> t ∈ Reals]]]
\[Piecewise]  0.190986 +0.190986 Cos[2.0944 -5.23599 t]   0.7<t<=1.
-0.190986-0.190986 Cos[7.33038 -5.23599 t]  1.7<t<=2.
0.0763944 (-1.+Cos[13.09 -13.09 t]) 1.<t<=1.12
-0.26738    1.42<t<=1.58
-0.0763944+0.190986 Cos[4.29351 -5.23599 t] 1.12<t<=1.42
-0.190986-0.0763944 Cos[20.6822 -13.09 t]   1.58<t<=1.7
0.26738 0.42<t<=0.58
0.0763944 -0.190986 Cos[0.942478 +5.23599 t]    0.12<t<=0.42
0.190986 +0.0763944 Cos[7.59218 -13.09 t]   0.58<t<=0.7
0.152789 Sin[6.54498 t]^2   0.<t<=0.12
0.  True

Which again looks fine, as does its square.

Plot[qt[t]^2, {t, 0, 2}]

plot

However, the integral of its square then contains very large numbers,

f[t_] = Module[{s}, FullSimplify[Integrate[qt[s]^2, {s, 0, t}, 
Assumptions -> t \[Element] Reals]]]
\[Piecewise]  0.  t>2.||t<=0
0.0582234   t==1.58
-0.0170727+0.0714922 t  0.42<t<=0.58
8.04759*10^44+2.02595*10^46 t   1.42<t<1.58
-1.54413*10^45-2.02595*10^46 t-2.63796*10^43 Sin[41.3643 -26.1799 t]    1.58<t<=1.7
0.00154447 +0.0393937 t-0.000111461 Sin[15.1844 -26.1799 t]-0.00222923 Sin[7.59218 -13.09 t]    0.58<t<=0.7
(-0.234137+0.629205 t+(-0.0104414+0.0273567 t) Cos[8.37758 -20.944 t]+(-0.0835312+0.218854 t) Cos[6.28319 -15.708 t]+(-0.292359+0.765988 t) Cos[4.18879 -10.472 t]-0.584718 Cos[2.0944 -5.23599 t]+1.53198 t Cos[2.0944 -5.23599 t]-0.0492421 Sec[1.0472 -2.61799 t]^4+(0.123105 t+(-0.016414+0.0410351 t) Cos[4.18879 -10.472 t]+(-0.0656561+0.16414 t) Cos[2.0944 -5.23599 t]) Sec[1.0472 -2.61799 t]^4-0.000435396 Sin[12.5664 -31.4159 t]-0.00696633 Sin[10.472 -26.1799 t]-0.0400564 Sin[8.37758 -20.944 t]-0.118428 Sin[6.28319 -15.708 t]-0.197234 Sin[4.18879 -10.472 t]-0.167192 Sin[2.0944 -5.23599 t])/(3. +Cos[4.18879 -10.472 t]+4. Cos[2.0944 -5.23599 t])^2  0.7<t<=1.
-3.87872*10^45+9.49666*10^45 t+3.16555*10^45 Sin[4.29351 -5.23599 t]    1.12<t<=1.42
(-7.8126*10^47+2.22224*10^47 t+(-4.44448*10^46+8.88896*10^45 t) Cos[29.3215 -20.944 t]+(-3.55558*10^47+7.11117*10^46 t) Cos[21.9911 -15.708 t]+2.84447*10^47 Cos[14.6608 -10.472 t]+2.84447*10^47 t Cos[14.6608 -10.472 t]+3.62669*10^48 Cos[7.33038 -5.23599 t]+6.40005*10^47 t Cos[7.33038 -5.23599 t]+(7.11117*10^46 t+(-3.55558*10^46+1.77779*10^46 t) Cos[14.6608 -10.472 t]+(-1.42223*10^47+7.11117*10^46 t) Cos[7.33038 -5.23599 t]) Sec[3.66519 -2.61799 t]^4-2.7778*10^44 Sin[43.9823 -31.4159 t]-5.5556*10^45 Sin[36.6519 -26.1799 t]-1.77779*10^46 Sin[29.3215 -20.944 t]-2.7778*10^46 Sin[21.9911 -15.708 t]-1.60279*10^47 Sin[14.6608 -10.472 t]-1.15556*10^47 Sin[7.33038 -5.23599 t]-4.44448*10^45 Sin[7.33038 -5.23599 t])/(3. +Cos[14.6608 -10.472 t]+4. Cos[7.33038 -5.23599 t])^2    1.7<t<=2.
-9.16584*10^44+3.79867*10^45 t+3.16555*10^44 Sin[13.09 (-1.+t)]-1.97847*10^43 Sin[26.1799 (-1.+t)]  1.<t<=1.12
0.00875415 t-0.00089169 Sin[13.09 t]+0.000111461 Sin[26.1799 t] 0.<t<=0.12
0.002843 +0.0240739 t-0.00557307 Sin[0.942478 +5.23599 t]+0.00174158 Sin[1.88496 +10.472 t] True

and the plot is

Plot of f

whereas the plot from using NIntegrate is more reasonable

fn[t_?NumericQ] := NIntegrate[qt[s]^2, {s, 0, t}];
Plot[fn[t], {t, 0, 2}]

Numerical Integration

I guess I could work around by defining variables for all numbers until the end, but it seems a bit superfluous for my purposes.

$\endgroup$
  • $\begingroup$ The function you define is not the function you plotted. Is this intentional? $\endgroup$ – yohbs Jul 20 '17 at 14:57
  • $\begingroup$ Try flattening the nested piecewise definitions by defining ga[t_] = PiecewiseExpand[ Piecewise[{{ga1[t], t < 1}, {-ga1[t - 1], t >= 1}, {0, t >= 2}}, 0]]. $\endgroup$ – b.gates.you.know.what Jul 20 '17 at 14:59
  • $\begingroup$ @b.gatessucks: I did try flattening after posting, but that didn't make a difference. $\endgroup$ – Sooner Jul 20 '17 at 15:13
  • $\begingroup$ It seems to do it for me, I get the same plot for f as yours for fn. $\endgroup$ – b.gates.you.know.what Jul 20 '17 at 15:15
  • $\begingroup$ @yohbs : which plot are you thinking about? The plots are of ga[t], qt[t]^2, f[t] and fn[t]. $\endgroup$ – Sooner Jul 20 '17 at 15:15
1
$\begingroup$
ga1[t_] = Piecewise[{{0, t < 0}, {Sin[2 Pi t/(4*0.12)], 
 t <= 0.12}, {Sin[Pi/2. + 2.0 Pi (t - .12)/(4.*0.3)], 
 t <= .42}, {0, t <= .58}, {-Sin[2 Pi (t - .58)/(4*0.12)], 
 t <= 0.70}, {-Sin[Pi/2. + 2.0 Pi (t - .7)/(4.*0.3)], t <= 1}}, 0];

ga[t_] = PiecewiseExpand[
  Piecewise[{{ga1[t], t < 1}, {-ga1[t - 1], t >= 1}, {0, t >= 2}}, 0]];
qt[t_] = Integrate[ga[s], {s, 0, t}, Assumptions -> t \[Element] Reals];
f[t_] = Integrate[qt[s]^2, {s, 0, t}, Assumptions -> t \[Element] Reals];

Plot[f[t], {t, 0, 2}]

plot

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Doesn't work for me. Also tried adding PiecewiseExpand around qt[s]^2 in the last integration to no avail. $\endgroup$ – Sooner Jul 20 '17 at 15:47
0
$\begingroup$

There is no problem if all numbers are exact, e.g.

ga1[t_] = Piecewise[{{0, t < 0}, {Sin[2 Pi t/(4*12/100)], 
 t <= 12/100}, {Sin[Pi/2 + 2 Pi (t - 12/100)/(4*3/10)], 
 t <= 42/100}, {0, t <= 58/100}, {-Sin[2 Pi (t - 58/100)/(4*12/100)], 
 t <= 7/10}, {-Sin[Pi/2 + 2 Pi (t - 7/10)/(4*3/10)], t <= 1}}, 0];

ga[t_] = PiecewiseExpand[Piecewise[{{ga1[t], t < 1}, {-ga1[t - 1],
t >=1}, {0, t >= 2}}, 0]];

qt[t_] = Module[{s}, FullSimplify[Integrate[ga[s], {s, 0, t}, 
Assumptions -> t \[Element] Reals]]]

f[t_] = Module[{s}, FullSimplify[Integrate[PiecewiseExpand[qt[s]^2], {s, 0, t}, 
 Assumptions -> t \[Element] Reals]]]

Plot[f[t], {t, 0, 2}]

enter image description here

Not sure I fully understand why. In the exact result no numbers ever come near close to as big as in my original post.

|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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