2
$\begingroup$

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.

$\endgroup$
  • 4
    $\begingroup$ 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. $\endgroup$ – mmal Apr 29 '13 at 16:42
  • $\begingroup$ 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. $\endgroup$ – freda42 Apr 30 '13 at 9:32
3
$\begingroup$

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

apply[func1] 

Or you can automate this by defining

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

and then

apply/@allFuncs

will run them all.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – freda42 Apr 30 '13 at 7:41
  • $\begingroup$ Make table1 the output of the function. Remove the semicolon from the end of the line with table1=. $\endgroup$ – bill s Apr 30 '13 at 8:07
  • $\begingroup$ 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" $\endgroup$ – freda42 Apr 30 '13 at 9:21
  • $\begingroup$ Copy from above. You had an extra right bracket in your Do loop. I fixed it. $\endgroup$ – bill s Apr 30 '13 at 9:47
  • $\begingroup$ 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! $\endgroup$ – freda42 Apr 30 '13 at 9:54
5
$\begingroup$
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

$\endgroup$
  • $\begingroup$ I would make one change: add DataRange -> {0, 500} to ListPlot, otherwise, +1. $\endgroup$ – rcollyer Apr 29 '13 at 17:00
  • $\begingroup$ @rcollyer I was doing that while you were writing your comment :) $\endgroup$ – Dr. belisarius Apr 29 '13 at 17:00
  • $\begingroup$ (:, what no love for doing it in one pass? I should revoke my +1. :D $\endgroup$ – rcollyer Apr 29 '13 at 17:02
  • $\begingroup$ 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. $\endgroup$ – freda42 Apr 30 '13 at 9:06
  • $\begingroup$ @freda42 t1holds all your values $\endgroup$ – Dr. belisarius Apr 30 '13 at 15:46
2
$\begingroup$

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}]
 ]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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