How to calculate the errors for Finite Element Method for 1D Poisson equation?
Show older comments
I have solved the 1D Poisson equation -u"(x) = sinx using the Finite Element Method over the period 0 to pi.
For n = 8 elements I got this as the (9x1) vector of nodal values:
[0; 0.3827; 0.7071; 0.9239; 1.0; 0.9239; 0.7071; 0.3827; 0]
For n = 16 elements I got this as the (17x1) vector of nodal values:
[0; 0.1951; 0.3827; 0.5556; 0.7071; 0.8315; 0.9239; 0.9808; 1.0; 0.9808; 0.9239; 0.8315; 0.7071; 0.5556; 0.3827; 0.1951; 0]
The Output is like this:

I have used xvec = linspace(0, pi, n+1) for the nodal points (1x9) vector. Now I want to calculate two error norms using the following formulas:

For the double absolute value i.e. norm, I am considering this
double( sqrt(int((error^2), x, 0, pi)) );
The output of errors should be like this in the loglog scale:

Can you help me out with this how to calculate and get the final plot? Thank you.
2 Comments
Torsten
on 26 Feb 2022
Hint:
The exact solution is u*(x) = sin(x).
To get a graph as shown, you will need more than two variations of the number of elements since you can always make a straight line through only two points.
Fahmid Mahmud
on 26 Feb 2022
Answers (1)
You must use higher precision for the output of the solution because now, the solution with 16 elements is classified worse than the solution with 8 elements:
u8 = [0; 0.3827; 0.7071; 0.9239; 1.0; 0.9239; 0.7071; 0.3827; 0];
u16 = [0; 0.1951; 0.3827; 0.5556; 0.7071; 0.8315; 0.9239; 0.9808; 1.0; 0.9808; 0.9239; 0.8315; 0.7071; 0.5556; 0.3827; 0.1951; 0];
x8 = linspace(0,pi,numel(u8)).';
x16 = linspace(0,pi,numel(u16)).';
ustar8 = sin(x8);
ustar16 = sin(x16);
eps_nodal8 = norm(u8-ustar8)/norm(ustar8)
eps_nodal16 = norm(u16-ustar16)/norm(ustar16)
eps_L2_8 = trapz(x8,abs(u8-ustar8))
eps_L2_16 = trapz(x16,abs(u16-ustar16))
9 Comments
Fahmid Mahmud
on 26 Feb 2022
At the same points, the values for 8 and 16 elements are exactly the same, the 16 elements has values in some extra points in the plot. Therefore the 16 elements should have given lower error norm.
No. The 16 elements vector should give a higher error norm since the error in the remaining 8 element positions is "added" (under the square root).
Fahmid Mahmud
on 26 Feb 2022
Torsten
on 26 Feb 2022
I made a mistake in the calculation of the L2-error.
The last two lines must read
eps_L2_8 = sqrt(trapz(x8,(u8-ustar8).^2))
eps_L2_16 = sqrt(trapz(x16,(u16-ustar16).^2))
You used the norm() function. Can the following be used to get the norm?
double( sqrt(int((error^2), x, 0, pi)) );
No, because you have no symbolic expressions, but numerical.
And can you explain what your this comment mean? You must use higher precision for the output of the solution because now, the solution with 16 elements is classified worse than the solution with 8 elements
Define
format long
to get the solution with more than 4 decimal places.
Fahmid Mahmud
on 26 Feb 2022
Fahmid Mahmud
on 27 Feb 2022
The expressions to evaluate must be calculated from the u_h values alone. If you used finite differences instead of finite elements, du_h/dx could be calculated as du_h/dx (xi) ~ (u_h(i+1)-u_h(i-1))/(x(i+1)-x(i-1)) for 2<=i<=n-1, du_h/dx(x1) = (u_h(2)-u_h(1))/(x(2)-x(1)), du_h(xn) = (u_h(n)-u_h(n-1))/(x(n)-x(n-1)). So an expression to compute eps_H1_8 would be
n8 = numel(u8);
x8 = linspace(0,pi,n8).';
du8 = [(u8(2)-u8(1))/(x8(2)-x8(1));(u8(3:n8)-u8(1:n8-2))./(x8(3:n8)-x8(1:n8-2));(u8(n8)-u8(n8-1))/(x8(n8)-x8(n8-1))];
dustar8 = cos(x8);
eps_H1_8 = sqrt(trapz(x8,(du8-dustar8).^2))
But I don't know how the first-order derivatives were approximated within the finite-element method you used. This method should be implemented to calculate du8.
Fahmid Mahmud
on 1 Mar 2022
Categories
Find more on Resampling Techniques in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
