2
$\begingroup$

I have stumbled upon an issue that seems very weird to me. I have two Piecewise functions that describe exactly the same curve. The only difference between them is that the first function has two intervals (0..80 and 80..100) where the second has only one (0..100). Plotting these functions takes 0.12 seconds for the first but 6.5 seconds for the second (Mathematica 11.3, Win7 x64).

f=Piecewise[{{1.*Sqrt[(1+1.*^-10*x)^2+1.0132118364233779*^-7*x^2],0<=x<=80.},
{1.000324183240269*Sqrt[(1+8.100541720337562*^-6*(-80.+x))^2+1.0118990353812073*^-7*(-80.+x)^2],80.<=x<=100.},
{1.0005064876533403*Sqrt[(1+4.58007351566221*^-6*(-100.+x))^2+2.0703132938331475*^-8*(-100.+x)^2],100.<=x<=122.},
{1.0006123124833644*Sqrt[(1+0.000011127214694901114*(-122.+x))^2+1.0107340215537729*^-7*(-122.+x)^2],122.<=x<=222.},
{1.0022307024014345*Sqrt[(1+0.000014605951017494362*(-222.+x))^2+4.776435618694173*^-8*(-222.+x)^2],222.<=x<=224.29087089812793},
{1.0022643630008223*Sqrt[(1+0.00002133630858410703*(-224.29087089812793+x))^2+1.0040864353174765*^-7*(-224.29087089812793+x)^2],224.29087089812793<=x<=324.29087089812793},
{1.0049048084438061*Sqrt[(1+0.00003125778378555373*(-324.29087089812793+x))^2+9.935747756614407*^-8*(-324.29087089812793+x)^2],324.29087089812793<=x<=490.29087089812793},
{1.0114866853138393*Sqrt[(1-0.007952708212499698*(-490.29087089812793+x))^2+9.679648197220001*^-8*(-490.29087089812793+x)^2],490.29087089812793<=x<=582.2908708981279},
{0.27297299309728995*Sqrt[(1-0.020124273502247485*(-582.2908708981279+x))^2+8.67952608343357*^-6*(-582.2908708981279+x)^2],582.2908708981279<=x<=588.6408708981279},
{0.23814476973915308*Sqrt[(1-0.031084737809954313*(-588.6408708981279+x))^2+0.000031501850318745966*(-588.6408708981279+x)^2],588.6408708981279<=x<=638.6408708981279},
{0.14794391037385346*Sqrt[(1+0.022146323222520223*(-638.6408708981279+x))^2+0.00004369849890959965*(-638.6408708981279+x)^2],638.6408708981279<=x<=640.6408708981279},
{0.15450911864022798*Sqrt[(1+0.04682422318350186*(-640.6408708981279+x))^2+0.00017778036511269715*(-640.6408708981279+x)^2],640.6408708981279<=x<=740.6408708981279}},0];
Plot[f,{x,0,740}]//AbsoluteTiming

==> 0.12 seconds

f=Piecewise[{{1.*Sqrt[(1+1.*^-10*x)^2+1.0132118364233779*^-7*x^2],0<=x<=100.},
{1.0005064876533403*Sqrt[(1+4.58007351566221*^-6*(-100.+x))^2+2.0703132938331475*^-8*(-100.+x)^2],100.<=x<=122.},
{1.0006123124833644*Sqrt[(1+0.000011127214694901114*(-122.+x))^2+1.0107340215537729*^-7*(-122.+x)^2],122.<=x<=222.},
{1.0022307024014345*Sqrt[(1+0.000014605951017494362*(-222.+x))^2+4.776435618694173*^-8*(-222.+x)^2],222.<=x<=224.29087089812793},
{1.0022643630008223*Sqrt[(1+0.00002133630858410703*(-224.29087089812793+x))^2+1.0040864353174765*^-7*(-224.29087089812793+x)^2],224.29087089812793<=x<=324.29087089812793},
{1.0049048084438061*Sqrt[(1+0.00003125778378555373*(-324.29087089812793+x))^2+9.935747756614407*^-8*(-324.29087089812793+x)^2],324.29087089812793<=x<=490.29087089812793},
{1.0114866853138393*Sqrt[(1-0.007952708212499698*(-490.29087089812793+x))^2+9.679648197220001*^-8*(-490.29087089812793+x)^2],490.29087089812793<=x<=582.2908708981279},
{0.27297299309728995*Sqrt[(1-0.020124273502247485*(-582.2908708981279+x))^2+8.67952608343357*^-6*(-582.2908708981279+x)^2],582.2908708981279<=x<=588.6408708981279},
{0.23814476973915308*Sqrt[(1-0.031084737809954313*(-588.6408708981279+x))^2+0.000031501850318745966*(-588.6408708981279+x)^2],588.6408708981279<=x<=638.6408708981279},
{0.14794391037385346*Sqrt[(1+0.022146323222520223*(-638.6408708981279+x))^2+0.00004369849890959965*(-638.6408708981279+x)^2],638.6408708981279<=x<=640.6408708981279},
{0.15450911864022798*Sqrt[(1+0.04682422318350186*(-640.6408708981279+x))^2+0.00017778036511269715*(-640.6408708981279+x)^2],640.6408708981279<=x<=740.6408708981279}},0];
Plot[f,{x,0,740}]//AbsoluteTiming

==> 6.5 seconds

$\endgroup$
  • $\begingroup$ They take the same amount of time for me (about 0.1 seconds). Mac OS 10.14.2, MMA V10.0.1. $\endgroup$ – march Dec 20 '18 at 18:34
  • $\begingroup$ same issue in Mathematica 11.3 ( Windows 10 x64) $\endgroup$ – kglr Dec 20 '18 at 19:21
  • $\begingroup$ I get the same result as kloppy with MMA 11.3 in MacOS 10.13.6. Interestingly, if I delete the last three pieces from both and, correspondingly, plot from x = 0 to x = 588, the situation reverses itself: it's 1.4 secs. for the first one, and 0.6 secs for the second. Also, if I add WorkingPrecision->30 to the Plot command in the second one (the unmodified version), I get an error that says: "The precision of the argument function...is less than WorkingPrecision (30. `)". Same if I try a very low value, i.e., WorkingPrecision->1. I don't get this with the first one. $\endgroup$ – theorist Dec 20 '18 at 20:04

Your Answer

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

Browse other questions tagged or ask your own question.