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 would like to create two arrays of functions yfunc[[i]] and tfunc[[i]], with each entry of those functions being a function depending on t, that is to say

yfunc={yfunc[[1]][t],yfunc[[2]][t],...} 

and the same for tfunc. Then I would also like to plot members of these array with ParametricPlot in pairs, e.g.

ParametricPlot[{yfunc[[i]][t],tfunc[[i]][t]},{t,0,1}]

This code works:

yfunc[t_?NumericQ, i_?IntegerQ] := i*t;
tfunc[t_?NumericQ, i_?IntegerQ] := 2 i*t;
comp[i_] := {tfunc[t, i], yfunc[t, i]};
Show[ParametricPlot[comp[1], {t, 0, 1}], AspectRatio -> 1]

But another try of creating this function array fails:

yfunc[t_?NumericQ] := Table[i*t, {i, 1, 100}]
tfunc[t_?NumericQ] := Table[2 i*t, {i, 1, 100}];
comp[i_?IntegerQ] := {tfunc[[i]][t], yfunc[[i]][t]};
Show[ParametricPlot[comp[1], {t, 0, 1}], AspectRatio -> 1]

I guess in this case it is a problem with the evaluation. What would be the correct way to do it in the second way?

Thanks!

share|improve this question
1  
Your patterns do not match; first argument is a real, second is an integer. –  b.gatessucks Jan 24 '13 at 9:46
    
Yes this works fine. Thanks! This resolved the first case. I mixed up t and i. Edited the post to correct this. –  malumno Jan 24 '13 at 10:00
1  
in the first example, you probably meant comp[i_] := {tfunc[t, i], yfunc[t, i]} (which works fine). Similarly for the second example: comp[i_] := {tfunc[t][[i]], yfunc[t][[i]]} works. –  kguler Jan 24 '13 at 10:04
    
The problem with using comp[i_] := {tfunc[t][[i]], yfunc[t][[i]]} is that first every function in the vector gets evaluated only to choose one value of interest in the end. This is of course no problem in the toy model i*t, but takes more time with longer arrays and more complicated functions. –  malumno Jan 24 '13 at 10:13
    
malumno, even with the toy example, it is quite slow. –  kguler Jan 24 '13 at 11:02
add comment

1 Answer

up vote 3 down vote accepted

The problem with not using tfunc[t][[i]] and trying as you do to call tfunc[[i]][t] is that tfunc is not a list and cannot be indexed. The result of calling tfunc[t] is a list, which can be indexed. If you want to use a list of functions, you can use anonymous functions:

yfunc = Table[With[{i = i}, Function[t, i*t]], {i, 1, 100}];
tfunc = Table[With[{i = i}, Function[t, 2 i*t]], {i, 1, 100}];

comp[i_?IntegerQ, t_] := {tfunc[[i]][t], yfunc[[i]][t]};
Show[ParametricPlot[comp[1, t], {t, 0, 1}], AspectRatio -> 1]

Note that since Function has attribute HoldAll, With[{i=i},...] is used to inject the value of i into the expression. If you don't do this it wont work as: Table[Function[t,i],{i,2}] evalutes to {Function[t,i],Function[t,i]}.

share|improve this answer
add comment

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.