Polska wersja
Numerical Differential Equations
winter semester 2019-20
Time: Monday lecture 1415-1545 room 1780 and classes/lab 1605-1735 room 1780 or computer lab 2043 (MIMUW bdg.,
Banacha 2 - entrance - Pasteura Street)
Evaluation
an oral exam. Three questions from the list of approx. 10-12. One is chosen by me the other by the student.
Or alternatively one can prepare a project.
List of projects in pdf file (originally the orojects were in Polish and they were automatically translated, thus English may be poor -if something is unclear or one has any questions or comments please ask).
Exam questions
I will ask three questions from the list below - the 1st two ones may be selected by a student, the last one by myself.
I will add new questions as the course progresses...
- One step schemes. Examples and convergence theory.
- Multistep schemes . Examples including Adams schemes.
Convergence theory.
- Stiffness. Definition. Examples of ODEs. Idea of adaptive step control.
- Finite difference method for elliptic equation in 1D. 1D Example: - u''+u=f u(a)=u(b)=0
and detailed convergence analysis of this problem in a discrete maximum norm
- Finite difference method for elliptic equations. 2D Example: -Laplacian u=f , u=0 on boundary.
Abstract convergence analysis: the order of local truncation error and stability. Discrete convergence -
definition and Lax-Filipov theorem.
- Idea of Finite Element Method. Analysis of convergence of linear finite element in 1D for -u''=f,
u(a)=u(b)=0.
- Elements of abstract FEM theory: Lax Milgram theorem. Cea Lemma. Application to 2d elliptic
boundary problem with homogeneous Dirichlet boundary element.
- Elements of abstract FEM theory: continuous FEM spaces, affine families of FEM spaces, shape regularity
etc
- FEM and different boundary conditions for 2nd order elliptic boundary problem: non-zero Dirichlet, Neumann, Robin,
mixed. (optional; it is not discussed in Lect Notes. I hope it will be discussed during a lecture but I will not ask this question
unless somebody choose it)
- Schemes for parabolic differential equations: FDM and FEM in 1D or 2D - an idea, some properties - no proofs
(optional - as in the lecture notes)
- Basic explicit finite difference schemes for hyperbolic PDEs. Stability and consistency. (optional - if somebody really like the subject)
- Pure Neumann boundary conditions for -Laplacian u=f. How to solve it using FEM? How to solve the singular linear problem arising there?
optional - means that this question may be asked only with the student consent
Link to current lab
Syllabus
Numerical methods for
- ordinary differential equations (ODEs)
- elliptic partial differential equations (PDEs)
- evolutionary PDEs (parabolic and hyperbolic of first order)
the following classes of methods are going to be discussed
- one-step and linear multi-step schemes for initial ODEs problems
- finite difference method
- finite element method
There are some computer labs (instead of standard "blackboard" classes)
The course is elementary - it is required to know the basics of liner algebra, mathematical
analysis and theory of ODEs.
It is not necessary to have any knowledge from PDEs theory
all necessary facts will be given during our course
There are lecture notes for this course in Polish.
Lecture notes
(In Polish)
Leszek Marcinkowski, Numeryczne równania różniczkowe, 2010.
Published on-line:
WWW page
(there is a link to pdf file with the lecture notes).
Pdf file with the newest version of the notes.
Please, send me an e-mail with comments if you find any errors, typos etc,
References
Text books
-
Deuflhard, Peter, Bornemann, Folkmar, Scientific Computing with Ordinary Differential Equations,
Series: Texts in Applied Mathematics, Vol. 42, Springer-Verlag, New York, 2002.
(theory of ODEs, ODE schemes, Boundary Value Problems in 1D)
One can download a pdf file from MIMUW computers (valid Dec 2014):
Springer link
-
David F. Griffiths, Desmond J. Higham,
Numerical Methods for Ordinary Differential Equations,
Springer-Verlag, 1st Edition, London, 2010. An elementary textbook on ODE schemes.
One can download a pdf file from MIMUW computers (valid Dec 2014):
Springer link
- Claes Johnson, Numerical solution of partial differential equations by the finite element method,
Cambridge University Press, Cambridge, 1987.
- Randall J. LeVeque, Finite difference methods for ordinary and partial differential
equations, Society for Industrial and Applied Mathematics (SIAM), Philadelphia,
PA, 2007, Steady-state and time-dependent problems.
(numerical schemes for ODES, finite difference methods for elliptic and parabolic PDEs)
- Alfio Quarteroni, Riccardo Sacco, and Fausto Saleri, Numerical mathematics, Texts
in Applied Mathematics, vol. 37, Springer-Verlag, New York, 2000.
(numerical schemes for ODEs and some PDES - hyperbolic nad parabolic)
One can download a pdf file from MIMUW computers (valid Dec 2014):
Springer Link
- John C. Strikwerda, Finite difference schemes and partial
differential equations, second ed., Society for Industrial and Applied
Mathematics (SIAM), Philadelphia, PA,
2004. (FD schemes for PDEs - all types)
Monographs or advanced text books
-
Dietrich Braess, Finite elements, third ed., Cambridge University Press, Cambridge,
2007, Theory, fast solvers, and applications in elasticity theory, Translated from the
German by Larry L. Schumaker. (advanced text book)
- Susanne C. Brenner and L. Ridgway Scott, The mathematical theory of finite element
methods, third ed., Texts in Applied Mathematics, vol. 15, Springer, New York, 2008.
- J. C. Butcher, Numerical methods for ordinary differential equations, second ed.,
John Wiley and Sons Ltd., Chichester, 2008.
-
P. G. Ciarlet and J.-L. Lions (eds.), Handbook of numerical analysis. Vol. II,
Handbook of Numerical Analysis, II, North-Holland, Amsterdam, 1991, Finite element
methods. Part 1.
- Philippe G. Ciarlet, The finite element method for elliptic problems, Classics in
Applied Mathematics, vol. 40, Society for Industrial and Applied Mathematics (SIAM),
Philadelphia, PA, 2002, Reprint of the 1978 original [North-Holland, Amsterdam].
-
E. Hairer, S. P. Norsett, and G. Wanner, Solving ordinary differential equations. I,
second ed., Springer Series in Computational Mathematics, vol. 8, Springer-Verlag,
Berlin, 1993, Nonstiff problems.
- E. Hairer and G. Wanner, Solving ordinary differential equations. II, second ed.,
Springer Series in Computational Mathematics, vol. 14, Springer-Verlag, Berlin,
1996, Stiff and differential-algebraic problems.
-
Bosko S. Jovanovich, Endre Suelli, Analysis of Finite Difference Schemes
For Linear Partial Differential Equations with Generalized Solutions, Springer Series in Computationam
Mathematics, volume 46, Springer , 2014.
Springer link (not available on MIMUW
servers)
- Alfio Quarteroni and Alberto Valli, Numerical approximation of partial differential
equations, Springer Series in Computational Mathematics, vol. 23, Springer-Verlag,
Berlin, 1994.
(FD schemes and FE for PDEs)
One can download a pdf file from MIMUW computers (valid Dec 2014):
Springer Link
- J. W. Thomas, Numerical Partial Differential Equations, Finite Difference
Methods, Texts in Applied Mathmematics, volume 22, Springer, 1995.
Springer link (valid Nov 2016)
LAB
link to Octave (one can download linux
or windows version of octave)
octave-forge - octave extension
octave manual in html
Lab 1 (1st two weeks of classes) Introduction to octave.
Euler's schemes
- Read the code in nrrbasic.m
- Create any matrices 3x4 A i 3x5 B - and then a matrix 3x8 C whose first 3 columns are A and next B.
Extract from C a block D from C(1,1) to C(3,3). Flip colums of D. Write D to a file.
(binary or ASCII) - change D(1,1) to -100 in the ascii-file and read the new matrix to octave'a.
Compute norms of D e.g. 1st, 2nd, Frobenius ones etc.
- Compute discrete max norm of (sin(x))^2 on [0,1] (without using loops).
- Write Euler's schemes for y'=ay y(0)=1, a=1,10,100,-1,-10,-100 on [0,T] - write graphs
for T=1,10,100, compute errors: ae(T;h)=|y(T)-y_h(T)| and ce(T;h)=ae(T;h)/y(T) and
ae(h;h)=|y(h)-y_h(h)| i ce(h,h)=ae(h;h)/y(h).
Print ae(t;h) i ce(t;h) for t\in[0,T] on the display.
m-file with explicit Euler scheme and a script with some tests:
testodelin19.m
- a script with some
tests of the explicit Euler scheme for dx/dt=ax x(t0)=x0 (Oct 7, 2019)
Lab 2 - continuation...
Function lsode() in octave and simple ODE schemes cont.
In multistep ODE schemes take x_1,x_2 etc as exact solutions if not take x_k k=1,..,p-
p-1 computed by using some explicite 1-step method of the same order e.g. Taylor scheme for midpoint one.
- Write a function with an implementation of the explicit Euler scheme working for any 1st order ODEs. The INPUT parameters - a handle to the vector field, the ends of the interval, an initial value, no of discrete time points plus one. OUTPUT the vectors of discrete times and the approximations of the discrete solution.
Test the scheme for y'=ay y(0)=1 on [0,T] with a=-1,1,10,-10,100,-100 and T=1,10,100 and h=0.1,0.01,1e-3.
Plot the graphs of the approximation and exact solutions
and compute errors: ae(T;h)=|y(T)-y_h(T)| and ce(T;h)=ae(T;h)/y(T).
Print ae(t;h) i ce(t;h) for t\in[0,T] on the display.
- Repeat the problem for the linear penduulum model: y''=-y z y(0)=0;y'(0)=1 - draw the computed orbits (y,y'). Are they
contained in a circle or periodical?
- Do the same but for y'=1+y; y(0)=0 on [0,T) for T=0.5,1,1.5.
Compute error t=0.5,1.5 for h=0.5,1/4,1/8 etc.
- read help for lsode - draw solutions for y'=-0.1y y(0)=1 on [0,20]using lsode().
Do the same for y'=(cos(x))^2 z y(0)=0 on [0,T] for T=1,10,20 or y'=1+y*y, y(0)=0.
- Draw graphs of solution and its derivative and of orbit (y(t),y'(t)) for y''=-y y(0)=0 y'(0)=1.
Check if (y(T)^2+(y'(T)^2==1 for large T and different h.
- Implement midpoint and Taylor schemes of order 2
for y'=ay, y(0)=1, a=1,10,100,-1,-10,-100 on [0,T] draw the graphs
for T=1,10,100, compute errors for different T etc
Compare the graphs and errors with the results for explicit Euler scheme
- Do the same for
y''=-y, y(0)=0;y'(0)=1 - draw the orbit (y,y'). Are orbits periodical?
Repeat the problem for y''=-sin(y), y(0)=0,y'(0)=1. Are computed solutions periodical?
Compare with lsode() solutions.
- Test error of midpoint for y'=-y, y(0)=1 on [0,T] for growing T with h=0.1,1e-2,1e-3 etc Draw graphs.
- Test experimentally the order of local truncation error of Taylor or midpoint scheme
for all IVP with known solutions: :
y'=ay,y(0)=1; a=-100,-10,-1,1,10,100 for t=1,10,100 (if there is no fl overflow)
do same for y''=y y(0)=0 y'(0)=1; y'=cos^2(y) y(0)=0 for t=1 i 100 itd
Do same for the pendulum eq: y''=-sin(y) y(0)=0 y'(0)=1 taking the lsode() solution as the exact one.
exEuler19.m - implementation of expicit Euler scheme
midpoint19.m - implementation of midpoint scheme
testexEuler19.m - test of explicit (forward) Euler in 1D and 2D
taylor19.m - implementation of the forward 2nd order Taylor scheme
Lab 3 Experimental testing the order of ODE schemes cont.
Testing starting for multistep schemes (for 2-step Adams-Bashforth).
- Plot the solutions of y'=ay y(0)=1 a=1,-1 using Taylor or midpoint schemes (e.g. N=20 T=4).
Test the convergence order od Taylor and midpoint scheme for this problem (one can use the scripts from this page...)
- Explicit two-step Adams-Bashforth scheme x.
x(n+1)=x(n)+(h/2)*[3*f(n)-f(n-1)] with f(n)=f(t(n),x(n))
Draw graphs of solutions: y'=-y, y(0)=1 or y''=-y y(0)=0;y'(0)=1
itp (y_1 take as a solution or compute by Heun method).
- Test the order of local truncation error of explicit 2-step Adams-Bashford scheme on
y'=ay, y(0)=1, : a=1,-1.
- Test the convergence order of explicit 2-step Adams-Bashford scheme on
y'=ay, y(0)=1, x_1=exp(ah) : a=1,-1.,1
for T=0.1,1,10,100. (using the method of halving the step i.e.
compute e(T,h)/e(T,h/2) for e(t,h)=||x(T,h)-sol(T)||; x(t,h) approximation
of the soultion sol(T) computed by the scheme using the step h)
Take different x_1: e.g. x_1=exp(ah), x_1 computed by Heun or explicit Euler: x_1=x_0+h*f(t_0,x_0).
Do you see any difference?
- Test the convergence order for Taylor(2nd order), Heun
y(n+1)=y(n)+ 0.5h*(f(n)+f(t(n+1),y(n)+hf(n))
and modified Eulera schemes
y(n+1)=y(n)+ h*f(t(n)+0.5h,y(n)+0.5hf(n))
on the IVP:
y'=ay, y(0)=1, x_1=exp(ah) for a=1,-1 and t=0.1,1,10,100.
- Test the order of convergence of Heun,, implicit two-step Adams and modified Eulera schemes on
y'=1+y^2, y(0)=0 for t=0.1,1,1.5, 1.54. What happens close to pi/2?
- Test the order of convergence of Heuna and modified Eulera schemes on
for y''=-y'', y(0)=0 y'(0)=1 for t=0.1,1,10,100,1000.
Are the computed orbits periodical?
Repeat the problem on
y'=-sin(y), y(0)=0, y'(0)=1.
- Write predictor corrector scheme -for explicit and implicit Euler schemes nonlinear system is solved by Banach
iterations:
x^{k+1}=x(n)+h*f(x^k,t^k+h) (if x^{k+1}=x^k then x^k=x_(n+1)) for x^0=x(n)+hf(n) (predictor ex. Euler)
-
no of nonlinear iterations M should be a parameter - test for M=1,2,3 -It can be written with a stopping criteria
like |x^{k+1}-x(n)-h*f(x^k,t^k+h)|<= h or <= x(n)*h or preset no of iterations e.g. one or two or three.
- Implement 2step Adams-Moulton scheme
x(n+1)=x(n)+(h/12)*(5*f(n+1)+8*f(n)-f(n-1)) with f(n)=f(t(n),x(n))
and test the order of convergence - use 1step
Heun scheme for computing x(1).
- Implement 2step predictor - corrector scheme: take 2step Adams-Bashford as a
predictor and 2-step Adams-Moulton as a corrector. Test the order of convergence
- Repeat tests for explicit 3step Adams-Bashford scheme::
x(n+1)=x(n)+(h/12)*[23*f(n)-16*f(n-1)+5*f(n-2)] with f(n)=f(t(n),x(n))
x(1),x(2) may be computed by Heun scheme (order 2) - or substitute x(t0+h) x(t0+2h) with x(t) the solution of IVP.
AdamsBashford19.m
- explicit Adams-Bashforth scheme (of order 2)
Heun19.m
- explicit Heun scheme (a Runge-Kutta scheme of order 2)
testconord19.m
- tests of convergence order of the following schemes: explicit Euler (order 1), Adams-Bashforth 2 step, midpoint, Taylor of 2nd order, Heun schemes (of order 2)
teststart19.m
- tests of convergence order of Adams-Bashforth scheme for different starting values of x1 \approx x(t0+h)
-a/exact value i.e. x1=solution at t0+h b/ x1 computed by one step of forward Euler of order one c/x1 computed by one step of forward Heun scheme (of order 2)
Lab 4. Stiffness. Shooting method for -u''+bu'+ cu=f with Dirichlet bnd
cond : u(a)=alpha u(b)=beta and for mixed bnf conditions:
u'(a)=alpha u(b)=beta
- Some older problems e.g. test the order of Loca lTruncation Error (LTE) of some schems:
forward Euler, cakward Euler, midpoint Heun, Taylor two step Adams-Bashforth etc For IVP: y'=ay a=1,-1 with solution y(t)=exp(a*t)
and in 2D : y''=-y with y(t)=cos(t).
How to do it e.g. compute loca lLTE for a given t=1 for forward Euler for x'=f(x,t): compute e(h,t):=||x(t+h)-x(t)-h*f(x(t),t)||
then e(h/2,t) and compute e(h,t)/e(h/2,t) - if it is 2^p - it means that for this case e(h,t) is like h^p.
We can also compute e(h)=max_k e(h,t(k)) taking t(k)=t0+h*k N+1 (N*h=T-t0) equidistant points on [t0,T].
- Solve some simple problems using lsode() - plot the graphs of the solution or trajectories in 2D cases e.g.
y'=ay y(0)=1; (penduulum equations) y''=-y (linear) or y''=-sin(y) with y(0)=1 y'(0)=0.
- Compare computation times of X(T) for X the solution of dX/dt=AX with X(0)=[1;2]
for the matrix [-1,-10;-1,-11] and T=10,100,1000,1000 etc once using
lsode() with stiff solvers and once with non-stiff (lsode_options("integration
method","non-stiff")). To get real time use: (tic;lsode(..);toc)
- Implement a predictor-corrector scheme based on Heun method (predictor) and trapezoid method (corrector) i.e.
for a known x(n) compute y(n) by the Heun method
y(n+1)=x(n)+ 0.5h*(f(n)+f(t(n+1),x(n)+hf(n))
and then substitute
y(n) into (replacing x(n+1) in the rhs of the trapezoid scheme i.e. we get
x(n+1)=x(n)+0.5 h(f(n)+f(t(n+1),y(n+1)).
Then test the convergence order for y'=ay a=1,-1, y(0)=1 on [0,1] and [0,10]
- starting with N=20.
-
One an also change the p-c scheme correcting twice i.e. computing y(n) by Heun method, then
z(k+1)=x(n)+0.5 h(f(n)+f(t(n+1),z(k)) k=0,1 and z(0)=y(n+1) obtaining z(1) which is taken a new approximation of
x(n+1).
We can actually correct many times e.g. p-times i.e. taking k =0,..,p-1 or as many times as long
||z(k+1)-x(n)-0.5 h(f(n)+f(t(n+1),z(k))||>TOL taking e.g. TOL=Ch^2 for some constant C. This is equivalent to solving the equation in one step of the implicit trapezoid method by a version of the Banach iterative method.
(in trapezoid method x(n+1) is a fix point of the function G(y)=x(n)+h*f(t(n+1),y) and our iteration is z(k+1)=G(z(k))).
Implement the p-c scheme which use Heun as the predictor and correct twice (corrector - trapezoid method)
and test the convergence order as before.
EulerLTE19.m
- tests of order of LTE for Euler methods
HeunLTE19.m
- tests of order of LTE for Heun method
trapzPC.m
- predictor-corrector with Heun as the predictor and the trapezoid as the corrector
testtrapzPC.m
- tests of predictor-corrector with Heun as the predictor and the trapezoid as the corrector
Lab 5.
Shooting method and finite difference method (FDM) for
-u''+d(x)u' c(x)u=f with Dirichlet bnd
cond : u(a)=alpha u(b)=beta and for mixed bnf conditions:
u'(a)=alpha u(b)=beta
-
Implement shooting method for
y''-d(x)y'- c(x)y=f(x), y(a)=ya; y(b)=yb, a, b, ya,yb given,
d,c,f given functions on [a,b]
(2 shoots with y'(a) equal to s_1=0 and s_2=1 i.e. we solve the equation with
y(a)=ya; y'(a)=s_k - get two solutions for t=b
: y(b;s_k) k=1,2 and hence we compute s such that the solution with y(a)=ya; y'(a)=s_k is such that y(b)=tb).
- Solve by the shooting method:
-y''+y=0; y(0)=1; y(b)=1 for b=1,4,10,15,20.
Draw graphs. Compute errors |y(b;s)-yb|.
- Solve using the shooting method:
- Linear 2nd order BVP with sin() RHS
-y''+y=f z y(0)=0; y(b)=0 for f=2*sin(x) and b=\pi (the solution can be computed).
- Linear 2nd order BVP with polynomial quadratic RHS
-y''+y=-2+(1-x*x); y(-1)=0; y(1)=0. (the solution can be computed).
- Linear 2nd order BVP with nonconstant coeff.
-y''+exp(-x) y =cos(x); y(-1)=0; y(1)=0.
- Linear 2nd order BVP with cos() RHS and a large coeff. and the 1st order term (large advection a)
-y''+ay'+ y =cos(x); y(-1)=0; y(1)=0 for a=0,1,10,100,-1,-10,-100.
- Nonconstant coeff.
-y''+(1+x*x)*y=sin(x)+ (1+x*x)*sin(x) with y(0)=0 and y(1)=sin(1) (the solution can be guessed i.e. y(x)=sin(x)).
Plot the graphs. Compute |y(b;s)-yb|.
- Implement the shooting method for
y''=F(t,y,y'), y(a)=ya; y(b)=yb.
Use lsode() and fsolve() or fzero() (or implement your own nonlinear solver e.g. the bisection method)
Test for F(t,y,y')=sin(y), y(0)=1, y(1)=2 and (2) y''=F(x,y,y')=y^2 x(0)=1 x(1)=-4 (two solutions s1 approx -6 and s2 \approx -22!)
linshoot.m
a function solving by a shooting method a linear 2nd order BVP: -y''+d(x)y'+c(x)y=f(x) y(a)=al y(b)=be
testshoot19.m some test of
-shooting method for nonlinear y''=F(x,y,y') y(a)=al y(b)=be
shooting.m
-a function solving by a shooting method a nonlinear BVP: y''=F(x,y,y') y(a)=al y(b)=be
Lab 6. FDM for Poisson equation in 1D
-
Form a tridiagonal symmetric matrix with two on main diagonal and minus one on
sub and superdiagonals: using a loop and the octave function diag()
Form this matrix in a sparse format using the octave function sparse() without using
diag() or forming any full format matrix.
-
Solve the FDM problem
-u''+u=0 , u(0)=u(T)=1 on [0,T]
for the known solution u(x) (you should compute
it...) for T=1,5,10,15,20 for N=100 and 1000. Compare with the shooting method.
- Compute the order of local truncation error of standard FDM for -u''=\sin(x) , u(0)=u(\pi)=0
-
Solve the FDM problem
-u''=f , u(0)=u(\pi)=0 on [0,\pi] for the known solution u(x)=\sin(x)
on the mesh of 10,20,40,80 points. Compute the discrete error in max and discrete L^2 norms.
- Compute the order of local truncation error of standard FDM for
-u''=\sin(x) , u(0)=u(\pi)=0
in L^2 and max discrete norms.
-
Compute the order of error of standard FDM for
-u''=\sin(x) , u(0)=u(\pi)=0
in L^2 and max discrete norms by the halving the mesh size method. I.e we compute the error
e_h and e_{h/2} and then check the ratio: e_h/e_{h/2} .
Repeat for discrete H1 norm (discrete L2 norm of difference of u_h) i.e.
||u||_{1,h}^2=\sum_{k=0}^{N-1} h*|D_hu(x_k)|^2
with D_h a forward difference.
-
Form the matrix and the right hand side vector for the FDM discretiation of the problem
-u''=f on [0,1], u'(0)=\alpha; u(1)=\beta (mixed bnd conditions)
picking f , \alpha,\beta for a known solution e.g., u(x)=\sin(x+1) (We would like to avoid sitution that u^{(k)}(\alpha)=0 for any k
which could artificially increase the order of convergence or of the local truncation error).
The Neumann condition at the left end of the interval we approximate by the forward difference. Compute the FDM solution - plot the FDM solution - plot the error - compute the
discrete norms of the error. Check experimentally the order of the error. Compute the error of the local truncation error.
-
Compute the order of error of standard FDM for - u''=f in [0,1] , u=sin(x) on the boundary for the known solution u(x)=\sin(x)
in L^2 and max discrete norms but on NO mesh which is ON THE BOUNDARY e.g. take the mesh (h*k) for k=0,...,N but for h < 1/N and introduce discrete zero bnd conditions for k=0,N.
One can use the old scripts e.g. taking the old mesh but be=sol(1+0.5h) so in fact we solve -u''=f on (0,1+h/2) u(0)=0 u(1+h/2)=sin(1+h/2)
-
Collatz approximation - test on - u''=f in [0,1] , u=0 on the boundary for the known solution u^*(x)=sin(x) or (1-x*x)sin(x), pick the mesh such that x_0=0 but x_N < 1 as in the previous problem
THE FDM scheme:
u_0=0, (-u_{k-1}+2u_k-u_{k+1})/(h*h)=f(x_k) k=1,..,N-1.
The last equation - Collatz approximation:
l1(1)=0=u^*(1) with l1 linear interpolant s.t. l1(x_k)=u_k k=N-1,N.
We have
l1(t)=u_{k-1}(t-x_k)/(-h)+u_k(t-x_{k-1})/h.
-
Test the fdm scheme for
- u''=f in [-1,1] , u(-1)=0 u'(1)=a
for the known solution u^*(x)=(x+1)\sin(x) - standard equidistant mesh, standard 3point stencil inside interval, but increase the order of the scheme at the right end. We add an extra so called ghost point x_{N+1]=1+h
and approximate u'(b) by the central difference getting the equation (u_{N+1}-u_{N-1})/(2h)=a.
Test the convergence error in the discrete max norm (and L^2). THis scheme has sense if we can extend the solution outside the interval i.e. if the solution is regular enough up to the boundary.
-
Test the fdm scheme for
- u''+cu =f in [a,b] , u(a)=al u'(b)=be
for the known solution e.g. u^*(x)=(x+1)\sin(x) - standard equidistant mesh, standard 3point stencil inside interval, but increase the order of the scheme at the right end. We add an extra Taylor expansion term i.e [u(b)-u(b-h)]/h=u'(b)-0.5hu''(b)+O(h^2) i.e. we approximate
u'(b)=be by [u(b)-u(b-h)]/h+0.5hu''(b)=be
but
-u''(b)=f(b)-c*u(b)
(assuming that the diff. equation is also valid at x=b) thus our last equation:
by
[u(N)-u(N-1)]/h+0.5hc*u(N)=be + 0.5*h*f(b).
Test the convergence error in the discrete max norm (and L^2). This scheme has sense if the solution is regular enough up to the boundary.
-
Form a 5-diagonal symmetric matrix - a FDM discretization of 2D Laplacian on a regular mesh on a square.
FDMDsolver.m
-m-file with the simplest FDM method (solver) for linear
boundary value ODE problem: -y''+cy=f; y(a)=al y(b)=be
fdm1d19.m
-script solving -u''+u=0 u(0)=u(b)=1 by FDM and shoooting method - and comparing the results- FDM works well for any b (reasonable), shootong stops working for b larger around 12-14
fdmLTE19.m
-script testing the order of LTE of FDM in max discrete norm for -u''=sin(x) u(0)=u(pi)=0 - the solution is sin(x) (please do the same in the discrete L2 norm ||uh||_{0,h}^2=h\sum_x uh(x)^2
testFDMConvOrd19.m
-m-file with tests of FDM schemes for boundary value ODE problems: -y''+cy=f; y(a)=al y(b)=be
or y'(b)=be - convergence order tests for standard mesh or mesh not containing the right end for Dirichlet bnd cond (with or without Collatz approximation), mixed bnd i.e. Dirichlet bnd cond at a and Neumann at b - the Neumann cond approxiamted by: backward difference (order 1); central difference (requirest an extra ghost point - order 2);
modified backward difference using the ODE equation at b (order 2)
Lab 7 FDM method in 2D
-
Solve the FDM problem
-Laplacian u=f in [0,1]^2 , u=0 on the boundary
for the known solution u(x)=\sin(\pi*x)\sin(\pi*y)
on the mesh of 10,20,40,80 points in each direction. Compute the discrete error in max and discrete L^2 norms.
Plot the error and the FDM solutions.
- Compute the order of local truncation error of standard FDM for -Laplacian u=f in [0,1]^2 ,
u=0 on the boundary for the known solution u(x)=\sin(\pi*x)\sin(\pi*y)
in L^2 and max discrete norms.
-
Compute the discrete maximum norm (matrix) of the matrix obtained in FDM for
-Laplacian u=f in [0,1]^2 , u=0 on the boundary
for difffrent N (or h=1/N).
This norm gives us the stability constant for the FDM scheme in the discrete maximum norm.
-
Compute the order of error of standard FDM for
-Laplacian u=f in [0,1]^2 , u=0 on the boundary
for the known solution
u(x)=\sin(\pi*x)\sin(\pi*y)
in L^2 and max discrete norms by the halving the mesh size method. I.e we compute the error
e_h and e_{h/2} and then check the ratio: e_h/e_{h/2} .
-
Compute the order of error of standard FDM for
-Laplacian u +c(x,y)u=f in [-1,1]^2 ,
u=0 on the boundary for c(x) nonnegative and discontinuous e.g. c(x,y)=1 for x<0 ; c(x,y)=0 otherwise for
the known solution u(x)=\sin(\pi*x)\sin(\pi*y)
in L^2 and max discrete norms.
-
Compute the order of error of standard FDM for
-Laplacian u + \vec{b}\nabla u=f in [-1,1]^2 ,
u=0 on the boundary for \vec{b} given constant vector e.g. \vec{b}=[1;1] or [1;0] etc for
the known solution u(x)=\sin(\pi*x)\sin(\pi*y)
in L^2 and max discrete norms. The gradient of u we can approximate by the respective central differences i.e.
\nabla u(x,y) \approx [u(x+h,y)-u(x-h,y),u(x,y+h)-u(x,y-h)]/(2h)
testFDM2d.m
-script testing the order of convergence of FDM in max and L2 discrete norms for - Laplacian u=2pi^2*sin(x)sin(y)
u=0 on boundary
- the solution is sin(pi*x)sin(pi*y)
Lab 8 - FEM in 1d
Finite element method (FDM) for -u'' +cu=f on [a,b] with Dirichlet bnd
condition. In almost all problems we use linear continuous finite element method i.e conforming P_1 FEM on any mesh (x_0,...,x_N)
with x_0=a and x_n=b.
Weak form: find u\in V_h s.t.
\int_a^b u'v' + c*u*v dx =\int_a^b f v dx \forall v \in V_h
- in nodal basis
we get the system (A+cB)u=F with A, B (or their submatrices...) obtained from the script below, the rhs for linear FEM we can approximate
by composite trapezoidal rule i.e. F=(f_i)_i with f_i=\int_a^b f\phi_i dx \approx 0.5*f(x_i)(x_{i-1}-x_{i+1}) i=1,...,N-1
(note that in case of equidistant mesh x_k=a+k*h we get f_i=h*f(x_i) as in FDM method...).
The H^1 seminorm and L^2 norm may be computed for u\inV_h as |u|_1^2=u^TAu and ||u||_0^2=u^TMu
- u vector of coefficients in nodal basis.
We computed during classes the stiffness and mass matrices for FEM on the following mesh:
a=x_0
A=( \int_a^b phi_k' phi_l' dx) and
M=( \int_a^b phi_k phi_l dx), namely:
\int_a^b \phi_k\phi_l dx=
| 0 | |k-l|>1;
|
| (1/3)*(|x_k-x_{k+1}|+|x_k-x_{k-1}|) | k=l ;
|
| (1/6)*|x_k-x_l| | |k-l|=1;
|
assuming x_{-1}=x_0,x_{N+1}=x_N.
\int_a^b phi_k'phi_l'dx= | 0 | |k-l|>1;
|
| (|x_k-x_{k+1}|^{-1}+|x_k-x_{k-1}|^{-1}) | k=l;
|
| -|x_k-x_l|^{-1} | |k-l|=1
|
taking |x_0-x_{-1}|^{-1}=|x_N-x_{N+1}|^{-1}=0.
Note that on the equidistant mesh and zero bnd (then k,l=1,...,N-1) A=(1/h)*tridiagonal matrix with 2 on the main diagonal and minus one on the subdiagonal and superdiagonal
and B=(h^2)*tridiagona matrix with 2/3 on the main diagonal and 1/6 on the remaining super and sub diagonals.
- FEM linear 1D; regular grid; Dirichlet homogeneous bnd conditions
Find FEM P_1 spproximation of -u'' +cu=f on [a,b] on a regular grid (x_k=a+k*h h=(b-a)/N) for u=\sin(\pi*x)
and a=0;b=\pi using the trapezoidal rule for rhs for N=10,20,40,80,160 .
Compute the L^2 , discrete \infty and H^1 norms of u_h-I_h u. Check the order by
the halving method. Compare with FDM method.
- FEM linear 1D; simple irregular grid; Dirichlet homogeneous bnd conditions
Find FEM P_1 spproximation of -u'' +cu=f u(a)=0=u(b) on [a,b] on [0,2\pi] for u=x^3*\sin(x)
on a simple irregular grid with
the 2h mesh on [0,\pi] and h mesh on [\pi,2\pi] .
Compute the L^2 , discrete \infty and H^1 norms of u_h-I_h u . Check the order by
the halving step method starting with h0=2\pi/40 .
-
FEM linear 1D; simple irregular grid; Dirichlet homogeneous bnd conditions
Find FEM P_1 spproximation of -u'' +cu=f u(a)=0=u(b) on [0,2\pi] for u=x^3*\sin(x) on the following irregular grid:
take grid getting denser closer to the right end :
take \{x_{n+1}\}\cup\{b\} for x_{n+1}=x_n+h_n\leq b with h_n=(b-a)/(sqrt(n)*N) and x_0=a ,
Compute the L^2 , discrete \infty and H^1 norms of u_h-I_h u . Check the order by
doubling N and computing the ratio of the error for N and 2N .
-
FEM linear 1D; any irregular grid; Dirichlet homogeneous bnd conditions
Find FEM P_1 spproximation of -u'' +cu=f u(a)=0=u(b) on [0,2\pi]
for u=x^3*\sin(x)
Then generate an initial grid as follows:
take any increasing function f such that f(0)=0 f(1)=1 and generate the
initial grid x(k)=a+f(k/N0)*(b-a) k=0,..,N0 obtaining T_0. Knowing grid T_k
generate T_{k+1} as follows - take all grid points of T_k plus midpoints between
grid points f T_k, i.e.add (x(j)+x(j+1))/2 for x(j) a grid point of T_k.
Let u_k be a linear FEM solution for the mesh T_k.
Compute the L^2 , discrete \infty and H^1 norms of u_k-I_k u.
Check the order by
computing the ratio of the respective error for T_k and T_{k+1} .
- FEM linear 1D; regular grid; mixed homogeneous bnd conditions}
Find FEM P_1 spproximation of -u''+cu=f u(0)=0; u'(b)=0
on equaidistant mesh. Compute the rhs using trapezoidal composite quadrature rule. on a regular
grid for u=sin(x) and a=0;b=1 using the trapezoidal rule for rhs for N=10,20,40,80,160 .
Compute the L^2 , discrete \infty and H^1 norms of u_h-I_h u . Check the order by
the halving step method. Compare with FDM methods.
Note that we must take larger submatrices i.e. our FEM space is spanned by (\phi_i)_{i=1}^N (\phi_N is this extra function)
- (extra problem)FEM linear 1D; general elliptic operator regular grid; Dirichlet homogeneous bnd conditions
Find FEM P_1 approximation of -(a(t)u')'+c(t)u=f(t) u(le)=u(re)=0
on equaidistant mesh. Compute the stifness matrix and the rhs vector using trapezoidal
composite quadrature rule. Test the convergence order for the known solution u=\sin(t) , a=1+t^2 , c=0
on [0,\pi] . Repeat the problem taking c(t)=1+t . Weak formulation is
find u \in V_h \int_le^re a(t)u'v' + c(t)uv dx=\int_le^re fvdx forall v \in V_h.
i.e. we get the system Au=F with A=(\int_le^re a(t)\phi_i'\phi_j' + c(t)\phi_i \phi_j)_{i,j}
- real H^1 error - it is easy to compute |u_h-I_hu|_1 (| |_s the H^s nseminorm; u_h discrete solution, I_h u - nodal interpolant of u the real solution (knon)) but to compute an approximation of |u - u_h|_1 (or |u-u_h|_0)
we need a quadrature using not only the nodal points but also the values inside - so we need a function which computes the value of v_j a FEM function at any x and then we can apply some quadrature rule e.g.
octave's integration blackbox: quad().
Write a function which for a real function u (e.g. sin(x)) and v_h a linear fem function i.e.
piecewise linear continuous computes |v_h-u|_s s=0,1. Then repeat the tests of convergence order of 1st few problems using this function.
- FEM linear 1D; regular grid; Dirichlet homogeneous bnd conditions; higher order quadrature rule for
RHS
Find FEM P_1 spproximation of -u'' +cu=f on [a,b] on a regular grid for u=\sin(\pi*x) and
a=0;b=\pi using the Simpson rule for rhs for N=10,20,40,80,160 .
Compute the L^2 , discrete \infty and H^1 norms of u_h-I_h u . Check the order by
the halving method. Compare with the results where the RHS was computed by the trapezoidal rule.
FEM1Dmats.m -
- function creates linear 1D linear FEM matrices for
-au''+cu=f
i.e.
A=(\int_a^b \phi_k'\phi_l')_{k,l=0}^N and B=(\int_a^b \phi_k\phi_l)_{k,l=0}^N
for \phi_k nodal basis - piecewise linear
- any grid - to get matrix for zero Dirichlet bnd - take submatrices of A and B
The returned matrices are for the nodal basis (\phi_i)_{i=0}^N i.e. includes
the basis functions related to bnd poits, in case of Dirichlet bnd we have to take
submatrices obtained by removing first and last rows/cols.
FEM1Dsolver.m -
- function solves using linear FEM on the given mesh the problem:
-au''+cu=f in (a,b)
u(a)=al, u(b)=be
The returned vector - solution contains the approximated values at the m mesh points -
the coefficients in the nodal basis (including boundary points).
testFEM1d.m -
some simple tests of FDM and FEM for -u''+cu=g in (a,b) u(a)=u(b)=0 (as wrotten during the lab)
Lab FD method for parabolic equations in 1D i.e. we discretized
equation u_t-u_{xx}=f u(t,a)=ga(t) u(t,b)=gb(t) u(0,x)=u0(x) by FDM with respect to x - and apply octave ODE
solver (lsode()) to the resulting ODEs system
-
solve
u_t=u_{xx} x\in(0,pi), t\in(0,T] u(t,0)=u(t,pi)=0, u0(x)=sin(x),
for known solution u(t,x)=exp(-t)sin(x).
Use lsode() for ODE solver for FDm semi-discrete problem: U'=-T U where T FDM discretization of
- 2nd derivative i.e. 3diagonal matrix (N-1)x(N-1) with 2/h^2 on the main diagonal, and -1/h^2 od sub and super diagonals, here h=(b-a)/N, U=(u(1),..,u(N-1))^T vector of values at discrete points.
- Compute the error u_h-r_h u for given t_n in a discrete max and L^2 norms. Test the
order of convergence i.e. let e(N)=||u_h-r_h u||_h then compute e(N)/e(2N).
Compute the global discrete error e.g. E_h=max_n ||u_h(t_n,*)-u(t_n,*)|_h
and then the E_h/E_{h/2}.
- Solve
u_t=u_{xx} u(t,a)=u(t,b)=0
with discontinuus u0 e.g.on [0,2] u0=0 on [0,1] and
u0(x)=1 on [1,2]. Plot the FDM solution for t=10,1,1e-1,1e-2 for
h=1/10,1/100,1/1000.
- Solve general problem: u_t-u_{xx}=f u(t,a)=ga(t) u(t,b)=gb(t) u(0,x)=u0(x) for any functions
f, ga,gb,u0.
- Using the function from the previous problem solve u_t-u_{xx}=0 with zero bnd and u0 being a peak e.g.
u0(x)=1e10 on t0+[-1e-7,1e7] (t0 given point) and zero otherwise
- change u0 to a unit function of [-1,1] for a=-2, b=2
- Change the Dirichelt bnd to Neumann (or mixed Neumann at a and Dirichlet at b) - test if it works and test convergence order with space step (i.e. takie initial h0 and repeat compution halving the step)
- Implement explicit, implicit Eulers and Crank-Nicholson FDM schemes for
u_t-u_{xx}=f u(t,a)=ga(a) u(t,b)=gb(b) u(0,x)=u0(x)
Test them for different valuers of parameters taking f=0 and ga=gb=0. Take different u0 (sin(x), sin(10x), 'a peak' etc)
Test the order of convergence in discrete max norm by the halving steps methos with respecrt to \tau and h. (for a known solution e.g.
u(t)=exp(-t)sin(x) a=0, b=\pi)
FDMDParabsol.m -
- function solves using linear FDM on the standard uniform equidistant mesh the problem:
d/dt u -au''+cu=f in (a,b)
u(t,a)=al(t), u(t,b)=be(t) boundary conditions
u(0,x)=u0(x) an initial value
The returned matrix - solution contains in rows the approximated values at interior points at the discrete times
i.e. each row is the FDM approximation (on equidistant mesh of (a,b)) of the solution of the respective discrete time
Octave scripts with solution of some problems from our labs
nrrbasic.m - a simple octave script
with basic operations like matrices multiplications etc
ODE schemes - simple implementations and tests
exEuler19.m - implementation of expicit Euler scheme
midpoint19.m - implementation of midpoint scheme
testexEuler19.m - test of explicit (forward) Euler in 1D and 2D
taylor19.m - implementation of the forward 2nd order Taylor scheme
AdamsBashford19.m
- explicit Adams-Bashforth scheme (of order 2)
Heun19.m
- explicit Heun scheme (a Runge-Kutta scheme of order 2)
testconord19.m
- tests of convergence order of the following schemes: explicit Euler (order 1), Adams-Bashforth 2 step, midpoint, Taylor of 2nd order, Heun schemes (of order 2)
teststart19.m
- tests of convergence order of Adams-Bashforth scheme for different starting values of x1 \approx x(t0+h)
-a/exact value i.e. x1=solution at t0+h b/ x1 computed by one step of forward Euler of order one c/x1 computed by one step of forward Heun scheme (of order 2)
EulerLTE19.m
- tests of order of LTE for Euler methods
HeunLTE19.m
- tests of order of LTE for Heun method
trapzPC.m
- predictor-corrector with Heun as the predictor and the trapezoid as the corrector
testtrapzPC.m
- tests of predictor-corrector with Heun as the predictor and the trapezoid as the corrector
linshoot.m
a function solving by a shooting method a linear 2nd order BVP: -y''+d(x)y'+c(x)y=f(x) y(a)=al y(b)=be
testshoot19.m some test of
-shooting method for nonlinear y''=F(x,y,y') y(a)=al y(b)=be
shooting.m
-a function solving by a shooting method a nonlinear BVP: y''=F(x,y,y') y(a)=al y(b)=be
FDM schemes - simple implementations and tests
FDMDsolver.m
-m-file with the simplest FDM method (solver) for linear
boundary value ODE problem: -y''+cy=f; y(a)=al y(b)=be
fdm1d19.m
-script solving -u''+u=0 u(0)=u(b)=1 by FDM and shoooting method - and comparing the results- FDM works well for any b (reasonable), shootong stops working for b larger around 12-14
fdmLTE19.m
-script testing the order of LTE of FDM in max discrete norm for -u''=sin(x) u(0)=u(pi)=0 - the solution is sin(x) (please do the same in the discrete L2 norm ||uh||_{0,h}^2=h\sum_x uh(x)^2
testFDMConvOrd19.m
-m-file with tests of FDM schemes for boundary value ODE problems: -y''+cy=f; y(a)=al y(b)=be
or y'(b)=be - convergence order tests for standard mesh or mesh not containing the right end for Dirichlet bnd cond (with or without Collatz approximation), mixed bnd i.e. Dirichlet bnd cond at a and Neumann at b - the Neumann cond approxiamted by: backward difference (order 1); central difference (requirest an extra ghost point - order 2);
modified backward difference using the ODE equation at b (order 2)
testFDM2d.m
-script testing the order of convergence of FDM in max and L2 discrete norms for - Laplacian u=2pi^2*sin(x)sin(y)
u=0 on boundary
- the solution is sin(pi*x)sin(pi*y)
FDM schemes - simple implementations and tests
FEM1Dmats.m -
- function creates linear 1D linear FEM matrices for
-au''+cu=f
i.e.
A=(\int_a^b \phi_k'\phi_l')_{k,l=0}^N and B=(\int_a^b \phi_k\phi_l)_{k,l=0}^N
for \phi_k nodal basis - piecewise linear
- any grid - to get matrix for zero Dirichlet bnd - take submatrices of A and B
The returned matrices are for the nodal basis (\phi_i)_{i=0}^N i.e. includes
the basis functions related to bnd poits, in case of Dirichlet bnd we have to take
submatrices obtained by removing first and last rows/cols.
Solvers for paraboli problems
FDMDParabsol.m -
- function solves using linear FDM on the standard uniform equidistant mesh the problem:
d/dt u -au''+cu=f in (a,b)
u(a)=al, u(b)=be (al be constant but can be easily changed to al(t) be(t))
u(0,x)=u0(x)
The returned matrix - solution contains in rows the approximated values at interior points at the discrete times
i.e. each row is the FDM approximation (on equidistant mesh of (a,b)) of the solution of the respective discrete time
My home page
Last update: February 7th, 2020.