# for loop for a fourier series equation

• Last Update :
• Techknowledgy :

This is all you need:

```import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
pi = np.pi
n = 10
x = np.linspace(-pi, pi, 100)
y = (1 / (2 * pi)) + (1 / pi) * (sum(np.cos(i * x) for i in xrange(1, n)))

plt.plot(x, y)
plt.show()```

You can use a list comprehension:

```n = 10
y = ao + (1 / pi) * sum([np.cos((i + 1) * x) for i in range(n)])```

Suggestion : 2

Hello everyone, I'd like to get the coefficients of the Fourier series with a for loop. I tried to do it in the following way:,Fourier series coefficients with for loop,Note that the data is not stored in the For loop above, which is not at all a smart way to compute data, but I've seen it done often. Below is the typical way one would compute data. For the for-loop way, you first declare/allocate an array, and then fill it with values. With Table[], it's all handled for you and very efficiently.,You can program WL sequentially (using DO and FOR) but you lose much of the power of the language and operations are slower because EL is optimized for lists. It is best to replace FOR with MAP or its related functions. You will also find that the programs become simpler if you avoid looping constructs and move to list-based constructs.

Hello everyone, I'd like to get the coefficients of the Fourier series with a for loop. I tried to do it in the following way:

`For[i = -10, i < 11, i++, FourierCoefficient[Sin[t], t, i]]`

Iterate a command `f[]` (here `f` is a no-op, so the timing reflects the overheads of `For` and `Do`):

```ClearAll[f, i];
For[i = 1, i <= 10 ^ 6, i++, f[i]]; // AbsoluteTiming  (*  i  is a global variable *)
i

Clear[i]
Do[f[i], {
i,
10 ^ 6
}]; // AbsoluteTiming  (*  i  is a local variable *)
i
( * Out[] = {
1.01164,
Null
}
1000001

{
0.226265,
Null
}
i *
)```

Note that `i` is not localized in `For`. The user has to localize `i` explicitly to get an equivalent behavior of `Do`:

```Block[{
i
}, For[i = 1, i <= 10 ^ 6, i++, f[i]]]( * user - localized i * )```

Compute some data:

```For[i = 1, i <= 10, i++, Print[Sin[0.1 i]]];

data = Table[Sin[0.1 i], {
i,
10
}];
data // TableForm```

Suggestion : 3

Version 8.4.1 ,You have successfully joined our subscriber list.

```\
SORRY / \/\
] not exist yet.[, '|][/  |] ___ ___[, '   |]]\ / [
[ |: | ]
]\ / [
[ |: | ]
]][
[
[ |: | ]
]
] __ __[[
[ |: | ]
]]]\ _ / [
[
[
[ |: | ]
]
]
](#)[[
[
[: === = ']
]
] _].nHn.[_[[
[]
]] HHHHH.[
[
[]
] / `HH("N  \ [  [
]__]/     HHH  "  \[__[
]         NNN         [
]         N/"         [
]         N H         [
/          N            \
/           q,            \
/                           \```

Suggestion : 4

It leads to successive revolution in the advancement of analytical and computational mathematics, data analysis, and numerical physics at an engineering scale.,The general concern when it comes to mathematical physics and engineering mathematics is to transform equations into a coordinate system where the expressions analyze, unravel, and are tractable to computation and analysis. The infrastructure to solve this problem of coordinate transformation was introduced by J.B Joseph Fourier in the early 18th century to investigate the theory of heat where he put forward the concept of the Fourier Series.,The concepts of Hilbert spaces and operator theory are given mathematical foundation with the introduction of the Fourier Series concept.,You will come across the terms Fourier Series and Fourier Transform frequently. The difference between them is that the Fourier series is an expansion of periodic signal as a linear combination of sines and cosines, while Fourier transform is the process or function used to convert signals from the time domain to frequency domain.

Python code for generating a square wave:

```import numpy as np

import matplotlib.pyplot as plt

from scipy.signal
import square

from scipy.integrate

from math
import * //import all function from math

x = np.arange(-np.pi, np.pi, 0.001) //x axis has been chosen from –π to +π, value
//of 1 smallest square along x axis is 0.001

y = square(x) //defining square wave function 𝑦 =−1, 𝑓𝑜𝑟 − 𝜋 ≤ 𝑥 ≤ 0
//y= +1, 𝑓𝑜𝑟 0 ≤ 𝑥 ≤ 𝜋

//define fuction

fc = lambda x: square(x) * cos(i * x) //i :dummy index

fs = lambda x: square(x) * sin(i * x)

n = 50 //max value of I, not taken infinity, better result with high value

An = [] // defining array

Bn = []

sum = 0

for i in range(n):

an = quad(fc, -np.pi, np.pi)[0] * (1.0 / np.pi)

An.append(an)

for i in range(n):

bn = quad(fs, -np.pi, np.pi)[0] * (1.0 / np.pi)

Bn.append(bn) //putting value in array Bn

for i in range(n):

if i == 0.0:

sum = sum + An[i] / 2

else:

sum = sum + (An[i] * np.cos(i * x) + Bn[i] * np.sin(i * x))

plt.plot(x, sum, 'g')

plt.plot(x, y, 'r--')

plt.title("fourier series for square wave")

plt.show()```

for L=0.5, Period=1

```import matplotlib.pyplot as plt

from scipy.signal
import square

L = 1

x = np.arange(-L, L, 0.001)

y = square(2 * np.pi * x)

plt.plot(x, y, 'r--')

plt.title("fourier series for square wave ")

plt.show()```