Finite Difference Schemes in Matlab

In summary, the conversation revolves around setting up a basic difference scheme for solving the simplest hyperbolic equation using the explicit Euler scheme. The main goal is to examine the errors between the numerical and analytical solutions by varying the number of grid points. Suggestions are given on how to initialize the solution vector, set the initial condition, and implement the difference scheme. The importance of using a second-order approximation for the first derivative is also mentioned. The conversation ends with encouraging words and advice to keep practicing.
  • #1
skrieger
6
0

Homework Statement



This is for a course in numerical solutions of PDEs. So far it has all been theory of PDEs which is great since I am the worst programmer in the world. Right now I have to program the following: to solve the simplest hyperbolic equation,

du/dt = du/dx,

using the scheme

v(_j)(^n+1) = (1 + kD_0)v(_j)(^n)
v(_j)(^0) = sin(x_j)

for h(x-mesh size) = (2*pi)/(N+1), k(t-mesh size) = 2h/3, total time T = [0, 2*pi], in the three cases N = 20, 40 and 80.

the idea being to examine the errors between this result and the analytic solution, which should increase exponentially.


2. The attempt at a solution

I prefer to learn this in Matlab, but my knowledge base does not extend very far at all beyond plugging things into ode45. I just need someone to help me figure out how to set up basic difference schemes, I don't get the code beyond setting up linspaces, etc. So far all I have is

N = 32;
M = 20;
t = linspace(0,(2*pi),N)';
dt = t(2) - t(1);
x = linspace(0, (2*pi),M)';
dx = x(2) - x(1);
% Create the first derivative operator
D1 = diag(ones(N,1));
D2 = diag(-ones(N,1),-1);
D2 = D2(1:N,1:N);
D = (D1+D2)/((2*pi)/21);

but even if this is the proper formation of the derivative operator I'm not sure how to set up the actual scheme from here. No need for anyone to solve for the three N's or talk about errors, I just need help learning how to put these schemes together.

Thanks!
 
Physics news on Phys.org
  • #2




I understand that you are struggling with setting up a basic difference scheme for solving the simplest hyperbolic equation. As a scientist with experience in numerical solutions of PDEs, I would be happy to offer some guidance and suggestions.

First of all, it looks like you have already made a good start by defining your time and space grids. The next step would be to initialize your solution vector, v, and set the initial condition v(_j)(^0) = sin(x_j). This can be done using the linspace function as well, but you will also need to specify the size of your solution vector. For example, if you have N grid points in space and M grid points in time, your solution vector v should have dimensions (N,M). You can then use a for loop to set the initial condition for each grid point in space.

Next, you will need to set up the difference scheme itself. Looking at the scheme given in the problem, it appears to be an explicit Euler scheme. This means that the value at the next time step, v(_j)(^n+1), is calculated using the value at the current time step, v(_j)(^n). You can use another for loop to iterate through each time step and update the solution vector accordingly. Keep in mind that the scheme you have written in your attempt uses a first-order approximation for the first derivative, but the scheme given in the problem uses a second-order approximation. You may need to adjust your derivative operator accordingly.

Finally, to examine the errors between the numerical solution and the analytic solution, you can calculate the difference between the two at each grid point and plot it over time. This can give you an idea of how the errors change as you increase the number of grid points. You may also want to consider using a more advanced solver like ode45 to compare your results with the exact solution.

I hope this helps you in setting up your numerical scheme. Remember, practice makes perfect, so don't be discouraged if it takes a few tries to get it right. Good luck with your project!
 

1. What is a finite difference scheme?

A finite difference scheme is a numerical method used to approximate solutions to differential equations. It involves discretizing the domain into a finite number of points and approximating the derivatives at these points using finite differences.

2. How is a finite difference scheme implemented in Matlab?

In Matlab, a finite difference scheme can be implemented using a combination of for loops, if statements, and matrix operations. The domain can be discretized using a uniform grid, and the derivatives can be approximated using finite difference formulas.

3. What types of differential equations can be solved using finite difference schemes in Matlab?

Finite difference schemes in Matlab can be used to solve a wide range of differential equations, including ordinary differential equations, partial differential equations, and systems of differential equations. However, the accuracy and stability of the solutions may vary depending on the specific problem.

4. How do I choose the appropriate finite difference scheme for my problem?

The appropriate finite difference scheme for a problem depends on the type of differential equation, the boundary conditions, and the desired accuracy and stability of the solution. It is important to consider the order of accuracy, convergence properties, and stability of different schemes when choosing the best fit for a particular problem.

5. Are there any limitations or drawbacks to using finite difference schemes in Matlab?

While finite difference schemes can be a useful tool for solving differential equations, they also have some limitations and drawbacks. For example, they may not be suitable for problems with irregular domains or complex boundary conditions. Additionally, the accuracy of the solutions may be affected by the choice of discretization and the size of the grid used.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
899
  • Engineering and Comp Sci Homework Help
Replies
1
Views
897
  • Engineering and Comp Sci Homework Help
Replies
1
Views
792
  • Engineering and Comp Sci Homework Help
Replies
2
Views
742
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
487
  • Differential Equations
Replies
11
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Differential Equations
Replies
8
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
991
Back
Top