# -*- coding: utf-8 -*-
"""
Created on Fri Oct 14 15:51:35 2022

@author: Chris
Lab 11 Q1
"""

import numpy as np
import matplotlib.pyplot as plt
import scipy.linalg as la
import scipy.sparse as sp
import scipy.sparse.linalg as sla

xmin = 0
xmax = 4*np.pi

x = np.linspace(xmin,xmax,101)
h = x[1]-x[0]
N = len(x)

f = np.sin(x)

plt.plot(x,f)

D1 = sp.csc_matrix(
    sp.diags([-1/(2*h),0,1/(2*h)],[-1,0,1],shape=(N,N))
    )
D1[0,0] = -1/h; D1[0,1] = 1/h
D1[N-1,N-1] = 1/h; D1[N-1,N-2] = -1/h


dfdx = D1 @ f

plt.plot(x,dfdx)



D2 = sp.csc_matrix(
    1/h**2*sp.diags([1,-2,1],[-1,0,1],shape=(N,N))
    )
D2[0,0] = 1/h**2; D2[0,1] = -2/h**2 ; D2[0,2] = 1/h**2 
D2[N-1,N-1] = 1/h**2; D2[N-1,N-2] = -2/h**2 ; D2[N-1,N-3] = 1/h**2 


df2dx2 = D2 @ f
plt.plot(x,df2dx2)

plt.show()





