Code covered by the BSD License

# Nonlinear Finite Difference Method

### Ernesto Momox Beristain (view profile)

The Finite Difference Method is employed for solving a nonlinear boundary value problem.

systemNxN(w,x,h,alpha,beta)
```function F = systemNxN(w,x,h,alpha,beta)
F = [2*w(1)-w(2)+(h^2)*f(x(1),w(1),(w(2)-alpha)/(2*h))-alpha;
-w(1)+2*w(2)-w(3)+(h^2)*f(x(2),w(2),(w(3)-w(1))/(2*h));
-w(2)+2*w(3)-w(4)+(h^2)*f(x(3),w(3),(w(4)-w(2))/(2*h));
-w(3)+2*w(4)-w(5)+(h^2)*f(x(4),w(4),(w(5)-w(3))/(2*h));
-w(4)+2*w(5)-w(6)+(h^2)*f(x(5),w(5),(w(6)-w(4))/(2*h));
-w(5)+2*w(6)-w(7)+(h^2)*f(x(6),w(6),(w(7)-w(5))/(2*h));
-w(6)+2*w(7)-w(8)+(h^2)*f(x(7),w(7),(w(8)-w(6))/(2*h));
-w(7)+2*w(8)-w(9)+(h^2)*f(x(8),w(8),(w(9)-w(7))/(2*h));
-w(8)+2*w(9)-w(10)+(h^2)*f(x(9),w(9),(w(10)-w(8))/(2*h));
-w(9)+2*w(10)-w(11)+(h^2)*f(x(10),w(10),(w(11)-w(9))/(2*h));
-w(10)+2*w(11)-w(12)+(h^2)*f(x(11),w(11),(w(12)-w(10))/(2*h));
-w(11)+2*w(12)-w(13)+(h^2)*f(x(12),w(12),(w(13)-w(11))/(2*h));
-w(12)+2*w(13)-w(14)+(h^2)*f(x(13),w(13),(w(14)-w(12))/(2*h));
-w(13)+2*w(14)-w(15)+(h^2)*f(x(14),w(14),(w(15)-w(13))/(2*h));
-w(14)+2*w(15)-w(16)+(h^2)*f(x(15),w(15),(w(16)-w(14))/(2*h));
-w(15)+2*w(16)-w(17)+(h^2)*f(x(16),w(16),(w(17)-w(15))/(2*h));
-w(16)+2*w(17)-w(18)+(h^2)*f(x(17),w(17),(w(18)-w(16))/(2*h));
-w(17)+2*w(18)-w(19)+(h^2)*f(x(18),w(18),(w(19)-w(17))/(2*h));
-w(18)+2*w(19)-w(20)+(h^2)*f(x(19),w(19),(w(20)-w(18))/(2*h));
-w(19)+2*w(20)-w(21)+(h^2)*f(x(20),w(20),(w(21)-w(19))/(2*h));
-w(20)+2*w(21)-w(22)+(h^2)*f(x(21),w(21),(w(22)-w(20))/(2*h));
-w(21)+2*w(22)-w(23)+(h^2)*f(x(22),w(22),(w(23)-w(21))/(2*h));
-w(22)+2*w(23)-w(24)+(h^2)*f(x(23),w(23),(w(24)-w(22))/(2*h));
-w(23)+2*w(24)-w(25)+(h^2)*f(x(24),w(24),(w(25)-w(23))/(2*h));
-w(24)+2*w(25)-w(26)+(h^2)*f(x(25),w(25),(w(26)-w(24))/(2*h));
-w(25)+2*w(26)-w(27)+(h^2)*f(x(26),w(26),(w(27)-w(25))/(2*h));
-w(26)+2*w(27)-w(28)+(h^2)*f(x(27),w(27),(w(28)-w(26))/(2*h));
-w(27)+2*w(28)-w(29)+(h^2)*f(x(28),w(28),(w(29)-w(27))/(2*h));
-w(28)+2*w(29)-w(30)+(h^2)*f(x(29),w(29),(w(30)-w(28))/(2*h));
-w(29)+2*w(30)-w(31)+(h^2)*f(x(30),w(30),(w(31)-w(29))/(2*h));
-w(30)+2*w(31)-w(32)+(h^2)*f(x(31),w(31),(w(32)-w(30))/(2*h));
-w(31)+2*w(32)-w(33)+(h^2)*f(x(32),w(32),(w(33)-w(31))/(2*h));
-w(32)+2*w(33)-w(34)+(h^2)*f(x(33),w(33),(w(34)-w(32))/(2*h));
-w(33)+2*w(34)-w(35)+(h^2)*f(x(34),w(34),(w(35)-w(33))/(2*h));
-w(34)+2*w(35)-w(36)+(h^2)*f(x(35),w(35),(w(36)-w(34))/(2*h));
-w(35)+2*w(36)-w(37)+(h^2)*f(x(36),w(36),(w(37)-w(35))/(2*h));
-w(36)+2*w(37)-w(38)+(h^2)*f(x(37),w(37),(w(38)-w(36))/(2*h));
-w(37)+2*w(38)-w(39)+(h^2)*f(x(38),w(38),(w(39)-w(37))/(2*h));
-w(38)+2*w(39)+(h^2)*f(x(39),w(39),(beta-w(38))/(2*h))-beta];
% -------------------------------------------------------------------------
% The above N x N nonlinear system of equations is genereted with the
% following lines of code, where N = number of mesh points. This system
% is then solved using the command 'fsolve'.
% N=39;
% k=1;
% for i=1:N
%     if i==1
%         fila1 = strcat('[2*w(',num2str(i),')','-w(',num2str(i+1),')','+',...
%            '(h^2)*f(x(',num2str(i),')',',','w(',num2str(i),')',',','(w(',num2str(i+1),')','-',...
%            'alpha',')','/(2*h)',')','-','alpha',';');
%     elseif i==N
%         filaN = strcat('-w(',num2str(i-1),')','+','2*w(',num2str(i),')','+','(h^2)*f(x(',num2str(i),')',...
%             ',','w(',num2str(i),')',',','(','beta','-','w(',num2str(i-1),')',')','/(2*h)',')','-','beta',']',';');
%     else
%         filas = strcat('-w(',num2str(i-1),')','+','2*w(',num2str(i),')','-w(',num2str(i+1),')','+',...
%            '(h^2)*f(x(',num2str(i),')',',','w(',num2str(i),')',',','(w(',num2str(i+1),')','-',...
%            'w(',num2str(i-1),')',')','/(2*h)',')',';');
%    quasiFormula(k,1:length(filas)) = filas; %#ok<AGROW>
%    k = k+1;
%     end
% end
% Formula = strvcat(fila1,quasiFormula,filaN); %#ok<VCAT>
% disp(Formula) ```