2-D Fixed-point iteration to solve Lame-Embden integral equation with matrix multiplication
You can modify the attached code for your or another problem. The code shows how to deal with finding a root via the fixed-point iteration in 2D.
For example, you have a Lame-Emden equation in the integral form
$$h(x)=1+\int _0^R\frac{y^2}{\max (x,y)}\,\,h(y)\,y\,\text{dy}$$
where $R$ is a positive constant, e.g. star radius. Namely, the smallest real root of $h(R)=0$.
The problem can be discretized using the grid
$$x_i= \left(i-\frac{1}{2}\right)\text{$\Delta $x},\,\,\,i=1,\text{...},n$$
where $\Delta x=\frac{R}{n}$ is the grid spacing. Using the mid-point rule, integration amounts to matrix multiplication, i.e. our integral equation can be written as
$$h_i=1+\sum _{j=1}^n M_{\text{ij}} h_j$$
where $h_i=h (x_i)$ and
$$M_{\text{ij}}=\text{$\Delta $x}\text{ }\left(\frac{x_j^2}{\max \left(x_i,x_j\right)}-x_j\right)$$
Therefore, we can solve our equation by iterating the matrix as follows
$$h_{\text{new}}=1+M\cdot h_{\text{old}}$$
As an initial guess, use $h_i=h (x_i)=1$
n=32; R=Pi; step=R/n; (*inputs*)
(* equidistant x-grid; mid-point rule allows to avoid origin, i.e. division by 0*)
x=Table[(i-0.5)*step, {i,1,n}];
(*We conversed the integral eq. into discrete summation. The scheme relies on matrix multiplication.*)
M=step*Table[(x[[j]])^2/Max[x[[i]],x[[j]]]-x[[j]], {i,1,n}, {j,1,n}];(*matrix updated at each step*)
(*we start from vector of 1's; next the vector of solutions at each grid is updated*)
h=Table[1., {i,1,n}];
err = ConstantArray[1, {100}]; (*for pre-allocation*)
count = ConstantArray[1, {100}];
eps = 10^(-14);
MaxIter = 100;
k=2;
While[k<= MaxIter && err[[k-1]] > eps, (*impose conditions*)
hold=h; (*start*)
hnext = 1+M . hold; (*dot vector onto matrix to update*)
err[[k]] = Norm[hnext-hold]; (*errors*)
h=hnext;
count[[k]]=k;
k++];
(*To display results*)
errors=Transpose[{count=DeleteCases[count,1]-1,DeleteCases[err, 1]}];
head = {"Iterations", "Error"};
Prepend[errors,head]//MatrixForm
Print["The solution vector at each point of grid is: ", h]
We can plot obtained numerical estimation of the root whose value approximates to the $\pi$ value, as it is demonstrated in the image below.
Let's plot the solution
ListPlot[Transpose[{x,h}],
PlotStyle->Magenta, AxesLabel->{"x","h(x)"}, PlotLabel->"Numerical solution" ,
ImageSize->Scaled[0.6]]
T[{x_,y_}] := {x/2,y/3}
. $\endgroup$