Why does:
diffpressure = gradient(pressure)./gradient(density);
and
diffpressure = diff(pressure)./diff(density);
give drastically different results? The derivative using each method are completely different. See attached figure

 채택된 답변

Matt J
Matt J 2018년 11월 5일
편집: Matt J 2018년 11월 5일

2 개 추천

gradient uses central differences while diff(x) uses right-hand differences x(i+1)-x(i).

댓글 수: 12

Benjamin
Benjamin 2018년 11월 5일
Look at my attached figure, should they really be that different?
Matt J
Matt J 2018년 11월 5일
I don't see why not, but on the other hand the picture isn't all that informative. You should provide the input data in a .mat file.
Benjamin
Benjamin 2018년 11월 5일
Check file now. It shows the original function, and the derivative using diff and derivative using grad. Surely, the derivative is not that sensitive to the method. I have hundreds of points on the curve.
Matt J
Matt J 2018년 11월 5일
I see no change in the attachment.
Benjamin
Benjamin 2018년 11월 5일
Sorry, check now
Matt J
Matt J 2018년 11월 5일
편집: Matt J 2018년 11월 5일
You have only plotted one of the original functions. We cannot tell if this is density or pressure and without both we also cannot gain any insight into the ratio of their gradients.
Once again, the most helpful thing would be to attach your numeric data in a .mat file.
Matt J
Matt J 2018년 11월 5일
편집: Matt J 2018년 11월 5일
My preliminary guess would be that the gradient of density is tending toward zero, where the ratio calculation gradient(pressure)./gradient(density) becomes numerically unstable.
Benjamin
Benjamin 2018년 11월 5일
The y-axis is pressure, x-axis is density. Ok, I'll try and attach, but quick question: If I have x values and yvalues, how would I differentiate y wrt to x? do I do diff(y)/diff(x) or just do diff(y) and plot it as plot(x,diff(y))
Benjamin
Benjamin 2018년 11월 5일
Perhaps the better question is, if I have numerical X and Y data, what way should I differentiate y with respect to x?
Benjamin
Benjamin 2018년 11월 5일
Do you think diff(y) / diff(x) is the way to go here?
Star Strider
Star Strider 2018년 11월 5일
Yes!
Matt J
Matt J 2018년 11월 5일
편집: Matt J 2018년 11월 5일
If the X-data are not equi-spaced, use diff().

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

2018년 11월 5일

편집:

2018년 11월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by