Message Boards Message Boards

0
|
7052 Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Store variable in each step?

Posted 7 years ago

Hello everyone. I am quite new to Mathematica and I think this is a very basic question but how can I store data from a function in each step. Suppose that I have a function which is called F and is defined as:

F[X_] := 1/2 (Cos[\[Omega]1 X] + 1);

Omega is an arbitrary number.

Now I would like to give data to F, for example X = {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10} , and save them in a variable like G.

Thanks a lot.

POSTED BY: Mohsen Rezaee
4 Replies
Posted 7 years ago

The most common method is using Table. Valeriu used Map (/@) above, which also works. It is best not to begin user variables with upper case to prevent conflict with reserved names.

In[8]:= f[x_] := 1/2 (Cos[\[CapitalOmega] x] + 1);

(* using an iteration *)
g = Table[f[x], {x, 0, 10}]

Out[9]= {1, 1/2 (1 + Cos[\[CapitalOmega]]), 
 1/2 (1 + Cos[2 \[CapitalOmega]]), 1/2 (1 + Cos[3 \[CapitalOmega]]), 
 1/2 (1 + Cos[4 \[CapitalOmega]]), 1/2 (1 + Cos[5 \[CapitalOmega]]), 
 1/2 (1 + Cos[6 \[CapitalOmega]]), 1/2 (1 + Cos[7 \[CapitalOmega]]), 
 1/2 (1 + Cos[8 \[CapitalOmega]]), 1/2 (1 + Cos[9 \[CapitalOmega]]), 
 1/2 (1 + Cos[10 \[CapitalOmega]])}

(* using an explicit list *)
g = Table[f[x], {x, {0, 1, 2, 3, 4, 5}}]

Out[10]= {1, 1/2 (1 + Cos[\[CapitalOmega]]), 
 1/2 (1 + Cos[2 \[CapitalOmega]]), 1/2 (1 + Cos[3 \[CapitalOmega]]), 
 1/2 (1 + Cos[4 \[CapitalOmega]]), 1/2 (1 + Cos[5 \[CapitalOmega]])}
POSTED BY: David Keith
Posted 7 years ago

Exactly what I was looking for. Many thanks.

POSTED BY: Mohsen Rezaee
Posted 7 years ago

Many Mathematica functions habe the attribute listable, i.e. the also work with lists as arguments

g = 1/2 ( Cos[\[CapitalOmega] Range[0, 10]])

You can replace [[CapitalOmega] by a numerical argument using

g /. \[CapitalOmega] -> 3
POSTED BY: Michael Helmle

One of possible approaches is the following:

In[1]:= Omega = 3;
F[X_] := 1/2 (Cos[Omega X] + 1);
F /@ Range[0, 10]

Out[2]= {1, 1/2 (1 + Cos[3]), 1/2 (1 + Cos[6]), 1/2 (1 + Cos[9]), 
 1/2 (1 + Cos[12]), 1/2 (1 + Cos[15]), 1/2 (1 + Cos[18]), 
 1/2 (1 + Cos[21]), 1/2 (1 + Cos[24]), 1/2 (1 + Cos[27]), 
 1/2 (1 + Cos[30])}
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract