import numpy as np, matplotlib.pyplot as plt
import time

#####################################
N=100   #Lengde på fourierrekke
POINTS = 1000   #antall punkter som plottes
######################################

def heaviside_rekke(x,N):
    val = 0.5
    for n in range(N):
        val += (2/np.pi)*(1/(2*n+1))*np.sin(((2*n)+1)*x)

    return val

def rarfunc(x):
    if x<0:
        return 0
    return x*(np.pi - x)

def fourier_rarfunc(x,N):
    val = (np.pi**2)/12
    for n in range(1,N):

        val += (-1/(2*(n**2)))*np.cos(2*n*x) + (4/(np.pi*((2*n-1)**3)))*np.sin((2*n-1)*x)
    return val

def oppg2():
    x_line = np.linspace(-np.pi,np.pi,POINTS)

    heaviside_x = [-np.pi,0,0,np.pi]
    heaviside_y = [0,0,1,1]

    heaviside_fourier_y = [heaviside_rekke(x,N) for x in x_line]

    plt.plot(x_line,heaviside_fourier_y)
    plt.plot(heaviside_x,heaviside_y)
    plt.show()

def oppg3():
    x_line = np.linspace(-np.pi,np.pi,1000)
    rar_y = [rarfunc(x) for x in x_line]

    fourier_rary = [fourier_rarfunc(x,N) for x in x_line]

    plt.plot(x_line,rar_y)
    plt.plot(x_line,fourier_rary)
    plt.show()

