I have to solve the following equation:
$-u_{xx}=1$,
with $x\in(0,1)$ and $u(0)=0,u(1)=0$. I have to solve it with the following numerical scheme:
$\frac{1}{h_k^2}(-\frac{1}{2}u_{k-1}+u_k-\frac{1}{2}u_{k+1})=1$. So I have to use a non-uniform grid. I have done this so far:
n = 4; %Number points
k = 0:n;
x = 0.5 - 0.5*cos(pi*k/n); %Function to generate points
h = diff(x);
h = h(1:n-1);
h = (h.^2)'; %Difference from point 1 to n-1
b = ones(n-1,1);
b = h.*b; %Solve
A=sparse(diag(2*ones(n-1,1))+diag((-1)*ones(n-2,1),1)+diag((-1)*ones(n- 2,1),-1));
u=A\b;
As you can see I was trying to define everything I need to use a for loop and also trying to do that using just matrix multiplications. The problem is that with for loop I am not able to understand what should be the value at $u(1)$ since I only have $u(0)$ and the method requires three points. With the "matrix version" I am not able to understand how to compute the vector containing the differences between the pints.