I am trying to use Matlab to find Numerical Solution to $u''(x)+e^{u(x)}=0, u(0)=0, u(1)=0$. I can't get my code to work out. I was hoping you could help me. I have added citations to my code to explain what is going on. My confusion is that I am getting a lot of different "solution" graphs output. They change each time I run it. This doesn't seem right to me. Also, the boundary conditions are not portrayed on the graphed "solution".
For $G=\frac{1}{h^2}(u_{j-1}-2u_j+u_{j+1})+N(u_j)$ where $N(u_j)$ is the $e^{u(x)}$ nonlinear part, and for J being the Jacobian, I am trying to follow this procedure: Solve for delta, $$J(U^{(k)})\delta = -G(U^{(k)})$$ Then set $$U^{(k+1)}=U^{(k)}+\delta$$ Test for accuracy and repeat if necessary epsilon isn't reached. Thank you for any help you can give.
Note: I think my initial guess should be this due to boundary conditions. Uk = [0 10*randn(1, m2-2) 0]' But this doesn't converge.
This Uk leads to convergence at least... Uk = rand(m2,1);
ax = 0;
bx = 1;
sigma = 0; % Dirichlet boundary condition at ax
beta = 0; % Dirichlet boundary condtion at bx
lambda = 1;
f = @(x) 0*x; % right hand side function
epsilon = 0.01;
count = 1;
m1vals = 40;
m2= m1vals+1;
m1 = m1vals;
m = m1 - 1; % number of interior grid points
%Uk = [0 10*randn(1, m2-2) 0]'
%This Uk leads to convergence at least.
Uk = rand(m2,1);
Uk1=Uk;
h=0.01;
N = @(u) lambda*exp(u);
% set grid points:
gridchoice = 'uniform';
x = xgrid(ax,bx,m,gridchoice);
while norm(Uk1-Uk) > epsilon || count ==1
norm(Uk1-Uk) %check norm and print
if count ~= 1 %only after first iteration
Uk=Uk1;
end
%Part of J with -2 on main and 1 on off digaonals
Jpart = (1/h^2)*(diag(-2*ones(m2,1),0)+diag(ones(m2-1,1),1)+diag(ones(m2-1,1),-1));
%Create N' part of Jacobian
%addJ is an estimate of u'(x) since we need u'(x)*lambda*exp(u(x))
addJ = spalloc(m2,m2,3*m2);
addJ(1,1:3) = fdcoeffF(1, x(1), x(1:3));
for i=2:m1
addJ(i,i-1:i+1) = fdcoeffF(1, x(i), x((i-1):(i+1)));
end
addJ(m2,m:m2) = fdcoeffF(1,x(m2),x(m:m2));
%computes u'(x)*lambda*exp(u(x)) at Uk.
J2 = diag(addJ*Uk,0)*N(Uk); %addJ*Uk is an estimate of u'(x) at Uk points.
J=Jpart+diag(J2,0); %put all together
% set up matrix G = u''+N
Gpart = spalloc(m2,m2,3*m2); % initialize to zero matrix
% first row for Dirichlet BC at ax:
Gpart(1,1:3) = fdcoeffF(0, x(1), x(1:3));
% interior rows:
for i=2:m1
Gpart(i,i-1:i+1) = fdcoeffF(2, x(i), x((i-1):(i+1)));
end
% last row for Dirichlet BC at bx:
Gpart(m2,m:m2) = fdcoeffF(0,x(m2),x(m:m2));
%Set up for J(Uk)delta=-G(Uk)
Gall=Gpart+diag(N(Uk),0);
Gans=-Gall*Uk; %evaluate G(Uk)
Delta = J\Gans;
Uk1 = Uk + Delta;
count = count + 1;
end
clf
plot(x,Uk1,'o') % plot computed solution
title(sprintf('Computed solution with %i grid points',m2));

I had trouble following your code, but it seemed that you were taking $$\frac d{dx}e^{U_k}=e^{U_k}\frac{dU_k}{dx}$$ Which is wrongheaded because you really need $$\frac d{dU_j}e^{U_i}=e^{U_i}\delta_{ij}$$ Writing a simpleminded code with that change I was able to reproduce Yuriy S's analytical results.