Questions tagged [finite-difference]
31 questions
2
votes
1 answer
Finite difference method for solving the Klein-Gordon equation in Matlab
I am trying to numerically solve the Klein-Gordon equation that can be found here. To make sure I solved it correctly, I am comparing it with an analytical solution that can be found on the same link. I am using the finite difference method and…

Nikola Ristic
- 133
- 4
1
vote
0 answers
Applying Neumann BC on 2D Diffusion Equation on Python using Finite-Difference Method
Given a diffusion equation:
$$\frac{\partial S}{\partial t} = c \Big(\frac{\partial^2 S}{\partial x^2} + \frac{\partial^2 S}{\partial y^2} \Big)$$
with homogeneous Neumann Boundary Condition
$$\frac{\partial S}{\partial \nu} = 0,$$
where $\nu$ is an…

JEAD MACALISANG
- 21
- 3
1
vote
1 answer
How to calculate the discrete First Order Derivative of a function f using a Matrix Differential Operator in Matlab?
I want to calculate a discrete approximation of df/dx in Matlab using a vector f representing the function and a Matrix D representing a differential operator. f in this example is a simple sine wave such that the df/dx should be cos(x).
The…

Bulbasaur
- 135
- 5
1
vote
0 answers
Eigen solution for a simple sparse system of equations is wrong. Why?
I can't figure out why this code is not working:
#include
#include
#include
#include
// u''(x)= 1
// Boundary conditions: u(0) = u(L) = 0.
typedef Eigen::SparseMatrix SpMat;…
1
vote
2 answers
How can I calculate the second-order derivative of a vector using finite differences if the interval is non-constant?
Say I have vectors x and y and want to calculate the second derivative of y with respect to x using finite differences.
I'd do
x <- rnorm(2000)
y <- x^2
y = y[order(x)]
x = sort(x)
dydx = diff(y) / diff(x)
d2ydx2 = c(NA, NA, diff(dydx) /…

Anthony Tan
- 13
- 2
1
vote
1 answer
How to use finite difference method for non-uniform grid in python?
I have a function f = dp/de (variation in pressure as a function of variation in energy) and I would like to calculate this derivative with finite differences. I have a list for the p values and a list for the e values. However, the grid formed by…

Ramos
- 9
- 4
1
vote
0 answers
Why does changing the boundary conditions cause the finite difference algorithm to diverge?
I wrote a finite difference algorithm to solve the wave equation which is derived here.
When I ran my code, the plotted graphs of the numerical and analytical solution deviated, which is the problem I am trying to solve. The finite difference…

Nikola Ristic
- 133
- 4
1
vote
1 answer
Solving a tridiagonal matrix in python
I have been looking at numerical methods to solve differential equations for chemical reactions. Usually I put the differential equation into a tridiagonal matrix using finite difference method, and then using a column vector for the boundary…

simon
- 83
- 1
- 9
1
vote
0 answers
Finite difference scheme in Matlab
I am trying to implement a finite difference scheme for KdV equation in MATLAB, and I have most of the code ready, except for approximation at the first level using initial condition. It was suggested I use Euler's method to obtain 'u' at m=1, and…

dannyt
- 11
- 2
1
vote
2 answers
1D Finite Difference Wave Equation Cuda
I am new to Cuda. I am trying to solve the wave equation with the initial condition in the form of the Ricky momentum. The performance of the code is 12 GFlops, although my GPU performance is 3900. Why is the code so ineffective for me and how can I…

homidov
- 11
- 1
1
vote
0 answers
Implementing the Lanczos algorithm into C++ for a quantum anharmonic oscillator
Firstly, I would like to mention that I am a complete beginner when it comes to coding, let alone C++, so bear with me, as I need complete guidance. My task is to implement the Lanczos algorithm for the case of a 1-D anharmonic oscillator in C++,…

Cia
- 11
- 1
1
vote
0 answers
Remove for loops in favor of vectorization in Python
I am new to Python and have seen some articles and examples of vectorizing nested for loops, but I am unclear on how to go about that with some FDTD code that I wrote. The goal is to make the code cleaner and run more efficiently. Can someone help…

pahagen
- 23
- 5
0
votes
0 answers
curl of the velocity in cylindrical coordinates
I have tried to calculate the curl of the velocity in cylindrical coordinates, following the equations from https://en.wikipedia.org/wiki/Del_in_cylindrical_and_spherical_coordinates
I have assumed finite differences for my derivatives. This is the…

crc150
- 1
- 1
0
votes
0 answers
What algorithm can be used to solve a set of coupled ODEs and an algebraic equation in 2D?
I have 3 coupled (implicit) ODE and 1 algebraic equation to be solved iteratively in two dimension. The algebraic equation output depends on the output of 3 ODEs. However, all ODE output does not depend on rest ODE output (initial value problem). I…

Pratyush Anand
- 1
- 1
0
votes
0 answers
What is the matlab code for below integrals
I want to solve the following integral using numerical techniques.
My integral are
1.) I want to solve a PDE system which is given by
PDE system
I only required the integral terms. Here what I tried
%% Discretization of Age , Space and Time
% Time…

mathemaniac
- 1
- 1