3
$\begingroup$

I have a function defined by $$f(t)=\int\limits_0^t g(x) dx$$ $g(x)$ is a complicated function (specifically the Mittag-Leffler function which has recently been implemented in Mathematica 9 as MittagLefflerE[a,b,t]) and so I evaluate this integral numerically using NIntegrate. I want to perform two tasks with this function---Plot it as a function of the limit $t$, and evaluate it's value at a number of different time instances. I have stored these values of time in a vector of the form {t1,t2,t3,...,tn}. Mathematica's documentation suggests that to plot the function I will have to define it as

f[t_?NumericQ] := NIntegrate[g[x],{x,0,t}]

which works perfectly fine. However, I cannot use this definition of the function $f$ to now evaluate it's value at every element of my list. I assume the issue is because NIntegrate is not listable. Is there any way I can get around this?

$\endgroup$
1
  • $\begingroup$ Attributes[f]={Listable} might want to look into using NDSolve though so you in a sense only have to evaluate the integral once $\endgroup$
    – ssch
    Dec 20, 2012 at 6:21

2 Answers 2

4
$\begingroup$

You can see examples here. Define:

f[t_] := NIntegrate[Sin[Cos[x]], {x, 0, t}]
SetAttributes[f, Listable];

Check:

f[{1, 2, 3}]

{0.738643, 0.803863, 0.11889}

But I am not sure why to plot such function you want to make it listable. You could just do something like:

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

or to speed up

DiscretePlot[f[t], {t, 0, 7, .1}, Filling -> 0]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks, this was what I was looking for. I wanted to make it listable because not only do I want to plot the function, I also want it's value at about a 100 discrete points in time. I have these time points generated through another piece of code and saved in a list. I required the function to be evaluated at every point in that list. $\endgroup$ Dec 20, 2012 at 19:19
1
$\begingroup$

I see no conflict between Listable and the argument test _?NumericQ. For example:

SetAttributes[f, Listable]

f[t_?NumericQ] := NIntegrate[Sqrt[x], {x, 0, t}]

f[{1, 2, 3, other}]
{0.666667, 1.88562, 3.4641, f[other]}

Perhaps you want a different result. I'll suppose that you would rather have f[{1, 2, 3, other}] returned unevaluated because one of the elements is not numeric. You could do that with:

f2[t_?NumericQ] := NIntegrate[Sqrt[x], {x, 0, t}]

f2[{t__?NumericQ}] := f /@ {t}

f2[{1, 2, 3}]

f2[{1, 2, 3, other}]
{0.666667, 1.88562, 3.4641}

f2[{1, 2, 3, other}]

This also illustrates a manual "listable" definition, though it only works on a simple list. Handling nested lists is possible but more complicated. What is your goal?

$\endgroup$
2
  • $\begingroup$ Thanks, setting the attribute as listable and removing the ?NumericQ argument test works fine. I'm new to Mathematica after having used MATLAB for many years (I now see the great utility in Mathematica!) so I apologize for my beginners questions! What exactly does the argument test do? $\endgroup$ Dec 20, 2012 at 19:21
  • $\begingroup$ @G.H.Hardly You shouldn't have to remove ?NumericQ when using Listable. The test, logically, checks to see if the argument is numeric. Because Listable is applied before DownValue rules this will not conflict with passing f a list of arguments, which I illustrate in my answer. $\endgroup$
    – Mr.Wizard
    Dec 23, 2012 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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