# Finite Difference equations - slow computations

The following code I have written runs very slow, in fact, so slow that it does not finish computation.

alpha = 0.50;
xmin = 1;
xmax = 15;
xdivisions = 14;
dx = (xmax - xmin)/xdivisions;
xgrid = Range[xmin, xmax, dx];
U = Array[u, xdivisions + 1, {xmin, xmax}];
a[x_] := -1;
b[x_] := -(3/x);
c[x_] := 1;
d[x_] := -1.5;
e[x_] := alpha/2;
aGrid = Map[a,xgrid];
bGrid = Map[b,xgrid];
cGrid = Map[c,xgrid];
dGrid = Map[d,xgrid];
eGrid = Map[e,xgrid];
dudx = NDSolveFiniteDifferenceDerivative[Derivative[1], xgrid, U];
dudx2 = NDSolveFiniteDifferenceDerivative[Derivative[2], xgrid,U];
uleft = NSolve[dudx[[1]] == 0, U[[1]]];
uright = NSolve[U[[-1]] == 0, U[[-1]]];
boundary = Join[uleft[[1]], uright[[1]]];
equations = Map[(0 == #) &, Flatten[Table[aGrid[[i]]*dudx2[[i]] +

bGrid[[i]]*dudx[[i]] + cGrid[[i]]*U[[i]] + dGrid[[i]]*(U[[i]]^2) + eGrid[[i]]*(U[[i]]^3), {i, 1, Length[xgrid]}] /. boundary]];
intSol = NSolve[equations, U[[2 ;; Length[xgrid] - 1]]][[1]]


The problem is with the final line of code. The fifteen finite difference equations just don't finish giving an output! :(

Edit:

Thanks for the help with that part of the question.

I would like to ask a small further question.

I appended the following to the end of my code. For some reason, my laptop's again taking forever to finish computations.

intSol = FindRoot[equations[[;; Length[xgrid] - 2]], Map[{#, 1} &, Apply[ Union, Map[Variables, equations [[All, 2]] ] ] ]];
boundarySol = (boundary /. intSol);
solutionArray = (U /. intSol) /. boundarySol;
dataPoints = Table[{xmin + i*dx, ymin + j*dy, solutionArray[[i + 1, j + 1]]}, {i, 0, Length[xgrid] - 1}, {j, 0, Length[ygrid] - 1}];
ListPlot3D[Flatten[dataPoints, 1], AxesLabel -> {Style[x, Medium, Blue], Style[y, Medium, Blue], Style[u, Medium, Blue]}, PlotRange -> All];


Any help would be very nice.

• It seems you have 15 equations and 13 variables Oct 29, 2015 at 16:29
• I omitted the first and last equations from the penultimate line of code. Having done that, once I run the code, all my other laptop functions freeze. Is there an explanation for this? Oct 29, 2015 at 16:34
• Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! Oct 29, 2015 at 16:41

This solves your first 13 equations (you have 13 variables)

s = FindRoot[equations[[;; 13]], {#, 1} &/@ Union@@ (Variables /@ equations[[All, 2]])]
(*
{u[2] -> 0.763932, u[3] -> 0.763932, u[4] -> 0.763932,
u[5] -> 0.763932, u[6] -> 0.763932, u[7] -> 0.763932,
u[8] -> 0.763932, u[9] -> 0.763932, u[10] -> 0.76393,
u[11] -> 0.763907, u[12] -> 0.763604, u[13] -> 0.759619,
u[14] -> 0.706721}
*)

• Thank you! Can you tell why my first method did not work? Oct 29, 2015 at 16:44
• @failexam Probably because NSolve[ ] uses the symbolic form while FindRoot[ ] is purely numeric Oct 29, 2015 at 16:46