TransWikia.com

How can I change th code to implement the Fourier series as defined?

Computational Science Asked on April 9, 2021

I want to compute the Fourier series expansion of the function $$f:[-1,1] to mathbb{R}$$
with $$f(x) = begin{cases}
-x & x < 0
0 & x geq 0
end{cases} $$

.The code below is from Dr.Brunton "Data Science and Engineering" implementing Gibbs phenomenon but I want to change it to compute my series and I don’t know how.
Can someone help?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.cm import get_cmap
plt.rcParams['figure.figsize'] = [15, 8]
plt.rcParams.update({'font.size': 18})



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

n = len(x)

# partition space X into 4 parts 
nquart = int(np.floor(n/4))


# Define hat function
f = np.zeros_like(x)


f[nquart:2*nquart]   =   1 # -1 < x < 0
f[2*nquart:3*nquart] =   0 #  0 < x < 1  


fig, ax = plt.subplots()


A0 = np.sum(f * np.ones_like(x)) * dx
fFS = A0/2

m = 100
A = np.zeros(m)
B = np.zeros(m)
for k in range(m):
    A[k] = np.sum(f * np.cos(np.pi*(k+1)*x/L)) * dx # Inner product
    B[k] = np.sum(f * np.sin(np.pi*(k+1)*x/L)) * dx
    fFS = fFS + A[k]*np.cos((k+1)*np.pi*x/L) + B[k]*np.sin((k+1)*np.pi*x/L)
    ax.plot(x,fFS,'-')
```

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP