# -*- coding: utf-8 -*-
"""
Created on Fri Oct 14 16:13:08 2022

@author: 102194
"""

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 = 2

x = np.linspace(xmin,xmax,101)
h = x[1]-x[0]
N = len(x)

D1 = sp.diags([-1/(2*h),0,1/(2*h)],[-1,0,1],shape=(N,N))
D2 = 1/h**2*sp.diags([1,-2,1],[-1,0,1],shape=(N,N))

I = sp.eye(N)

#create matrix:
A = sp.csc_matrix(-D2 + 4*D1 + I)

#create RHS:
f = sp.csc_matrix(x*np.exp(-x)).T

# put in boundary condition
A[0,0] = 1; A[0,1]=0
A[-1,-1]=1/h; A[-1,-2] = -1/h
f[0] = 0
f[-1] = -1

u = sla.spsolve(A,f)

plt.plot(x,u)










