% Begin

function [x, w, A, B] = FiniteDifferenceGrid (N)

% The distance between nodes will be used multiple times. 
dx = 1/(N-1);

% a) Creating equally spaced vector 
x = linspace(0,1,N);

%b) Creating the weight array
w = dx * ones(1, N);
% Changing the first and last elements:
w(1) = dx/2;
w(end) = dx/2;

% c) Creating the matrix for the 1st derivative
A = zeros(N,N);
A(1:N+1:end) = -1;
A(N+1:N+1:end) = 1;
% Fixing the end node:
A(end, end) = 1;
A(end, end-1) = -1;
% Multiplying by 1/dx:
A = 1/dx * A;


% d) Creating the matrix for the 2nd derivative
B = zeros(N,N);
B(1:N+1:end) = -2;
B(2:N+1:end) = 1;
B(N+1:N+1:end) = 1;
% Fix the boundaries
B(1,1) = 1; 
B(1,2) = -2;
B(1,3) = 1;
B(end, end) = 1;
B(end, end-1) = -2;
B(end, end-2) = 1;
% Multiply with 1/dx^2
B = 1/dx^2 * B;
