Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I want to evaluate

Plot[Integrate[Sin[((1 + 1/2) x)] (1/(2 Sin[(x/2)]) - 1/x), {x, 0, t}], {t, 0, 5}]

but it takes ages to load on Corei3 computer with Mathematica 9.

I'm still a novice when it comes to using Mathematica. Is there a way to get this expression to be evaluated faster? (I'm not used to waiting more than a minute while computing, that's why it feels odd to me).

share|improve this question

marked as duplicate by Jens, ciao, Dr. belisarius, bobthechemist, m_goldberg Apr 7 '14 at 1:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

I can't answer the "Why" question but fun[t_] = Integrate[Sin[((1 + 1/2) x)] (1/(2 Sin[(x/2)]) - 1/x), {x, 0, t}]; Plot[fun[t], {t, 0, 5}] is fast while doing everything at once is very slow indeed. – Öskå Apr 6 '14 at 13:28
Strongly related: How to speed up the plot of NIntegrate? – Jacob Akkerboom Apr 6 '14 at 14:36
up vote 5 down vote accepted

Oska nailed it in the comments before me

The trick here is to evaluate the integral only once. In your code it is being evaluated once for every point. You can evaluate the following

Integrate[Sin[((1 + 1/2) x)] (1/(2 Sin[(x/2)]) - 1/x), {x, 0, t}]

The outcome of this is

ConditionalExpression[1/2 (t + 2 Sin[t] - 2 SinIntegral[(3 t)/2]), 
 t \[Element] Reals]

You know that t will always be a real number (at least in your plot), so you can just copy paste the first argument of this ConditionalExpression and make a function out of it.

func[t_] := 1/2 (t + 2 Sin[t] - 2 SinIntegral[(3 t)/2])

We can then do

Plot[func[t], {t, 0, 5}]

which gives

enter image description here

To define the function "automatically", you could use the code in Oska's answer. I slightly prefer the following

func[t_] := Evaluate@
  Integrate[Sin[((1 + 1/2) x)] (1/(2 Sin[(x/2)]) - 1/x), {x, 0, t}, 
   Assumptions -> t \[Element] Reals]

Which gets rid of the ConditionalExpression and uses SetDelayed rather than Set. The code only works if t does not have value, so watch out.

share|improve this answer

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