# Solving nonlinear differential equations with Newton's method

I have difficulties with this equation

$$\frac{d^2 u}{d x^2} + u^2 - x^2 = 0$$

with boundary conditions: $u(0)=u(1)=0$

I do not know how to solve nonlinear differential equations with Newton's method. If somebody knows could you please explain?

I followed the comments and ı finally reach these two equations (eqn1 and eqn2). But my problem is that from now on , ı do not have any idea to combine these two equations. İf somebody has any idea, ı will be thankful so much for sharing with me. I just applied the newton raphson method

• Welcome! I edited your question a bit (improved its format, hope it still reads as you intended). What sort of difficulty did you encounter with Newton's method? Where do you get stuck? Oct 27, 2015 at 15:43
• My problem is that I do not know how to apply Newtons method to nonlinear equations ? Oct 28, 2015 at 12:16
• The first step is to read a bit about the fundamentals of Newton's method, then try it out on a very simple equation. Here is a reference to get you started; a Google search will yield lots more material. Oct 28, 2015 at 13:07
• Thank you so much for your help. ı read your reference. ı finally reached the two equations (eqn1 and 2) but it is all I have nothing more to solve it. Oct 31, 2015 at 20:34
• If the $u^2$ term was not there, could you solve the problem? In other words, are you comfortable with solving the linear problem? (this is prerequisite to solving the nonlinear problem via Newton's method) Nov 1, 2015 at 0:52

@GoHokies already had a good link to how to think about nonlinear problems. My own contribution is in lectures 31.5 and following here: http://www.math.tamu.edu/~bangerth/videos.html

I understood that you are trying to solve a nonlinear ODE with two Dirichlet conditions. The Newton method is applied to find the root numerically in an iterative manner. In this case, I would try a numerical method to solve this ODE. You could do this using Finite Element Method.

As this problem is nonlinear, you would need to apply the Newton's method. To apply the Newton Method's, you would need to do a Gateaux's differentiation.

After the Gateaux differentiation, you can then apply the Newton's method. The FEM method to discretize is not mandatory; there are other numerical methods. You can also try a simple Ritz method; it is even more direct.

You can use the Fenics software to do this (http://fenicsproject.org/documentation/tutorial/nonlinear.html).

Best,

This is a boundary value problem, not a root finding problem. One way to solve it is to replace the derivative with a finite difference, as you did, and iteratively relax to the correct solution.

As far as I understood, your problem consists in solving an ordinary differential equation subject to boundary conditions. Your problem can be solved by the finite difference method for nonlinear problems. I come to this conclusion because I cannot fit it into the category of finite difference method for linear problems

I - finite difference method for linear problems $$\frac{d^{2}y(x)}{dx^{2}}+p(x)\frac{dy(x)}{dx}+q(x)y(x) = r(x)$$ $$a \leqslant x \leqslant b$$ $$y(a) = \alpha$$ $$y(b) = \beta$$

II - finite difference method for non-linear problems $$\frac{d^{2}y(x)}{dx^{2}} = f\left( x,y(x),\frac{dy(x)}{dx} \right)$$ $$a \leqslant x \leqslant b$$ $$y(a) = \alpha$$ $$y(b) = \beta$$

Choosing the second case, we apply the finite difference method to your problem $$\frac{du}{dx} = f\left( x,u,\frac{du}{dx}\right) =-u^{2}+x^{2}$$ with $$u(a = 0) = u(b = 1) = 0$$, where $$\alpha = 0$$ and $$\beta = 0$$.

(Attention! Certain criteria must be satisfied before we can apply a routine in search of the ODE solution under the imposed boundary conditions, for more details I refer to Burden's numerical analysis book)

Assuming that we can apply this method to this problem, I made an implementation in the Java language of the finite difference method for nonlinear problems.

// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//              UNIVERSIDADE FEDERAL DE GOIÁS (UFG)             //
//                  INSTITUTO DE FÍSICA (IF-UFG)                //
//                                                              //
// ALUNO     : CARLOS EDUARDO DA SILVA LIMA                     //
// DISCIPLINA: FÍSICA COMPUTACIONAL/CÁLCULO NUMÉRICO            //
// TEMA      : MÉTODO DAS DIFERENÇAS FINITAS NÃO LINEAR         //
// LINGUAGEM : JAVA                                             //
// IDE       : INTELLIJ 2022                                    //
// DATA      : 18/07/2022                                       //
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

import javax.swing.*;
import java.io.FileWriter;

public class Principal
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null,"Nonlinear Finite Differences\n"
+"Author: Carlos Eduardo da Silva Lima");

// STRING´S
String a_    = JOptionPane.showInputDialog("Enter with a");
String b_    = JOptionPane.showInputDialog("Enter with b");
String tol   = JOptionPane.showInputDialog("Enter with tolerance (TOL)");
String alpha_= JOptionPane.showInputDialog("Enter boundary condition y(a) = alpha");
String beta_ = JOptionPane.showInputDialog("Enter boundary condition y(b) = beta");

// Conversion
double a     = Double.parseDouble(a_);
double b     = Double.parseDouble(b_);
double TOL   = Double.parseDouble(tol);
double alpha = Double.parseDouble(alpha_);
double beta  = Double.parseDouble(beta_);

int N = 1000;
int M = 100;

// FINITE DIFFERENCE NONLINEAR CLASS INSTANCE
nonlinearFiniteDifference sol = new nonlinearFiniteDifference();

sol.nonlinearFiniteDifference(a,b,alpha,beta,TOL,N,M);

// Attention
// N: Number of elements in the mesh
// M: Number of iterations to be performed

}// END OF MAIN METHOD, MAIN CLASS
}// END MAIN CLASS

