$u''(x)$ + $2u(x)^2$ $=$ $0$ for $0<x<1$
$u(0) = 1$, $u(1) = 1$
What would the matrices look like if I were to use Newtons method for system nonlinear equations?
Given:
$u'' = (u_{i-1}-2u_{i}+u_{i+1})/h^2$
I know the scheme will be $J(u^k)v = -f(u^k)$ where
$v = u^{k+1} - u^{k}$
I am writing a C++ program but can't get past understanding the math so if someone could write the matrices that would make things easier. I can see that the Jacobian will be tridiagonal, but I'm not sure how the math would work out.
Also, what is $-f(u^k)$ here? There are no functional forms given
In this problem, it's easier to make the transformation $u=v+1$ so that the boundary conditions will be homogeneous. This will simplify our objective function as the discretization of the solution near the boundary becomes a lot nicer. We discretize with $N-1$ interior unknown points and step size $h = 1/N$. Our objective function is then $$ F(\mathbf{v}) = D_2\mathbf{v}+2(\mathbf{v}+\mathbf{1})^{2}=\mathbf{0}, $$ where $D_2$ is a $N-1\times N-1$ matrix with main diagonal $-2/h^2$ and off diagonals $-1/h^2$. I am abusing notation a bit with the squaring, but note that this is done element-wise.
Now for the Jacobian. The Jacobian of a matrix-vector product is simply the matrix itself, so we have
$$ J(\mathbf{v}) = D_2 - 4 \cdot\mathrm{diag}(\mathbf{v}+\mathbf{1}), $$ where the "diag" denotes the matrix with the argument along the main diagonal.
This should be enough to get you started but be wary of sign differences between what I have written here and your source/code. Also, don't forget to add 1 from the final answer!