/ tutorials / laplace-dirichlet
Sign in
tutorials / laplace-dirichlet
laplace-dirichlet / src / matlab / lp_exact2d.m
33 lines · 1.3 KBBlameHistoryRaw
1function U = lp_exact2d(kind, X, Y, par)
2% LP_EXACT2D A harmonic function on the unit square, used as ground truth.
3%
4% Every choice here satisfies Laplace's equation exactly, so taking its trace
5% on the boundary as Dirichlet data makes it the exact solution of the
6% Dirichlet problem, and the difference between a computed solution and this
7% function is the whole of the error. par is a per-kind shape parameter.
8if nargin < 4
9 par = 1;
10end
11switch kind
12 case 'expsin'
13 % exp(x) sin(y): entire, gentle, the standard test
14 U = exp(X) .* sin(Y);
15 case 'poly'
16 % Re (x + iy)^k: a harmonic polynomial of degree k
17 k = round(par);
18 U = real((X + 1i*Y).^k);
19 case 'wave'
20 % exp(k*y) sin(k*x): oscillatory along one edge, growing across the
21 % square by a factor exp(k), which is where its conditioning goes
22 k = par;
23 U = exp(k*Y) .* sin(k*X);
24 case 'log'
25 % log|z - z0| with z0 a distance par outside the right edge: harmonic
26 % inside, but with a singularity just off the domain, so the solution
27 % has structure on the length scale par near that edge
28 z0 = (1 + par) + 0.5i;
29 U = log(abs((X + 1i*Y) - z0));
30 otherwise
31 error('lp_exact2d: unknown solution "%s"', kind);
32end
33end
↑↓ move↵openescclose