How to Use a Function in a Loop in Mathematica?

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 4K views
ggeo1
Messages
61
Reaction score
0
Hello, i have this formula to iterate(attached).
It is hermite's polynomial .

I wanted to ask if in order to access Ai(x) , where A is another function ,

can i do this ?

Code:
...
suma = 0; sumb = 0;
For[i = 1, i <= n, i++,
 suma +=[B][SIZE="4"] a[x][[i]][/B]*Cos[Pi*x] ;    (a is A)
 sumb += b[x][[i]]*D[Cos[Pi*x], x];
...
 

Attachments

  • image.png
    image.png
    1.3 KB · Views: 553
Physics news on Phys.org
If A really is a list of functions, perhaps something like this might work:

In[8]:=af1[x_]:=Cos[x];af2[x_]:=2*Sin[x];a={af1,af2};a[[1]][x]+a[[2]][x]
Out[8]=Cos[x]+2 Sin[x]

Then this might work:
In[4]:=n=2;
af1[x_]:=Cos[x];af2[x_]:=2*Sin[x];a={af1,af2};
bf1[x_]:=Tan[x];bf2[x_]:=Exp[x];b={bf1,bf2};
suma=Sum[a[][x]*Cos[Pi*x],{i,1,n}];
sumb=Sum[b[][x]*D[Cos[Pi*x],x],{i,1,n}];
 
Last edited:
Ok ,thanks ,i will check it.

It must be right.I didn't know how to put a function into a loop.But ,as you did,you must make a list of the function in order to do that.

Thanks again!