Why will my multi-dimensional Newton Method not converge?

Hi I am trying to solve this problem:
Below is the code that I wrote to try and solve this problem. For some reason when I use a node size greater than 5 my code will overshoot and never converge. I am not sure what is wrong. Any suggestions would be helpful thank you.
clear
clc
Nx = 29; %number of nodes in x
Ny =Nx; %number of nodes 1in y
h = 1/(Nx);
intervalx = [0,1];
intervaly = [0,1];
N = Nx+1;
BC = 0;
la = 19.6;
b(N^2,1) = 0;
A = 1;
for i = 1:N^2
condition(i,1) = 1;
du(i,1) = 0;
end
dx = (intervalx(2)-intervalx(1))/(N-1);
deltax= intervalx(1)+(0:1:(N-1))*dx;
dy = (intervaly(2)-intervaly(1))/(N-1);
deltay= intervaly(1)+(0:1:(N-1))*dy;
%Diagnoals of matrix A
beta(1:N^2-1)=1/h^2;
lambda(1:N^2-N) = 1/h^2;
%Create the discretized Matrix
J = zeros(N^2,N^2);
J = eye(N^2,N^2);
J = J + diag(beta,-1);
J = J + diag(beta,1);
J = J + diag(lambda,-N);
J = J + diag(lambda,N);
m = linspace(0,1,N);
w = linspace(0,1,N);
m = m';
w = w';
for i = 1:N
for g = 1:N
c(g,i) = A*sin(pi*w(g))*sin(pi*m(i));
end
end
for i=1:N
for j=1:N
u((i-1)*N + j) = c(i,j);
end
end
u = u'
for i = N+2:N^2-N
R(i,1) = 1;
J(i,i) = 1;
end
for h = 1:N+1
J(h,h) = 1;
condition(h,1) = 0;
end
for g = 2:N-1
J(g*N,g*N) = 1;
J((g*N)+1,(g*N)+1) = 1;
R(g*N,1) = 0;
R((g*N)+1,1) = 0;
condition(g*N,1) = 0;
condition((g*N)+1,1) = 0;
end
for q = (N-1)*N:N^2
R(q,1) = 0;
J(q,q)=1;
condition(q,1) = 0;
end
%Upadate and use the while loop below to solve for u
sigs = 7; %# of sig figs requested of solution
iterations = 1000;
itermax = iterations - 1; %# of iteration
es=0.5*10^(2-sigs);
k=0; %counter
t = 1; %step size
while (1)
duold =du;
uold=u;
for i = 1:N^2
if condition(i,1) == 1
R(i,1) = (u(i+1)-2*u(i)+u(i-1))/h^2 + (u(i+N)-2*u(i)+u(i-N))/h^2 +la*u(i)*(1+u(i));
J(i,i) = -2/h^2-2/h^2+la+2*la*u(i);
else
R(i,1) = 0;
J(i,:) = 0;
J(i,i) = 1;
end
end
du = J\(-R);
norm (du);
u = u + (1/t)*(du);
k=k+1;
if k > itermax
fprintf('maximum number of iterations reached: no convergence. \n');
break
end
if norm(du) < es
break
end
end

 Accepted Answer

Torsten
Torsten on 2 Mar 2023
Edited: Torsten on 2 Mar 2023
Your Jacobian is wrong. You didn't specify J(i,i-1),J(i,i+1),J(i,i-N),J(i,i+N). Only computing the diagonal is not sufficient for convergence.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!