Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
Attributes[f]={Listable} might want to look into using NDSolve though so you in a sense only have to evaluate the integral once –  ssch Dec 20 '12 at 6:21

2 Answers 2

up vote 3 down vote accepted

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

share|improve this answer
    
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. –  G. H. Hardly Dec 20 '12 at 19:19

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?

share|improve this answer
    
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? –  G. H. Hardly Dec 20 '12 at 19:21
    
@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. –  Mr.Wizard Dec 23 '12 at 11:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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