# Map several functions in one routine

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

func1[x_]=x;
func2[x_]=3*x-5;
func3[x_]=0.1*x^2;


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.

-
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

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

apply[func1]


Or you can automate this by defining

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


and then

apply/@allFuncs


will run them all.

-
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}]


-
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_] :=
Block[{points},
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]] :=
Block[{points},
points = Map[ With[{val = func@#}, UnitStep[val] val]&, xvals];
Transpose[{xvals, points}]
]

-