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.