# Pseudo-Spectral Method, How to Accelerate FindRoot for a Nonlinear Algebraic Equation

We try to solve N times N nonlinear algebraic equations using FindRoot in mathematica.
In fact, it is a nonlinear partial differential equation coupled to two variables. We use a pseudospectral method to extend the function in terms of Chebyshev polynomial series and Fourier series in u and x, respectively. By doing this, we convert the nonlinear PDE into algebraic equations. Then, we evaluate the equations in points of a grid of N times N so that the number of variables is equal to the number of algebraic equations. We then use Findroot to solve these algebraic problems.

Our main problem is that FindRoot takes a lot of time to run the program when we set the size of the N wide grid, such as N = 10. Is there a process to speed up FindRoot or any other method that allows you to to parallelize the FindRoot process?

``````Clear[Nu, Nx, L, psi, eqz, a1, psi1, z1, ay1]; L, 1.646; Nx = 3; Bare
= 6; mpsi = 0;
mu = 4; b = 0;
psi[x_, u_] =
Sum[psi1[j, k] ChebyshevT[j, 2 u - 1] Cos[2 Pi k x/L], {j, 0,
Nu - 1}, {k, 0, Nx - 1}];
a0[x_, u_] : =
Sum[a1[j, k] ChebyshevT[j, 2 u - 1] Cos[2 Pi k x/L], {j, 0,
Nu - 1}, {k, 0, Nx - 1}];
z[x_, u_] : =
Sum[z1[j, k] ChebyshevT[j, 2 u - 1] Cos[2 Pi k x/L], {j, 0,
Nu - 1}, {k, 0, Nx - 1}];
Yes[x_, u_] : =
Sum[ay1[j, k] ChebyshevT[j, 2 u - 1] Cos[2 Pi k x/L], {j, 0,
Nu - 1}, {k, 0, Nx - 1}];
f1 = 1 / Sqrt[2]; f2 = 1 / Sqrt[2]; psiinfty = Pi / 4; psix =
re[psi[x, u], X]; a0x = D[a0[x, u], X]; ayx = D[ay[x, u], X];
zx = D[z[x, u], X]; psiu = D[psi[x, u], u]; a0u =
re[a0[x, u], u]; ayu = D[ay[x, u], u];
zu = D[z[x, u], u];
(* P = 1;
G = 1; *)
A =
1 + h u ^ 2 psiu 2 2 + h zu ^ 2 - u ^ 4 a0u ^ 2 +
h u ^ 4 ayu ^ 2; Ax = - a0x ^ 2 / h + ayx ^ 2 + zx ^ 2 / u ^ 4 +
psix ^ 2 / u ^ 2; Axu = - u ^ 4 a0x ^ 2 ayu ^ 2 - zu ^ 2 a0x ^ 2 -
u ^ 2 psiu ^ 2 a0x ^ 2 - u ^ 4 a0u ^ 2 (zx ^ 2 / u ^ 4 + psix ^ 2 / u ^ 2 + ayx ^ 2) +
h zu ^ 2 ayx ^ 2 + h u ^ 2 psiu ^ 2 ayx ^ 2 + h psiu ^ 2 zx ^ 2 / u ^ 2 +
h ayu ^ 2 zx ^ 2 + h zu ^ 2 psix ^ 2 / u ^ 2 + h u ^ 2 ayu ^ 2 psix ^ 2 -
2 hrs zu psiu zx psix / u ^ 2 - 2 h ayu ayx (zu zx + u ^ 2 psiu psix) +
2 u ^ 2 a0u a0x (u ^ 2 ayu ayx + zu zx / u ^ 2 + psiu psix);
c = psi[x, u] - (1/4) Sin[4psi[4psi[4psi[4psi[x, u]]-
psiinfty + (1/4) Sin[4 psiinfty]; h = 1 - u ^ 4;
P = Sqrt[u^(-4)  A + Ax +  Axu ];
(* Q = Sqrt[G] sqrt[r^4 + A + Axu ] - (1 / u ^ 2) f1 f2 D[z[x,u], u]- 2 c
u ^ 2 (a0u ayx-ayu a0x); *)

G = Sqrt[(F1^2+4(Cos[(F1^2+4(Cos[(f1^2+4(Cos[(f1^2+4(Cos[psi[x, u]]) ^ 4) (f1 ^ 2 +
4 (sin[psi[x, u]]) ^ 4)];
eqpsi = D[(((2 h psiu) / u ^ 2 - 2 a0x ^ 2 psiu u ^ 2 + 2 ayx ^ 2 h psiu u ^ 2 +
2 a0u a0x psix u ^ 2 - 2 ayu ayx h psix u ^ 2 - (2 hrs psix zu zx) /
u ^ 2 + (2 h psiu zx ^ 2) /
u ^ 2) Sqrt[(1/2+4cos[(1/2+4cos[(1/2+4Cos[(1/2+4Cos[psi[x, u]]^ 4) (1/2 +
4 sin[psi[x, u]]^ 4)]) / (2 u ^ 2 [Sqrt](ayx ^ 2 - a0x ^ 2 / h +
psix ^ 2 / u ^ 2 - a0x ^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rs psiu psix zu zx) / u ^ 2 + ayu ^ 2 h zx ^ 2 + zx ^ 2 / u ^ 4 + (
h psiu ^ 2 zx ^ 2) / u ^ 2 - 2 ayu ayx h (psiu psix u ^ 2 + zu zx) +
2 a0u a0x u ^ 2 (psiu psix + ayu ayx u ^ 2 + (zu zx) / u ^ 2) -
a0u ^ 2 u ^ 4 (ayx ^ 2 + psix ^ 2 / u ^ 2 + zx ^ 2 / u ^ 4))), x]-
2 (-a0x ayu + a0u ayx) u ^ 2 (1 -
Cos[4psi[4psi[4psi[4psi[x, u]]) + ([Sqrt](ayx ^ 2 - a0x ^ 2 / h + psix ^ 2 / u ^ 2 -
a0x ^ 2 psiu ^ 2 u ^ 2 + ayx ^ 2 h psiu ^ 2 u ^ 2 + ayu ^ 2 h psix ^ 2 u ^ 2 -
a0x ^ 2 ayu ^ 2 u ^ 4 - a0x ^ 2 zu ^ 2 + ayx ^ 2 h zu ^ 2 + (
h psix ^ 2 zu ^ 2) / u ^ 2 + (
1 + h psiu ^ 2 u ^ 2 - a0u ^ 2 u ^ 4 + ayu ^ 2 hu ^ 4 + h zu ^ 2) / u ^ 4 - (
2 hrs psiu psix zu zx) / u ^ 2 + ayu ^ 2 h zx ^ 2 + zx ^ 2 / u ^ 4 + (
h psiu ^ 2 zx ^ 2) / u ^ 2 - 2 ayu ayx h (psiu psix u ^ 2 + zu zx) +
2 a0u a0x u ^ 2 (psiu psix + ayu ayx u ^ 2 + (zu zx) / u ^ 2) -
a0u ^ 2 u ^ 4 (ayx ^ 2 + psix ^ 2 / u ^ 2 + zx ^ 2 / u ^ 4)) (16 Cos[
psi[x, u]](1/2 + 4 Cos[psi[x, u]]^ 4) sin[psi[x, u]]^ 3 -
16 Cos[psi[x, u]]^ 3 sin[
psi[x, u]](1/2 + 4 Sin[psi[x, u]]^ 4))) / (2 u ^ 2 Sqrt[(1/2 +
4 cos[psi[x, u]]^ 4) (1/2 + 4 Sin[psi[x, u]]^ 4)])
eqaz = D[- (1 / (
2 u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 + (h psiu ^ 2 zx ^ 2) / u ^ 2 -
2 ayu ayx h (psiu psix u ^ 2 + zu zx) +
2 a0u a0x u ^ 2 (psiu psix + ayu ayx u ^ 2 + (zu zx) / u ^ 2) -
a0u ^ 2 u ^ 4 (ayx ^ 2 + psix ^ 2 / u ^ 2 + zx ^ 2 / u ^ 4))) -
2 a0x u ^ 2 (-psiinfty + psi[x, u] + 1/4 sin[4 psiinfty] -
1/4 sin[4psi[4psi[4psi[4psi[x, u]]), u]+
re[((2 ayx + 2 ayx h psiu ^ 2 u ^ 2 + 2 a0u a0x ayu u ^ 4 -
2 a0u ^ 2 ayx u ^ 4 + 2 ayx h zu ^ 2 -
2 ayu h (psiu psix u ^ 2 + zu zx)) Sqrt[(1/2 +
4 cos[psi[x, u]]^ 4) (1/2 +
4 sin[psi[x, u]]^ 4)]) / (2 u ^ 2 [Sqrt](ayx ^ 2 - a0x ^ 2 / h +
psix ^ 2 / u ^ 2 - a0x ^ 2 psiu ^ 2 u ^ 2 + ayx ^ 2 h psiu ^ 2 u ^ 2 +
ayu ^ 2 h psix ^ 2 u ^ 2 - a0x ^ 2 ayu ^ 2 u ^ 4 - a0x ^ 2 zu ^ 2 +
ayx ^ 2 h zu ^ 2 + (h psix ^ 2 zu ^ 2) / u ^ 2 + (
1 + h psiu ^ 2 u ^ 2 - a0u ^ 2 u ^ 4 + ayu ^ 2 hu ^ 4 + h zu ^ 2) /
u ^ 4 - (2 hours psiu psix zu zx) / u ^ 2 + ayu ^ 2h zx ^ 2 + zx ^ 2 /
u ^ 4 + (h psiu ^ 2 zx ^ 2) / u ^ 2 -
2 ayu ayx h (psiu psix u ^ 2 + zu zx) +
2 a0u a0x u ^ 2 (psiu psix + ayu ayx u ^ 2 + (zu zx) / u ^ 2) -
a0u ^ 2 u ^ 4 (ayx ^ 2 + psix ^ 2 / u ^ 2 + zx ^ 2 / u ^ 4))) +
2 a0u u ^ 2 (-psiinfty + psi[x, u] + 1/4 sin[4 psiinfty] -
1/4 sin[4psi[4psi[4psi[4psi[x, u]]), X];

eqa0 = D[((-2) +
2 a0x u ^ 2 (psiu psix + ayu ayx u ^ 2 + (zu zx) / u ^ 2) -
2 a0u u ^ 4 (ayx ^ 2 + psix ^ 2 / u ^ 2 + zx ^ 2 / u ^ 4)) Sqrt[(1/2 +
4 cos[psi[x, u]]^ 4) (1/2 +
4 sin[psi[x, u]]^ 4)]) / (2 u ^ 2 [Sqrt](ayx ^ 2 - a0x ^ 2 / h +
psix ^ 2 / u ^ 2 - a0x ^ 2 psiu ^ 2 u ^ 2 + ayx ^ 2 h psiu ^ 2 u ^ 2 +
ayu ^ 2 h psix ^ 2 u ^ 2 - a0x ^ 2 ayu ^ 2 u ^ 4 - a0x ^ 2 zu ^ 2 +
ayx ^ 2 h zu ^ 2 + (h psix ^ 2 zu ^ 2) / u ^ 2 + (
1 + h psiu ^ 2 u ^ 2 - a0u ^ 2 u ^ 4 + ayu ^ 2 hu ^ 4 + h zu ^ 2) /
u ^ 4 - (2 hours psiu psix zu zx) / u ^ 2 + ayu ^ 2h zx ^ 2 + zx ^ 2 /
u ^ 4 + (h psiu ^ 2 zx ^ 2) / u ^ 2 -
2 ayu ayx h (psiu psix u ^ 2 + zu zx) +
2 a0u a0x u ^ 2 (psiu psix + ayu ayx u ^ 2 + (zu zx) / u ^ 2) -
a0u ^ 2 u ^ 4 (ayx ^ 2 + psix ^ 2 / u ^ 2 + zx ^ 2 / u ^ 4))) +
2 ayx u ^ 2 (-psiinfty + psi[x, u] + 1/4 sin[4 psiinfty] -
1/4 sin[4psi[4psi[4psi[4psi[x, u]]), u]+
re[((((2 a0x) / h) - 2 a0x psiu ^ 2 u ^ 2 - 2 a0x ayu ^ 2 u ^ 4 -
2 a0x zu ^ 2 +
2 a0u u ^ 2 (psiu psix + ayu ayx u ^ 2 + (zu zx) / u ^ 2)) ((1/2 +
4 cos[psi[x, u]]^ 4) (1/2 + 4 Sin[psi[x, u]]^ 4)) ^ (
1/4)) / (2 u ^ 2 [Sqrt](ayx ^ 2 - a0x ^ 2 / h + psix ^ 2 / u ^ 2 -
a0x ^ 2 psiu ^ 2 u ^ 2 + ayx ^ 2 h psiu ^ 2 u ^ 2 +
ayu ^ 2 h psix ^ 2 u ^ 2 - a0x ^ 2 ayu ^ 2 u ^ 4 - a0x ^ 2 zu ^ 2 +
ayx ^ 2 h zu ^ 2 + (h psix ^ 2 zu ^ 2) / u ^ 2 + (
1 + h psiu ^ 2 u ^ 2 - a0u ^ 2 u ^ 4 + ayu ^ 2 hu ^ 4 + h zu ^ 2) /
u ^ 4 - (2 hours psiu psix zu zx) / u ^ 2 + ayu ^ 2h zx ^ 2 + zx ^ 2 /
u ^ 4 + (h psiu ^ 2 zx ^ 2) / u ^ 2 -
2 ayu ayx h (psiu psix u ^ 2 + zu zx) +
2 a0u a0x u ^ 2 (psiu psix + ayu ayx u ^ 2 + (zu zx) / u ^ 2) -
a0u ^ 2 u ^ 4 (ayx ^ 2 + psix ^ 2 / u ^ 2 + zx ^ 2 / u ^ 4))) -
2 ayu u ^ 2 (-psiinfty + psi[x, u] + 1/4 sin[4 psiinfty] -
1/4 sin[4psi[4psi[4psi[4psi[x, u]]), X];
bc1 = psi[x, 0] - psiinfty;
bc2 = D[psi[x, u], u]/. u -> 0 - mpsi;
bc3 = a0[x, 0] - mu;
bc4 = a0[x, 1];
bc5 = z[x, 0];
bc6 = ay[x, 0] - b x;

tb = Table[{u -> a, x -> a2}, {a, .1, .5, .2}, {a2, .1, 1, .3}];
tbu = tb[[1, 1]][[1, 1]]->
Flatten[Table[Table[tb[[j, i]][[1, 2]], {i, 1, 4}], {j, 1, 3}]];
tbx = tb[[1, 1]][[2, 1]]->
Flatten[Table[Table[tb[[j, i]][[2, 2]], {i, 1, 4}], {j, 1, 3}]];

cpt = {tbu, tbx};
dat1 = Flatten[
Table[{psi1[j, k] -> 0, a1[j, k] -> 0}, {j, 0, Nu - 1}, {k, 0,
Nx - 1}]];
dat2 = Flatten[
Table[{Ay1[{Ay1[{ay1[{ay1[j, k] -> 0, z1[j, k] -> 0}, {j, 0, Nu - 1}, {k, 0,
Nx - 1}]];
dat3 = Flatten[{dat1, dat2}];
Length[dat3]
eqs = {{eqa0, eqay, eqaz, eqpsi} /.
cpt, {bc1, bc2, bc3, bc4, bc5, bc6} /.
x -> Table[tbx[[2, i]], {i, 1, 4}]};
Length[Flatten[eqs]]72

72

s1 = FindRoot[Re[eqs], dat3 /. Rule -> List,
Method -> {"Newton", "StepControl" -> "TrustRegion"}]//
AbsoluteTiming
Sort[Flatten[eqs /. s1[[2, All]]]]// AbsoluteTiming
``````