Finite difference method-convergence

Click For Summary

Discussion Overview

The discussion revolves around the implementation of the finite difference method for solving a fourth-order differential equation. Participants explore the rate of convergence, error behavior with varying step sizes, and specific formulations of the finite difference equations. The conversation includes technical details about the method, error calculations, and the correctness of given functions.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant seeks clarification on the formula for the rate of convergence and how to determine values for \( J_1 \) and \( J_2 \).
  • Another participant questions how the error behaves as the step size decreases, wondering if it increases or decreases.
  • Several participants discuss the specific fourth-order problem they are trying to solve, including boundary conditions and the formulation of the differential equation.
  • There is a claim that halving the step size results in the error being divided by 4, but this is challenged by another participant who suggests that a difference equation must be derived first.
  • Participants share the finite difference equations they derived for both the original problem and the transformed problem involving \( w(x) \).
  • One participant suggests a possible error in the formulation of the function \( f(x) \), leading to a verification process involving substitutions into the differential equation.
  • Another participant reports increasing errors when solving the difference equation with specific step sizes, suggesting numerical instability.
  • After confirming a correction to the function \( f(x) \), one participant asks if others know how to solve the difference equation.
  • Participants share their error calculations from their implementations of the finite difference method, comparing results and discussing the reference points used for these errors.

Areas of Agreement / Disagreement

There is no consensus on the behavior of the error with respect to step size, and participants express differing views on the correctness of the function \( f(x) \). The discussion remains unresolved regarding the implications of the derived equations and the stability of the numerical method.

Contextual Notes

Participants reference specific mathematical formulations and numerical methods without fully resolving the implications of their findings. There are indications of potential errors in the problem setup and numerical instability, but these issues remain open for further exploration.

Who May Find This Useful

This discussion may be useful for those interested in numerical methods for differential equations, particularly in the context of finite difference methods and convergence analysis.

mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hello! :)
I am implementing the finite difference method in a program in C and I got stuck at the rate of convergence.. The formula is \frac{log(\frac{e_{1}}{e_{2}})}{log(\frac{J_{2}}{J_{1}})}, right? where e_{i}=max|y_{j}^{J_{i}}-y(x_{j}^{J_{i}})|, 0<=j<=J_{i}. How can I find the J_{1} and J_{2}?
 
Last edited by a moderator:
Physics news on Phys.org
I would also know how the error changes while the step size is getting smaller? Is it getting smaller or bigger??
 
mathmari said:
Hello! :)
I am implementing the finite difference method in a program in C and I got stuck at the rate of convergence.. The formula is \frac{log(\frac{e_{1}}{e_{2}})}{log(\frac{J_{2}}{J_{1}})}, right? where e_{i}=max|y_{j}^{J_{i}}-y(x_{j}^{J_{i}})|, 0<=j<=J_{i}. How can I find the J_{1} and J_{2}?

Which finite difference method do you mean?
I looked it up on wiki and found a family of methods.
In particular wiki does not refer to $J_i$ or $y_{j}^{J_{i}}$.
Can you provide some more information?
 
I like Serena said:
Which finite difference method do you mean?
I looked it up on wiki and found a family of methods.
In particular wiki does not refer to $J_i$ or $y_{j}^{J_{i}}$.
Can you provide some more information?

I want to implement a finite difference method for a forth order problem.
$$y'''-q(x)y''(x)=f(x), \forall x \in [0,1]$$
$$y(0)=y(1)=y''(0)=y''(1)=0$$
where $f:[0,1] \rightarrow R$, $q:[0,1] \rightarrow R$, $q(x) \geq 0, \forall x \in [0,1]$

To find the solution y, we consider the function $w:[0,1] \rightarrow R$, where $w(x)=y''(x)$, so we have the following problem:
$w''(x)-q(x)w(x)=f(x), \forall x \in [0,1]$ (1)
$w(0)=w(1)=0$
and
$y''=w(x), \forall x \in [0,1]$
$y(0)=y(1)=0$For the problem (1) when we halve the step, the error get divided by 4, right?

But what happens for the whole problem??
 
mathmari said:
I want to implement a finite difference method for a forth order problem.
$$y'''-q(x)y''(x)=f(x), \forall x \in [0,1]$$
$$y(0)=y(1)=y''(0)=y''(1)=0$$
where $f:[0,1] \rightarrow R$, $q:[0,1] \rightarrow R$, $q(x) \geq 0, \forall x \in [0,1]$

To find the solution y, we consider the function $w:[0,1] \rightarrow R$, where $w(x)=y''(x)$, so we have the following problem:
$w''(x)-q(x)w(x)=f(x), \forall x \in [0,1]$ (1)
$w(0)=w(1)=0$
and
$y''=w(x), \forall x \in [0,1]$
$y(0)=y(1)=0$For the problem (1) when we halve the step, the error get divided by 4, right?

That sounds reasonable, but how did you get it?
Seems to me you have to write it as a difference equation and solve it before you can say anything...
 
I like Serena said:
That sounds reasonable, but how did you get it?
Seems to me you have to write it as a difference equation and solve it before you can say anything...

The solution of the original differential is given. It's $y(x)=sin(\pi x)$.
Also the functions $f,q$ are given, $f(x)=\pi^4 \sin(\pi x)-\pi^2 q(x) \sin(\pi x)$, $q=\sin(x)^2$.

The finite difference method for $w$ is:
$$\frac{W_{j-1}-2W_j+W_{j+1}}{h^2}-q(x_j)W_j=f(x_j), j=1,...,J-1$$
$$W_0=W_J=0$$

where $W_j$ is the approximation of $w(x_j)$.The finite difference method for $y$ is:
$$\frac{Y_{j-1}-2Y_j+Y_{j+1}}{h^2}=W_j, j=1,...,J-1$$
$$Y_0=Y_J=0$$

where $Y_j$ is the approximation of $y(x_j)$.In an other exercise there was a finite difference method for a second order problem of two points, and when I halved the step, the error got divided by 4. So I thought that so it would be for all second order problems.
 
mathmari said:
The solution of the original differential is given. It's $y(x)=sin(\pi x)$.
Also the functions $f,q$ are given, $f(x)=\pi^4 \sin(\pi x)-\pi^2 q(x) \sin(\pi x)$, $q=\sin(x)^2$.

The finite difference method for $w$ is:
$$\frac{W_{j-1}-2W_j+W_{j+1}}{h^2}-q(x_j)W_j=f(x_j), j=1,...,J-1$$
$$W_0=W_J=0$$

where $W_j$ is the approximation of $w(x_j)$.The finite difference method for $y$ is:
$$\frac{Y_{j-1}-2Y_j+Y_{j+1}}{h^2}=W_j, j=1,...,J-1$$
$$Y_0=Y_J=0$$

where $Y_j$ is the approximation of $y(x_j)$.In an other exercise there was a finite difference method for a second order problem of two points, and when I halved the step, the error got divided by 4. So I thought that so it would be for all second order problems.

Can it be that it should be $f(x)=\pi^4 \sin(\pi x)+\pi^2 q(x) \sin(\pi x)$?
With a $+$ instead of a $-$?

Assuming it is and rewriting the difference equation, I get:
$$W_{j+1}=h^2f(x_j) + (2 + h^2 q(x_j))W_j-W_{j-1}$$

If I solve it for h=0.1, h=0.05, and h=0.025, I get increasing errors.
I think it is numerically unstable.

That makes sense because an error in $W_j$ is blown up by at least a factor 2.
 
I like Serena said:
Can it be that it should be $f(x)=\pi^4 \sin(\pi x)+\pi^2 q(x) \sin(\pi x)$?
With a $+$ instead of a $-$?
No, it is like I wrote it, with a $-$.
 
Last edited by a moderator:
mathmari said:
No, it is like I wrote it, with a $-$.

Well, let's verify.
We have:
\begin{array}{}
y(x)&=&sin(\pi x) & (1)\\
f(x)&=&\pi^4 \sin(\pi x)-\pi^2 q(x) \sin(\pi x) & (2)\\
q(x)&=&\sin(x)^2 & (3)\\
w(x)&=&y''(x) & (4) \\
w''(x)-q(x)w(x)&=&f(x) & (5)
\end{array}

