Taking derivative an array which is a numerical value with respect to another array

172 ビュー (過去 30 日間)
Hello,
I need to take a derive of an array (1*101) (which is numerical not analytical)
with respect to another array (1*101). How could I do that in Matlab? Any help please?

採用された回答

Roger Stafford
Roger Stafford 2015 年 5 月 3 日
編集済み: Roger Stafford 2015 年 5 月 3 日
Here is a second order approximation to du/dx which should be somewhat more accurate than a first order approximation, and if u is a quadratic function of x, then du/dx is exact (except of course for round-off errors.) Also note that there are as many points in the derivative array as in u and x. I assume in this code that u and x are row vectors of the same length. Intervals in x are not required to be equal.
xd = diff([x(3),x,x(n-2)]); % <-- Corrected
ud = diff([u(3),u,u(n-2)]); % <-- Corrected
dudx = (ud(1:end-1)./xd(1:end-1).*xd(2:end) ...
+ ud(2:end)./xd(2:end).*xd(1:end-1)) ...
./ (xd(2:end)+xd(1:end-1));
  3 件のコメント
Roger Stafford
Roger Stafford 2015 年 5 月 3 日
編集済み: Roger Stafford 2015 年 5 月 3 日
I'm glad you caught that error, Mohammad. Thank you. I guess my ancient brain had a short-circuit there. The following is what I meant to use. At each end it yields a valid expression for the derivative at the end point for a second degree polynomial running through the three points at that end. Also I should have stated that each array must have a minimum of three elements.
xd = diff([x(3),x,x(n-2)]);
ud = diff([u(3),u,u(n-2)]);
dudx = (ud(1:end-1)./xd(1:end-1).*xd(2:end) ...
+ ud(2:end)./xd(2:end).*xd(1:end-1)) ...
./ (xd(2:end)+xd(1:end-1));
Roger Stafford
Roger Stafford 2015 年 5 月 3 日
The formula I gave you computes the derivative at one of three adjacent points of your data for the unique second degree polynomial which passes through all three points. Your data does not have to be a polynomial for this to be performed. It can be done on any three points provided they each have different values of x. For reasonably smooth data this usually provides a more accurate estimate of the actual derivative at that given point than is given by the slope of a straight line drawn through two adjacent points.
The code is vectorized so that it gives you the above derivative approximation at each point using it and the surrounding two points. The exception is that at the two end points which each have no points to one side it gives the derivative there using that point and the two points on other side.

サインインしてコメントする。

その他の回答 (1 件)

pfb
pfb 2015 年 5 月 2 日
if f is the vector representing f(x) and x contains the corresponding abscissae
df = diff(f)./diff(x)
note that this has one less element than f and x. You can choose
xd = (x(1:end-1)+x(2:end))/2
(i.e. the halfway points) as the corresponding abscissae.
  4 件のコメント
Mohammad Abouali
Mohammad Abouali 2015 年 5 月 3 日
編集済み: Mohammad Abouali 2015 年 5 月 3 日
Either use, sided stencil on the boundaries, or if you have boundary conditions use them to set some sort of ghost/dummy nodes.
If you are trying to handle the advective terms u.du/dx+v.du/dy ... in NS equation I suggest that you read Numerical Simulation in Fluid Dynamics: A Practical Introduction It gives a very nice introduction to numerical solutions of NS equations.
You can also look at some of the references in http://www.sciencedirect.com/science/article/pii/S1877050913003530
Meva
Meva 2015 年 5 月 3 日
Many thanks Mohammed. That is right.
I am trying to solve Navier Stokes.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by