Mathematica: array of functions

In summary, the conversation is about a person asking for help with creating a new list from two existing lists, where the elements of the new list are obtained by applying functions from the first list to values from the second list. Two different methods are suggested, one using the Transpose and Function commands, and the other defining a function and using the Part command. The use of [[]] and [[ ]] operators is also explained.
  • #1
Anna Kaladze
35
0
Hi intelligent people,

Forgive me for a silly question, but I am really new to Mathematica and thus I need your help for my own research (I am done constructing NIntegrate values for some "non-integrable" functions, involving multiple steps, and I am kinda stuck at the last step. Here is the example which will help me if I know how to crack it).
OK, Here are the series of simple inputs-outputs in the command window:

f:={1+t^2,1/t,5-t}

f

{1+t2,1/t,5-t}

j=Table[x,{x,0,2,1}]

{0,1,2}

All I need to do, is to create a new list, where the first function in f list (i.e. 1+t^2) would take as an argument the first value in j vector (0), the 2nd function in f (i.e. 1/t) would take as an argument the 2nd element in j vector (2), and so on and then display the resulting values in an array. I tried to use some Table commands, and basic looping, could not go far ... The answer is apparently this:

{0,1,3}

But the question is how to get to there...

Many thanks,

Anna.
 
Physics news on Phys.org
  • #2
I suppose the quickest way to do it is construct a list of expressions and values first:
Code:
xy = Transpose[{j, f[t]}]
gives {{0, 1 + t^2}, {1, 1/t}, {2, 5 - t}}

Then you can replace the t's in the second list by the values in the first list, using an anonymous function with two arguments:
Code:
Function[{x, y}, y /. t -> x] @@@ xy
Note that you need the @@@ to properly pass the arguments (you can see what happens if you do
Code:
g @@@ xy
But I was wondering, if f is a function, why don't you define it as such?
Code:
f[t_]:={1+t^2,1/t,5-t}
J = Range[0, 2, 1];
Then you can also do something like
Code:
Table[f[j[[i]]][[i]], {i, 1, Length[j]}]
 
  • #3
Dera CompuChip,
Thanks a lot for your answers, they are really great!
Can you please help me to find out where I can read about "[[]]" command/operators to understand your 2nd method better? Wolfram online gives basic info about tables, etc. only. I am a little confused what "f[j[]][]" does, and more so about the very last [] part. Your method works fine, just would like to understand it better (the 1st one made perfect sense).
Thanks again for your time.
Anna.
 
  • #5
list[] takes the i'th element from a list, for example, if
Code:
list = {2, 4, 6, 8}
then list[[1]] = 2, list[[2]] = 4, list[[3]] = 6 and list[[4]] = 8; list[[5]] will give an error.

So basically what I am doing is taking the i'th x-value from j and applying f to that.
Code:
f[j[[i]]]
Now f produces a list again. Since we are interested in the i'th (for the same value of i) output, we need to use the [[ ]] operator again. So
Code:
f[j[[i]]][[i]]

I hate defining variables when it is not necessary, but just for educational purposes, you could write it like this:
Code:
i = 3; (* for example, let's look at the third element *)
x = j[[i]]; (* or x = Part[j, i] or x = Take[j, {i}] *)
yArray = f[x];
y = yArray[[i]]; (* this is the value you want *)
 

1. What is an array of functions in Mathematica?

An array of functions in Mathematica is a collection of functions that are stored in a single data structure. This allows for easy manipulation and evaluation of multiple functions at once.

2. How do I create an array of functions in Mathematica?

To create an array of functions in Mathematica, you can use the Function command followed by a list of functions. For example, array = {Function[x, x^2], Function[x, Sin[x]]} creates an array with two functions: x^2 and Sin[x].

3. How do I access and manipulate elements in an array of functions in Mathematica?

You can access and manipulate elements in an array of functions using standard indexing and manipulation techniques. For example, array[[1]] will retrieve the first function in the array, and array[[2]][3] will evaluate the second function in the array at x=3.

4. Can I combine multiple arrays of functions in Mathematica?

Yes, you can combine multiple arrays of functions using the Join command. For example, combinedArray = Join[array1, array2] will combine two arrays of functions into one.

5. How can I plot an array of functions in Mathematica?

To plot an array of functions in Mathematica, you can use the Plot command and specify the array as the function to be plotted. For example, Plot[array, {x, 0, 10}] will plot all the functions in the array over the range x=0 to x=10.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
221
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
261
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
412
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
934
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top