Taking advantage of linearity of integration in Mathematica

328 Views Asked by At

I want to evaluate an integral of form given below

$$\int\limits_\alpha^\beta (f(x) + g(x) + h(x) + ...) dx$$

When I give it to Mathematica it takes forever to evaluate. But if I give it in this form

$$\int\limits_\alpha^\beta f(x)dx + \int\limits_\alpha^\beta g(x)dx + \int\limits_\alpha^\beta h(x)dx + ...$$

It takes comparatively lesser time.

According to this page it can be defined as

integrate[y_ + z_, x_] :=
integrate[y, x] + integrate[z, x]

for two variables. But I want to be able to do this for arbitrary number of variables. How to is the question.

2

There are 2 best solutions below

4
On

For $$\int\limits_\alpha^\beta (f(x) + g(x) + h(x) + ...) dx$$

  In[1]:=  f[x_]:= your definition 
  In[2]:=  g[x_]:= your definition
  In[3]:=  h[x_]:= your definition
  In[4]:=  F={f[x],g[x],h[x]}
  In[5]:=  Sum[Integrate[F[[i]],{x,a,b}], {i, 1, 3}]

This does what you want, i.e integrates the $f,g,h\cdots$ and then adds them, rather than adding and then integrating. Tested on Mathematica 7

0
On

I just noticed this question, so please forgive the (very) late reply.

If you want a function that will automatically split across addition, like you've tried to define, I'd do this

Clear[integrate]
integrate[a_Plus, x_, opts:OptionsPattern[]] := 
  integrate[#, x, opts]& /@ a

which with input

integrate[a + b + c, {x, 0, 5}]

gives

integrate[a, {x, 0, 5}] + integrate[b, {x, 0, 5}] 
  + integrate[c, {x, 0, 5}]

Then, you can define

integrate[a_, x_, opts:OptionsPattern[]]:= Integrate[a, x, opts]

to map it back to the original function.