3
$\begingroup$

I have a list in Mathematica for example

P={1+t,1+t+t^2,1+t+t^3};

My question is, how I can define each element of this list be a function of t? Indeed I want something like this input

P[[1]][.2]

which its output is

1.2

Thanks.

$\endgroup$
  • 2
    $\begingroup$ you can do something like p[[1]] /. t -> .2 while this is not a function, it will give the same out you are looking for. $\endgroup$ – Nasser Feb 5 '15 at 22:47
  • 1
    $\begingroup$ @Nasser Thanks a lot. Put your comment as answer. $\endgroup$ – Qaher Feb 5 '15 at 22:51
7
$\begingroup$

There are few ways to do this. The simplest might be

   p = {1 + t, 1 + t + t^2, 1 + t + t^3};
   p[[n]] /. t -> .2

where n is the index. Or you can convert the list to actual functions like this

p = Function[t, #] & /@ p

Mathematica graphics

And now you can write p[[n]][.2] as you wanted.

Now for example, you can evaluate all these function for at some value using

  p[[#]][.1] & /@ Range[Length[p]]

Mathematica graphics

I am sure there are other ways to do this.

$\endgroup$
3
$\begingroup$

Try this:

p[m_, t_] := Table[Sum[t^i, {i, 0, n}], {n, 1, m}];

Then the list of three elements you gave in your question is obtained as follows:

    p[3, t]

(*   {1 + t, 1 + t + t^2, 1 + t + t^2 + t^3}   *)

and the first term of the list is

p[3, 0.2][[1]]

(*  1.2   *)

Have fun!

$\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.