Substitute (1) in (4) to get:
$$w(x) = -\pi^2\sin(\pi x) \qquad\qquad\qquad (6)$$
Another 2 derivatives to get:
$$w''(x) = \pi^4\sin(\pi x) \qquad\qquad\qquad (7)$$
Substitute (3), (6) and (7) in (5) to get:
$$w''(x)-q(x)w(x) = \pi^4\sin(\pi x) - \sin(x)^2 \cdot -\pi^2\sin(\pi x)
= \pi^4\sin(\pi x) + \pi^2 \sin(x)^2 \sin(\pi x)$$
That looks like $f(x)$ except for the $+$ sign...
It looks like either $f(x)$ is wrong or your differential equation is wrong... :eek:
 
  • #10
I like Serena said:
Well, let's verify.
We have:
\begin{array}{}
y(x)&=&sin(\pi x) & (1)\\
f(x)&=&\pi^4 \sin(\pi x)-\pi^2 q(x) \sin(\pi x) & (2)\\
q(x)&=&\sin(x)^2 & (3)\\
w(x)&=&y''(x) & (4) \\
w''(x)-q(x)w(x)&=&f(x) & (5)
\end{array}

Substitute (1) in (4) to get:
$$w(x) = -\pi^2\sin(\pi x) \qquad\qquad\qquad (6)$$
Another 2 derivatives to get:
$$w''(x) = \pi^4\sin(\pi x) \qquad\qquad\qquad (7)$$
Substitute (3), (6) and (7) in (5) to get:
$$w''(x)-q(x)w(x) = \pi^4\sin(\pi x) - \sin(x)^2 \cdot -\pi^2\sin(\pi x)
= \pi^4\sin(\pi x) + \pi^2 \sin(x)^2 \sin(\pi x)$$
That looks like $f(x)$ except for the $+$ sign...
It looks like either $f(x)$ is wrong or your differential equation is wrong... :eek:

I asked the professor and he told me that the function $f$ was wrong. It is with a $+$.
 
  • #11
mathmari said:
I asked the professor and he told me that the function $f$ was wrong. It is with a $+$.

Good!
So do you know how to solve the difference equation?
 
  • #12
I like Serena said:
Good!
So do you know how to solve the difference equation?

I implemented the method to find the approximations $Y_j$ and found the following errors:
for $J=5$ $\text{error}=0.064097$
for $J=10$ $\text{error}=0.016395$

Could you tell me if these errors are right?
 
  • #13
mathmari said:
I implemented the method to find the approximations $Y_j$ and found the following errors:
for $J=5$ $\text{error}=0.064097$
for $J=10$ $\text{error}=0.016395$

Could you tell me if these errors are right?

Which method did you implement?
What is the reference point that you are using for your errors? :confused:

What I did, is use the shooting method to find the solution to your difference equation.
I compared that with the original function.
My error is the largest difference between the two (that occurs in the middle of the interval).
Chances are that you effectively did the same...
 
  • #14
I like Serena said:
Which method did you implement?
The finite difference method.

I like Serena said:
What is the reference point that you are using for your errors? :confused:

What I did, is use the shooting method to find the solution to your difference equation.
I compared that with the original function.
My error is the largest difference between the two (that occurs in the middle of the interval).
Chances are that you effectively did the same...

I wrote a program in C, that calculates first the approximations $W_j$, for each $j$, using the Gauss elimination for tridiagonal systems. Then having found these approximations, it calculates the approximations $Y_j$ with the same way... The error is $max_{1 \leq j \leq J-1}|Y_j-y(x_j)|$, where $y$ is the real solution.
 
  • #15
mathmari said:
The finite difference method.

I wrote a program in C, that calculates first the approximations $W_j$, for each $j$, using the Gauss elimination for tridiagonal systems. Then having found these approximations, it calculates the approximations $Y_j$ with the same way... The error is $max_{1 \leq j \leq J-1}|Y_j-y(x_j)|$, where $y$ is the real solution.

I have for J=10 an error in y of 0.1514.
 
  • #16
I like Serena said:
I have for J=10 an error in y of 0.1514.

Oh.. :confused: Ok.. I will check my program... And for J=20? When you double J, what happens with the error?
 
  • #17
mathmari said:
Oh.. :confused: Ok.. I will check my program... And for J=20? When you double J, what happens with the error?

With J=20, the maximum error in y that I get is 0.67848.
 
  • #18
