5
$\begingroup$

My problem is to solve the following BVP.

enter image description here

and I want to recreate a contour plot like the following graph using Mathematica.

enter image description here

I am trying with the following code.

ClearAll["Global`*"];
k = 5;
α = 3.0*π/4.0;
η = 0;
pde = Laplacian[u[r, θ], {r, θ}, "Polar"] == 
   k^2*u[r, θ];
bc = u[1, θ] == 
   1 - (1 - η)*HeavisideTheta[θ - α];
sol = NDSolveValue[{pde, bc}, u, {r, 0, 1}, {θ, 0, Pi}]
ContourPlot[sol[Sqrt[x^2 + y^2], ArcTan[x, y]], {x, -1, 1}, {y, 0, 1},
  Contours -> 50, ColorFunction -> "Pastel", AspectRatio -> Automatic,
  PlotLegends -> Automatic]

But the code is continuously running without showing any result.

$\endgroup$

2 Answers 2

6
$\begingroup$

Another approach:

When dealing with numerical solvers, discontinuities from functions like HeavisideTheta can introduce major hurdles. Such discontinuities might result in convergence issues or even failures in securing a solution. A typical method to mitigate this is to "smooth out" the discontinuity, allowing the numerical solver to work more effectively.

The logistic function (or sigmoid function) is one such tool to achieve this smoothing:

$$f(\theta)=\frac{1}{1+e^{-a (\theta -\alpha )}}$$

Here:

  • $\alpha$ is the point of discontinuity.
  • $a$ controls the "sharpness" of the transition.

For a large value of $a$, the logistic function closely approximates a step function, making it a suitable choice for this purpose. The advantage of the logistic function is that it transitions smoothly from $0$ to $1$, thereby avoiding the abrupt change that can confound numerical methods.

By using the logistic function, we create a gentle transition around the point of discontinuity, allowing the numerical solver to handle the problem more robustly and often leading to better solutions. The sharpness of this transition can be adjusted by varying $a$, but care should be taken: making the transition too sharp (by choosing a very high value for $a$) might reintroduce numerical issues.

Based on this approach, let's apply the logistic function to smooth out the discontinuity in your differential equation. Here's the solution implemented using a logistic function instead of a piecewise function:

k = 5;
α = 3*π/4;
η = 0;
a = 50;  (*Controls the sharpness of the transition*)

(*Logistic function to smooth the discontinuity*)
smoothStep[θ_, α_, a_] := 1/(1 + Exp[-a (θ - α)]);

pde = Laplacian[u[r, θ], {r, θ}, "Polar"] == k^2*u[r, θ];

bc = u[1, θ] == 1 - (1 - η)*smoothStep[θ, α, a];

sol = NDSolveValue[{pde, bc}, u, {r, 0, 1}, {θ, 0, Pi}];

ContourPlot[sol[Sqrt[x^2 + y^2], Mod[ArcTan[x, y], Pi]], {x, -1, 1}, {y, 0, 1}, 
Contours -> 20, ColorFunction -> "Pastel", AspectRatio -> Automatic, 
PlotLegends -> Automatic, PerformanceGoal -> "Quality", PlotPoints -> 75]

enter image description here

I hope you find it useful!

$\endgroup$
1
  • 1
    $\begingroup$ Thank you for enriching myself. $\endgroup$
    – user94537
    Commented Sep 23, 2023 at 5:37
6
$\begingroup$

Replacing HeavisideTheta with Piecewise makes it work. it could be a limitation of NDSolve. I do not know.

ClearAll["Global`*"];
k=5;
α=3*π/4
η=0;
pde=Laplacian[u[r,θ],{r,θ},"Polar"]==k^2*u[r,θ];
bc=u[1,θ]==1-(1-η)*Piecewise[{{0,θ<α},{1,True}}]
sol=NDSolveValue[{pde,bc},u,{r,0,1},{θ,0,Pi}]

enter image description here

ContourPlot[sol[Sqrt[x^2 + y^2], ArcTan[x, y]], {x, -1, 1}, {y, 0, 1},
  Contours -> 20, ColorFunction -> "Pastel", AspectRatio -> Automatic,
  PlotLegends -> Automatic]

enter image description here

Note that HeavisideTheta can be written using Piecewise

Plot[1 - (1 - η)*   HeavisideTheta[θ - α], {θ, -4, 4}]

enter image description here

Plot[1 - (1 - η)*   Piecewise[{{0, θ < α}, {1, True}}], {θ, -4,   4}]

enter image description here

How to remove the black dots that appears on the boundary of the semicircle?

Just add more PlotPoints:

ContourPlot[sol[Sqrt[x^2 + y^2], ArcTan[x, y]], {x, -1, 1}, {y, 0, 1},
  Contours -> 20, ColorFunction -> "Pastel", AspectRatio -> Automatic,
  PlotLegends -> Automatic, PerformanceGoal -> "Quality", 
 PlotPoints -> 75]

Mathematica graphics

This below shows the problem more clearly

ClearAll["Global`*"];
k = 5;
α = 3*π/4;
η = 0;
pde = Laplacian[u[r, θ], {r, θ}, "Polar"] == 0;
bc = u[1, θ] == 1 - (1 - η)*HeavisideTheta[θ - α];
sol = DSolveValue[{pde, bc}, u[r, θ], {r, θ}]

Mathematica graphics

Now we do the same thing, but using NDSolve instead of DSovle and get an error

ClearAll["Global`*"];
k = 5;
α = 3*π/4;
η = 0;
pde = Laplacian[u[r, θ], {r, θ}, "Polar"] == 0;
bc = u[1, θ] == 1 - (1 - η)*HeavisideTheta[θ - α];
sol = NDSolveValue[{pde, bc}, u[r, θ], {r, 0, 1}, {θ, 0, Pi}]

Mathematica graphics

Changing to Piecewise fixes the above.

Maybe this is a bug or could be a feature.

$\endgroup$
6
  • $\begingroup$ Thank you very much. How to remove the black dots that appears on the boundary of the semicircle? $\endgroup$
    – user94537
    Commented Sep 23, 2023 at 4:37
  • $\begingroup$ @user94537 updated $\endgroup$
    – Nasser
    Commented Sep 23, 2023 at 4:41
  • $\begingroup$ Thank you for your kind help. $\endgroup$
    – user94537
    Commented Sep 23, 2023 at 5:01
  • 1
    $\begingroup$ It's sufficient to replace HeavysideTheta by Unitstep $\endgroup$ Commented Sep 23, 2023 at 10:49
  • $\begingroup$ @UlrichNeumann I see. This looks also like a good workaround. Easier than Piecewise. I did not try it, but I believe you :) $\endgroup$
    – Nasser
    Commented Sep 23, 2023 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.