3

Plot[E^(-0.5 x) NIntegrate[Cos[t] E^(Cos[t] + 0.5 t), {t, 0, x}], {x, 0, 40}]

Mathematica evaluates this integral for each point, which takes a long time. It is possible to iterate each time step without going through the whole function. What is the correct way to do this?

Gappy Hilmore
  • 1,253
  • 1
  • 9
  • 17

1 Answers1

7

To flesh out GuessWho's suggestion:

sol[t_] = NDSolveValue[{Cos[t] E^(Cos[t] + 0.5 t) == f'[t], f[0] == 0}, f[t], {t, 0, 40}]

Show[
  Plot[E^(-0.5 x) NIntegrate[Cos[t] E^(Cos[t] + 0.5 t), {t, 0, x}], {x, 0, 40}]
  Plot[E^(-0.5 t) sol[t], {t, 0, 40}, PlotStyle -> {Red, Dashed}]
]

Mathematica graphics

The second plot is about 300 times faster than the first one.

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323