I like Serena said:
With J=20, the maximum error in y that I get is 0.67848.

Maybe I've found wrong the approximations..

For $J=4$ the approximations $W_j$ are:
69.305796
-10.379758
-7.337134

and the approximations $Y_j$:
69.305796
-1.2877786
-0.414607

The first one of both approximations isn't right, is it?
Could tell me if your result are different?
 
  • #19
mathmari said:
Maybe I've found wrong the approximations..

For $J=4$ the approximations $W_j$ are:
69.305796
-10.379758
-7.337134

and the approximations $Y_j$:
69.305796
-1.2877786
-0.414607

The first one of both approximations isn't right, is it?
Could tell me if your result are different?

My previous result for $J=20$ was wrong, the error in $Y$ should have been $0.05877$.

With $J=4$, I'm getting for $W$:
-6.708713243
-7.283950347
-3.563132688

And for $Y$:
0.452783069
0.450319241
0.225159621

for an error in $Y$ of $0.54968$.
 
  • #20
I like Serena said:
My previous result for $J=20$ was wrong, the error in $Y$ should have been $0.05877$.

With $J=4$, I'm getting for $W$:
-6.708713243
-7.283950347
-3.563132688

And for $Y$:
0.452783069
0.450319241
0.225159621

for an error in $Y$ of $0.54968$.

