How to Use a Function in a Loop in Mathematica?

Click For Summary
SUMMARY

This discussion focuses on using functions within loops in Mathematica, specifically for Hermite's polynomial calculations. The user demonstrates how to define a list of functions and iterate over them using the Sum function. Key examples include defining functions af1 and af2 for cosine and sine, and utilizing them in a loop to compute sums involving trigonometric functions. The conclusion emphasizes the necessity of organizing functions into lists for effective iteration in Mathematica.

PREREQUISITES
  • Familiarity with Mathematica syntax and functions
  • Understanding of Hermite's polynomial and its applications
  • Knowledge of trigonometric functions in Mathematica
  • Basic concepts of iteration and summation in programming
NEXT STEPS
  • Explore advanced function manipulation in Mathematica
  • Learn about the use of the Table function for generating lists of functions
  • Investigate the implementation of higher-order functions in Mathematica
  • Study the performance implications of using loops versus vectorized operations in Mathematica
USEFUL FOR

Mathematica users, mathematicians, and anyone interested in computational methods for polynomial calculations and function iteration.

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: 543
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!
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K