diff vs. gradient
显示 更早的评论
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
采纳的回答
gradient uses central differences while diff(x) uses right-hand differences x(i+1)-x(i).
12 个评论
Look at my attached figure, should they really be that different?
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.
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.
I see no change in the attachment.
Sorry, check now
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.
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.
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))
Perhaps the better question is, if I have numerical X and Y data, what way should I differentiate y with respect to x?
Do you think diff(y) / diff(x) is the way to go here?
Yes!
If the X-data are not equi-spaced, use diff().
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Mathematics 的更多信息
另请参阅
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
