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

I have several functions, let's assume they are:


and a lot more like these.

For each and every one of these I want to do the following

xvalues = Range[0, 500, 2.5];
points1 = Map[func1, xvalues];
Do[If points1[[[i]] < 0, points1[[i]] = 0, points1[[i]] = points1[[i]], 
 {i, 1,  Length[points1]}]
table1 = Transpose[{xvalues, points1}];

Now seeing as I have a lot of these functiones, is there any way to automate this in some kind of routine?

While answering, please be aware that I don't really have any extensive Mathematica knowledge.

share|improve this question
There is some mismatch with braces in your code. Why points1[[i]] = points1[[i]]? Maybe you should write a higher order procedure with a function as an argument. – mmal Apr 29 '13 at 16:42
Yes, sorry, just noticed that! And points1[[i]] = points1[[i]] is there because it's actually points1[[i]] = Re[points1[[i]]] in my code, because some of my functions give out complex numbers. – freda42 Apr 30 '13 at 9:32
up vote 3 down vote accepted

How about:

apply[func_] := Module[{}, xvalues = Range[0, 500, 2.5];
    points1 = Map[func1, xvalues];
    Do[If[points1[[i]] < 0, points1[[i]] = 0], {i, 1, Length[points1], 1}];
    table1 = Transpose[{xvalues, points1}]];

Now you call the function apply with your desired funcX as an argument


Or you can automate this by defining

allFuncs = {func1, func2, func3, func4};

and then


will run them all.

share|improve this answer
But then how do I call the table created with the apply function? Now it seems like everytime I call the function, "table1" will be overwritten. – freda42 Apr 30 '13 at 7:41
Make table1 the output of the function. Remove the semicolon from the end of the line with table1=. – bill s Apr 30 '13 at 8:07
Ok, I tried it with the return value (and without) and I get an error saying"Do::itform: "Argument points1[[i]]=0 at position 2 does not have the correct form for an iterator" – freda42 Apr 30 '13 at 9:21
Copy from above. You had an extra right bracket in your Do loop. I fixed it. – bill s Apr 30 '13 at 9:47
Ah, thank you so much! I actually found out I missed two curly brackets around the whole if statement, because I also need that little bit with points1[[i]] = points1[[i]] which is points1[[i]] = Re[points1[[i]]] in my code! But I figured it out, thank you so much! – freda42 Apr 30 '13 at 9:54
f = {# &, 3*# - 5 &, 0.1*#^2 &};
xvalues = Range[0, 500, 2.5];
t1 = Through[f[xvalues]] /. x_ /; x < 0 -> 0;
ListPlot[t1, DataRange -> {0, 500}]

enter image description here

share|improve this answer
I would make one change: add DataRange -> {0, 500} to ListPlot, otherwise, +1. – rcollyer Apr 29 '13 at 17:00
@rcollyer I was doing that while you were writing your comment :) – Dr. belisarius Apr 29 '13 at 17:00
(:, what no love for doing it in one pass? I should revoke my +1. :D – rcollyer Apr 29 '13 at 17:02
But this only plots the values, doesn't make them accessible as a table, which is what I need to export them. I should have maybe added that in the original post, but I need a table for each function in the end. – freda42 Apr 30 '13 at 9:06
@freda42 t1holds all your values – Dr. belisarius Apr 30 '13 at 15:46

I would simplify your code a bit, merging everything into the Map statement, and move everything into a function, as follows:

process[func_, xvals_] :=
  points = Map[ With[{val = func@#}, UnitStep[val] val]&, xvals];
  Transpose[{xvals, points}]

and then for your functions, you can simply run

process[func1, Range[0, 500, 2.5]]

Or, if you prefer to bury your xvals inside your function, just do this, instead:

process[func_, xvals_:Range[0, 500, 2.5]] :=
  points = Map[ With[{val = func@#}, UnitStep[val] val]&, xvals];
  Transpose[{xvals, points}]
share|improve this answer

Your Answer


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.