class nonlinearFiniteDifference
{
// MODULE OF A VECTOR
public double mod(double [] x)
{
double modulo = 0;
double soma   = 0;
for(int i = 0; i<x.length; i++)
{
soma += Math.pow(x[i],2);
}
modulo = Math.sqrt(soma);
return modulo;
}

// MODULE OF THE DIFFERENCE OF TWO VECTORS
private double mod(double [] x, double [] y)
{
double modulo = 0;
double soma   = 0;
for(int i = 0; i<x.length; i++)
{
soma += Math.pow((x[i]-y[i]),2);
}
modulo = Math.sqrt(soma);
return modulo;
}

private double f(double x, double y, double yl)
{
return -Math.pow(y,2)+Math.pow(x,2);
}

private double fy(double x, double y, double yl)
{
return -2*y;
}

private double fyl(double x, double y, double yl)
{
return 0;
}

public void nonlinearFiniteDifference(double a_, double b_, double alpha, double beta, double TOL, int N, int M)
{

double [] w = new double[N+4];
double [] a = new double[N+4];
double [] b = new double[N+4];
double [] c = new double[N+4];
double [] d = new double[N+4];
double [] l = new double[N+4];
double [] u = new double[N+4];
double [] z = new double[N+4];
double [] v = new double[N+4];
double [] X = new double[N+4];

double h = Math.abs(b_-a_)/(N+1);
w[0]   = alpha;
w[N+1] = beta;

for(int i = 1; i<=N; i++)
w[i] = alpha+(i*((beta-alpha)/(b_-a_)))*h;

int k = 1;

while(k<=M) // do passo 5 à 16
{
double x = a_+h;
double t = ((w[2]-alpha)/(2*h));
a[1] = 2+Math.pow(h,2)*fy(x,w[1],t);
b[1] = -1+(h/2)*fyl(x,w[1],t);
d[1] = -(2*w[1]-w[2]-alpha+Math.pow(h,2)*f(x,w[1],t));

for(int i = 2; i<=N-1; i++)
{
x = a_+i*h;
t = ((w[i+1]-w[i-1])/(2*h));
a[i] = 2+Math.pow(h,2)*fy(x,w[i],t);
b[i] = -1+(h/2)*fyl(x,w[i],t);
c[i] = -1-(h/2)*fyl(x,w[i],t);
d[i] = -(2*w[i]-w[i+1]-w[i-1]+Math.pow(h,2)*f(x,w[i],t));
}

x = b_-h;
t = (beta-w[N-1])/(2*h);
a[N] = 2+Math.pow(h,2)*fy(x,w[N],t);
c[N] = -1-(h/2)*fyl(x,w[N],t);
d[N] = -(2*w[N]-w[N-1]-beta+Math.pow(h,2)*f(x,w[N],t));

l[1] = a[1];
u[1] = b[1]/a[1];
z[1] = d[1]/l[1];

for(int i=2;i<=N-1; i++)
{
l[i] = a[i] - c[i]*u[i-1];
u[i] = b[i]/l[i];
z[i] = (d[i]-c[i]*z[i-1])/l[i];
}

l[N] = a[N] - c[N]*u[N-1];
z[N] = (d[N]-c[N]*z[N-1])/l[N];

v[N] = z[N];
w[N] = w[N]+v[N];

for(int i = N-1; i>=1; i--)
{
v[i] = z[i] - u[i]*v[i+1];
w[i] = w[i] + v[i];
}

if(mod(v)<=TOL)// do passo 14 à 15
{
for(int i = 0; i<=N+1; i++)
{
x = a_+i*h;
X[i] = x;
//System.out.println("x = "+x+" | w = "+w[i]);
}
break;
}

k = k + 1;
}// fim do loop while

for(int i = 0; i<=N+1; i++)
{
System.out.println("x = "+X[i]+" | w = "+w[i]);
}

// TXT FILE
try
{
FileWriter arquivo = new FileWriter("DiferençasFinitasNãoLinear.txt");

for(int i = 0; i<(N-1); i++)
{
arquivo.write(X[i]+"               "+w[i]+"\n");
}
arquivo.close();
}
catch (Exception e)
{
System.out.println("Erro "+e.getMessage());
}

JOptionPane.showMessageDialog(null,"TXT file successfully created!");

}// END OF THE LINEAR FINITE DIFFERENCE METHOD

}// END OF FINITE DIFFERENCE LINEAR CLASS

/*                           GNUPLOT
cd 'C:\Users\Cliente 14555\Desktop\Introdução à prgramação e Física Computacional\Linguagem Java\Nonlinear Finite-Difference'
plot "DiferençasFinitasNãoLinear.txt" using 1:2 with lines title "x por y(x)"
set grid
set xlabel "x"
set ylabel "y"
# set title "Diferenças Finitas não linear - y''(x) + p(x)y'(x) + q(x)y(x) = f(x,y(x),y´(x))"
set title "Diferenças Finitas não linear - y''(x) = f(x,y(x),y´(x))"
replot

*/


Graph generated from the txt file obtained by applying the numerical method of nonlinear finite differences.