# A Better way to find Finite Difference

1. Oct 16, 2017

### joshmccraney

Given a function $\psi$ having $N$ components, how would you (as fast as possible) construct $\psi'$ also having $N$ components? I thought about taking a forward finite difference approach on the first $N-1$ components of $\psi$ to generate $\psi'$ and then a backward finite difference approach to generate the last ($Nth$) component of $\psi'$.

Any faster ideas or better ways? Thanks!

2. Oct 16, 2017

### Paul Colby

What do you mean by fast? First thing that comes to mind is computation speed on a computer. If this is the case I recommend finding a stable and accurate algorithm for your application (BTW what would that be?) and optimize later if required. Using a canned solver is often the way to go. Sometimes choosing a "slower" algorithm step is less computation because fewer integration steps are required for the computation. All depends on what one is doing. You haven't supplied any info in that regard.

3. Oct 16, 2017

### joshmccraney

By fast I refer to computation time. That's why I'm here: to get others' opinions of a stable algorithm. The application is difficult to explain, but I need to compute $\psi'$. Not solving a PDE though. So a canned solver is your recommendation?

4. Oct 16, 2017

### Paul Colby

Okay, what ever. I assume you have $\psi_n$ for $n=1$ to $N$ and you would like $\psi'_n$ with no discussion of accuracy or constrains. What's wrong with $\psi'_n \approx \psi_{n} - \psi_{n-1}$?

Also, if $\psi(x)$ is given as an analytic function, take the derivative and evaluate $\psi'(x_n)$ works very well.

5. Oct 16, 2017

### joshmccraney

Accuracy of the technique is not an issue so long as it converges "reasonably well" (say decreasing step size by half leaves an error estimate of 1%). Since the step size is smaller than 1, so far I've been using $\psi'_n(x) \approx (\psi_{n} - \psi_{n-1})/dx$ where $dx$ is the step size. Even if $dx=1$, the numerical technique you suggest (which is also what I've been using) would give me $N-1$ values of $\psi$ unless I use a backward/forward technique on the last/first component, depending how you index. Is this okay?
Yea, I wish it were analytic but it's not, sadly.

6. Oct 16, 2017

### Paul Colby

No. "as long as it converges" - can't help with that given I don't know what "it" might be. I might suggest using two inboard values of $\psi'$ to extrapolate the missing end points. Good luck.

7. Oct 16, 2017

### joshmccraney

Sorry, "it" refers to the derivative, $\psi'$. Hmmmm so you're saying to compute the interior components of $\psi'$ and the extrapolate to find $\psi_1,\psi_N$?

8. Oct 16, 2017

### Paul Colby

I said extrapolate $\psi'$ not $\psi$. In the absence of other information like boundary conditions at $n=1$ and or $n=N$ such as one might find in a physical problem what else would one do?

9. Oct 16, 2017

### Nidum

Rolling splines and choose a spline function with a simple derivative .

10. Oct 16, 2017

### joshmccraney

Sorry, that's what I meant, extrapolate $\psi'$. Thanks to you both!

11. Oct 17, 2017

### Stephen Tashi

12. Oct 18, 2017

### Staff: Mentor

You're solving an ODE. Please give a decent idea what the problem is that you're trying to solve. It's very hard to give advice without adequate information.

13. Oct 18, 2017

### joshmccraney

It's ok, I ended up using a built in function, and it works very well and efficient. Thank you all for input though!