I cannot find my error.. :(

To find the approximations $W_j$ I did the following:

Code:
for(j=0; j<J-1; j++){
                x=(j+1)*h;
		b[j]=f(x);	
}

For the Gauss elimination for tridiagonal systems, the system is $Aw=b$, where $b$ is calculated above and the matrix $A$ is tridiagonal which has the form:
Diagonal:-2.0/(h*h)-q(x), where x=(j+1)*h
Upper diagonal:1.0/(h*h)
Lower diagonal:1.0/(h*h)

Do you see something wrong? Did you find these approximations with an other way?
 
  • #21
mathmari said:
I cannot find my error.. :(

To find the approximations $W_j$ I did the following:

Code:
for(j=0; j<J-1; j++){
                x=(j+1)*h;
		b[j]=f(x);	
}

For the Gauss elimination for tridiagonal systems, the system is $Aw=b$, where $b$ is calculated above and the matrix $A$ is tridiagonal which has the form:
Diagonal:-2.0/(h*h)-q(x), where x=(j+1)*h
Upper diagonal:1.0/(h*h)
Lower diagonal:1.0/(h*h)

Do you see something wrong? Did you find these approximations with an other way?

That looks correct.
However, your value of 69.305796 for $W_1$ is clearly way too large.

I used a different method: the shooting method.
I wrote each value as a recursive relation depending on the previous 2 values.
Then I changed $W_1$ just as long until $W_J$ became 0.

Either way, you should get the same values.
How did you solve $Aw=b$?
Did you verify by evaluating $Aw$ with the result for $w$ you found?
 
  • #22
I like Serena said:
However, your value of 69.305796 for $W_1$ is clearly way too large.
Yes...I ran the program for several $J$, and for $J>4$ the values for $W_j$ are not so large...
For example for $J=5$ I get:
for $W_j$:
-5.671676
-8.580075
-7.553415
-3.738234

for $Y_j$:
0.538176
0.849485
0.817591
0.483560

I like Serena said:
How did you solve $Aw=b$?
A is a matrix of dimension (J-1)x3.
It has the form:
$\begin{bmatrix}
0 & d_1 & u_1\\
l_1 & d_2 &u_2 \\
l_2 & d_3 &u_3 \\
... & ... & ...\\
l_{J_3} & d_{J-2} & u_{J-2}\\
l_{J-2} & d_{J-1} & 0
\end{bmatrix}$
where $l_1,...,l_{J-1}$ are the elements of the lowdiagonal, $u_1,...,u_{J-1}$ the elements of the upperdiagonal and $d_1,...,d_{J-1}$ the elements of the diagonal.
then we shift the elements of the first row one place to the left and then we implement the Gauss elimination with pivot. We do that at each step..

I like Serena said:
Did you verify by evaluating $Aw$ with the result for $w$ you found?
No, I haven't done it yet..
 
  • #23
mathmari said:
A is a matrix of dimension (J-1)x3.
It has the form:
$\begin{bmatrix}
0 & d_1 & u_1\\
l_1 & d_2 &u_2 \\
l_2 & d_3 &u_3 \\
... & ... & ...\\
l_{J_3} & d_{J-2} & u_{J-2}\\
l_{J-2} & d_{J-1} & 0
\end{bmatrix}$
where $l_1,...,l_{J-1}$ are the elements of the lowdiagonal, $u_1,...,u_{J-1}$ the elements of the upperdiagonal and $d_1,...,d_{J-1}$ the elements of the diagonal.
then we shift the elements of the first row one place to the left and then we implement the Gauss elimination with pivot. We do that at each step..

That sounds about right... but it's easy to make a mistake there. :eek:
No, I haven't done it yet..

So will you? (Wasntme)
 
  • #24
I like Serena said:
So will you? (Wasntme)

For $J=5$ the matrix $A$ is $\begin{bmatrix}
-50.039470& 25 & 0 & 0\\
25& -50.151647 & 25 &0 \\
0 & 25 &-50.318821 & 25\\
0& 0 & 25& -50.514600
\end{bmatrix}
$

and $b=\begin{bmatrix}
57.484598\\
94.064990 \\
95.634182\\
60.240927
\end{bmatrix}$

Using the Matlab command w=A\b, I get the same $w$ as I get with my program:
$w=\begin{bmatrix}
-5.992187\\
-9.694450 \\
-9.692919\\
-5.989633
\end{bmatrix}$
 
  • #25
That means that the solution of the system is right, isn't it ?
 
  • #26
mathmari said:
Yes...I ran the program for several $J$, and for $J>4$ the values for $W_j$ are not so large...
For example for $J=5$ I get:
for $W_j$:
-5.671676
-8.580075
-7.553415
-3.738234

mathmari said:
For $J=5$ the matrix $A$ is $\begin{bmatrix}
-50.039470& 25 & 0 & 0\\
25& -50.151647 & 25 &0 \\
0 & 25 &-50.318821 & 25\\
0& 0 & 25& -50.514600
\end{bmatrix}
$

and $b=\begin{bmatrix}
57.484598\\
94.064990 \\
95.634182\\
60.240927
\end{bmatrix}$

Using the Matlab command w=A\b, I get the same $w$ as I get with my program:
$w=\begin{bmatrix}
-5.992187\\
-9.694450 \\
-9.692919\\
-5.989633
\end{bmatrix}$

mathmari said:
That means that the solution of the system is right, isn't it ?

Yep.
Did you fix something?
Because in your previous post you had a different solution for w...
 
  • #27
I like Serena said:
Yep.
Did you fix something?
Because in your previous post you had a different solution for w...

At the following part I replaced $J-1$ with $J$.
mathmari said:
Code:
for(j=0; j<J-1; j++){
                x=(j+1)*h;
		b[j]=f(x);	
}

Code:
for(j=0; j<J; j++){
                x=(j+1)*h;
		b[j]=f(x);	
}
 
  • #28
mathmari said:
At the following part I replaced $J-1$ with $J$.
Code:
for(j=0; j<J; j++){
                x=(j+1)*h;
		b[j]=f(x);	
}

Oh?
That should not make a difference?? :confused:
Can there be a $\pm 1$ mistake afterwards?
 
  • #29
I like Serena said:
Can there be a $\pm 1$ mistake afterwards?

What do you mean? :confused:
 
  • #30
mathmari said:
What do you mean? :confused:

You have measurements at $x_0, x_1, ..., x_J$.
That are $(J+1)$ measurements.
However, you can leave out $x_0$ and $x_J$ since they are both given as boundary conditions: they are 0.
The leaves you with $(J-1)$ measurements.
So your matrix should be $(J-1) \times (J-1)$, your $b$ should have $(J-1)$ entries, and your resulting $w$ should have $(J-1)$ entries.

However, your "fix" gives you $J$ entries instead of $(J-1)$ entries.
That is $1$ off.
If it helps you to get the right answer, it means that later on you are also $1$ off.
So before you were probably calculating with an uninitialized variable.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 17 ·
Replies
17
Views
4K
Replies
9
Views
2K
Replies
1
Views
11K
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
5K
Replies
0
Views
1K
  • · Replies 7 ·
Replies
7
Views
3K